recoverable_array.bend

bendvy-ecs-core@0.1.0.0 · 13 lines · 0 definitions

1import Base
2# Library-owned inverse records. No arbitrary caller inverse or destructive callback.
3# Release rows retain the displaced owners; callers decide their subsequent use.
4type Record<P:Type> is Type:
5  Replaced{value:Array<P>}
6  Swapped{index:U32,value:P}
7def undone(~P:Type,index:U32,result:Array<P> & P) -> Array<P> & Record<P>:
8  match result:
9    case (array,replacement):(array,Swapped{index,replacement})
10def undo(~P:Type,record:Record<P>,array:Array<P>) -> Array<P> & Record<P>:
11  match record:
12    case Replaced{old}:(old,Replaced{array})
13    case Swapped{+index,old}:undone(~P,index,Array.swap(P,array,index,old))