11use caw_builder_proc_macros:: builder;
22use caw_core:: { Buf , Sig , SigCtx , SigT } ;
3- use caw_persistent :: PersistentWitness ;
3+ use caw_persist :: Persist ;
44use itertools:: izip;
55use serde:: { Deserialize , Serialize } ;
66
@@ -42,54 +42,51 @@ impl<T> Sequence<T> {
4242 }
4343}
4444
45- struct KeyLooperPersistentWitness ;
46- impl < T > PersistentWitness < Sequence < Option < T > > > for KeyLooperPersistentWitness
47- where
48- T : Serialize + for < ' a > Deserialize < ' a > ,
49- {
50- const NAME : & ' static str = "key_looper" ;
51- }
45+ const KEY_LOOPER_PERSIST : & ' static str = "key_looper" ;
5246
53- pub trait KeyLooperPersist < T > {
47+ /// Driver for saving and loading a sequence state to a file.
48+ pub trait KeyLooperIo < T > {
5449 fn load ( & self ) -> Sequence < Option < T > > ;
5550 fn save ( & self , sequence : & Sequence < Option < T > > ) ;
5651}
5752
58- pub struct KeyLooperPersistNone ;
59- impl < T > KeyLooperPersist < T > for KeyLooperPersistNone {
53+ /// Implementation of `KeyLooperIo` which doesn't actually save or load any data.
54+ pub struct KeyLooperIoNull ;
55+ impl < T > KeyLooperIo < T > for KeyLooperIoNull {
6056 fn load ( & self ) -> Sequence < Option < T > > {
6157 Sequence :: new_with ( 1 , || None )
6258 }
6359
6460 fn save ( & self , _sequence : & Sequence < Option < T > > ) { }
6561}
6662
67- pub struct KeyLooperPersistWithName ( pub String ) ;
68- impl < T > KeyLooperPersist < T > for KeyLooperPersistWithName
63+ /// Implementation of `KeyLooperIo` which saves state into a file of a given name.
64+ pub struct KeyLooperIoWithName ( pub String ) ;
65+ impl < T > KeyLooperIo < T > for KeyLooperIoWithName
6966where
7067 T : Serialize + for < ' a > Deserialize < ' a > ,
7168{
7269 fn load ( & self ) -> Sequence < Option < T > > {
73- if let Some ( sequence) = KeyLooperPersistentWitness . load_ ( & self . 0 ) {
70+ if let Some ( sequence) = KEY_LOOPER_PERSIST . load_ ( & self . 0 ) {
7471 sequence
7572 } else {
7673 Sequence :: new_with ( 1 , || None )
7774 }
7875 }
7976
8077 fn save ( & self , sequence : & Sequence < Option < T > > ) {
81- KeyLooperPersistentWitness . save_ ( sequence, & self . 0 )
78+ KEY_LOOPER_PERSIST . save_ ( sequence, & self . 0 )
8279 }
8380}
8481
85- pub struct KeyLooper < X , S , T , C , N , P >
82+ pub struct KeyLooper < X , S , T , C , N , I >
8683where
8784 X : Clone ,
8885 S : SigT < Item = Option < X > > ,
8986 T : SigT < Item = bool > ,
9087 C : SigT < Item = bool > ,
9188 N : SigT < Item = u32 > ,
92- P : KeyLooperPersist < X > ,
89+ I : KeyLooperIo < X > ,
9390{
9491 sig : S ,
9592 last_value : Option < X > ,
@@ -98,17 +95,17 @@ where
9895 length : N ,
9996 sequence : Sequence < S :: Item > ,
10097 buf : Vec < S :: Item > ,
101- persist : P ,
98+ io : I ,
10299}
103100
104- impl < X , S , T , C , N , P > SigT for KeyLooper < X , S , T , C , N , P >
101+ impl < X , S , T , C , N , I > SigT for KeyLooper < X , S , T , C , N , I >
105102where
106103 X : Clone ,
107104 S : SigT < Item = Option < X > > ,
108105 T : SigT < Item = bool > ,
109106 C : SigT < Item = bool > ,
110107 N : SigT < Item = u32 > ,
111- P : KeyLooperPersist < X > ,
108+ I : KeyLooperIo < X > ,
112109{
113110 type Item = S :: Item ;
114111
@@ -144,31 +141,31 @@ where
144141 * out = self . sequence . current ( ) . clone ( ) ;
145142 }
146143 if changed_this_frame {
147- self . persist . save ( & self . sequence ) ;
144+ self . io . save ( & self . sequence ) ;
148145 }
149146 & self . buf
150147 }
151148}
152149
153- impl < X , S , T , C , N , P > KeyLooper < X , S , T , C , N , P >
150+ impl < X , S , T , C , N , I > KeyLooper < X , S , T , C , N , I >
154151where
155152 X : Clone ,
156153 S : SigT < Item = Option < X > > ,
157154 T : SigT < Item = bool > ,
158155 C : SigT < Item = bool > ,
159156 N : SigT < Item = u32 > ,
160- P : KeyLooperPersist < X > ,
157+ I : KeyLooperIo < X > ,
161158{
162- fn new ( sig : S , tick : T , clearing : C , length : N , persist : P ) -> Sig < Self > {
159+ fn new ( sig : S , tick : T , clearing : C , length : N , io : I ) -> Sig < Self > {
163160 Sig ( KeyLooper {
164161 sig,
165162 last_value : None ,
166163 tick,
167164 clearing,
168165 length,
169- sequence : persist . load ( ) ,
166+ sequence : io . load ( ) ,
170167 buf : Vec :: new ( ) ,
171- persist ,
168+ io ,
172169 } )
173170 }
174171}
@@ -178,7 +175,7 @@ builder! {
178175 #[ constructor_doc = "A looper for key presses" ]
179176 #[ generic_setter_type_name = "X" ]
180177 #[ build_fn = "KeyLooper::new" ]
181- #[ build_ty = "Sig<KeyLooper<V, S, T, C, N, P >>" ]
178+ #[ build_ty = "Sig<KeyLooper<V, S, T, C, N, I >>" ]
182179 #[ extra_generic( "V" , "Clone" ) ]
183180 pub struct KeyLooperBuilder {
184181 #[ generic_with_constraint = "SigT<Item = Option<V>>" ]
@@ -195,26 +192,26 @@ builder! {
195192 #[ generic_name = "N" ]
196193 #[ default = 16 ]
197194 length: u32 ,
198- #[ generic_with_constraint = "KeyLooperPersist <V>" ]
199- #[ default = KeyLooperPersistNone ]
200- #[ generic_name = "P " ]
201- persist : KeyLooperPersistNone ,
195+ #[ generic_with_constraint = "KeyLooperIo <V>" ]
196+ #[ default = KeyLooperIoNull ]
197+ #[ generic_name = "I " ]
198+ io : KeyLooperIoNull ,
202199 }
203200}
204201
205- impl < X , S , T , C , N , P > KeyLooperBuilder < X , S , T , C , N , P >
202+ impl < X , S , T , C , N , I > KeyLooperBuilder < X , S , T , C , N , I >
206203where
207204 X : Clone + Serialize + for < ' a > Deserialize < ' a > ,
208205 S : SigT < Item = Option < X > > ,
209206 T : SigT < Item = bool > ,
210207 C : SigT < Item = bool > ,
211208 N : SigT < Item = u32 > ,
212- P : KeyLooperPersist < X > ,
209+ I : KeyLooperIo < X > ,
213210{
214211 pub fn persist_with_name (
215212 self ,
216213 name : impl AsRef < str > ,
217- ) -> KeyLooperBuilder < X , S , T , C , N , KeyLooperPersistWithName > {
214+ ) -> KeyLooperBuilder < X , S , T , C , N , KeyLooperIoWithName > {
218215 let Self {
219216 sig,
220217 trig,
@@ -227,7 +224,7 @@ where
227224 trig,
228225 clearing,
229226 length,
230- persist : KeyLooperPersistWithName ( name. as_ref ( ) . to_string ( ) ) ,
227+ io : KeyLooperIoWithName ( name. as_ref ( ) . to_string ( ) ) ,
231228 }
232229 }
233230}
0 commit comments