@@ -145,7 +145,7 @@ where
145145/// The changes to the state will be persisted to storage and all other app sessions will be notified of the change to update their local state.
146146pub fn use_synced_storage < S , T > ( key : S :: Key , init : impl FnOnce ( ) -> T ) -> Signal < T >
147147where
148- S : StorageBacking < T > + StorageSubscriber < S > ,
148+ S : StorageBacking < T > + StorageSubscriber < S , T > ,
149149 T : Serialize + DeserializeOwned + Clone + Send + Sync + PartialEq + ' static ,
150150 S :: Key : Clone ,
151151{
@@ -161,7 +161,7 @@ where
161161/// The changes to the state will be persisted to storage and all other app sessions will be notified of the change to update their local state.
162162pub fn new_synced_storage < S , T > ( key : S :: Key , init : impl FnOnce ( ) -> T ) -> Signal < T >
163163where
164- S : StorageBacking < T > + StorageSubscriber < S > ,
164+ S : StorageBacking < T > + StorageSubscriber < S , T > ,
165165 T : Serialize + DeserializeOwned + Clone + Send + Sync + PartialEq + ' static ,
166166 S :: Key : Clone ,
167167{
@@ -203,7 +203,7 @@ pub fn use_synced_storage_entry<S, T>(
203203 init : impl FnOnce ( ) -> T ,
204204) -> SyncedStorageEntry < S , T >
205205where
206- S : StorageBacking < T > + StorageSubscriber < S > ,
206+ S : StorageBacking < T > + StorageSubscriber < S , T > ,
207207 T : Serialize + DeserializeOwned + Clone + Send + Sync + PartialEq + ' static ,
208208 S :: Key : Clone ,
209209{
@@ -232,7 +232,7 @@ pub fn new_synced_storage_entry<S, T>(
232232 init : impl FnOnce ( ) -> T ,
233233) -> SyncedStorageEntry < S , T >
234234where
235- S : StorageBacking < T > + StorageSubscriber < S > ,
235+ S : StorageBacking < T > + StorageSubscriber < S , T > ,
236236 T : Serialize + DeserializeOwned + Clone + PartialEq + Send + Sync + ' static ,
237237 S :: Key : Clone ,
238238{
@@ -301,7 +301,7 @@ pub trait StorageEntryTrait<S: StorageBacking<T>, T: PartialEq + Clone + 'static
301301/// A wrapper around StorageEntry that provides a channel to subscribe to updates to the underlying storage.
302302#[ derive( Clone ) ]
303303pub struct SyncedStorageEntry <
304- S : StorageBacking < T > + StorageSubscriber < S > ,
304+ S : StorageBacking < T > + StorageSubscriber < S , T > ,
305305 T : Serialize + DeserializeOwned + Clone + Send + Sync + PartialEq + ' static ,
306306> {
307307 /// The underlying StorageEntry that is used to store the data and track the state
@@ -312,11 +312,11 @@ pub struct SyncedStorageEntry<
312312
313313impl < S , T > SyncedStorageEntry < S , T >
314314where
315- S : StorageBacking < T > + StorageSubscriber < S > ,
315+ S : StorageBacking < T > + StorageSubscriber < S , T > ,
316316 T : Serialize + DeserializeOwned + Clone + Send + Sync + PartialEq + ' static ,
317317{
318318 pub fn new ( key : S :: Key , data : T ) -> Self {
319- let channel = S :: subscribe :: < T > ( & key) ;
319+ let channel = S :: subscribe ( & key) ;
320320 Self {
321321 entry : StorageEntry :: new ( key, data) ,
322322 channel,
@@ -352,7 +352,7 @@ where
352352
353353impl < S , T > StorageEntryTrait < S , T > for SyncedStorageEntry < S , T >
354354where
355- S : StorageBacking < T > + StorageSubscriber < S > ,
355+ S : StorageBacking < T > + StorageSubscriber < S , T > ,
356356 T : Serialize + DeserializeOwned + Clone + Send + Sync + PartialEq + ' static ,
357357{
358358 fn save ( & self ) {
@@ -420,7 +420,8 @@ where
420420 }
421421
422422 fn update ( & mut self ) {
423- self . data = S :: get ( & self . key ) . unwrap_or ( self . data ) ;
423+ let read = S :: get ( & self . key ) ;
424+ self . data . set ( read. unwrap ( ) ) ;
424425 }
425426
426427 fn key ( & self ) -> & S :: Key {
@@ -488,12 +489,15 @@ pub trait StoragePersistence: Clone + 'static {
488489 fn store ( key : Self :: Key , value : & Self :: Value ) ;
489490}
490491
491- /// New trait which can be implemented to define a data format for storage.
492- pub trait StorageEncoder < T > : Clone + ' static {
493- /// The type of value which can be stored.
494- type Value ;
495- fn deserialize ( loaded : & Self :: Value ) -> T ;
496- fn serialize ( value : & T ) -> Self :: Value ;
492+ /// Defines a data encoding for storage.
493+ ///
494+ /// Encodes a `Value` into `EncodedValue`.
495+ pub trait StorageEncoder < Value > : Clone + ' static {
496+ /// The type which the storied entries are encoded into.
497+ type EncodedValue ;
498+ /// TODO: support errors for this codepath
499+ fn deserialize ( loaded : & Self :: EncodedValue ) -> Value ;
500+ fn serialize ( value : & Value ) -> Self :: EncodedValue ;
497501}
498502
499503/// A way to create a StorageEncoder out of the two layers.
@@ -522,7 +526,7 @@ impl<
522526 T : DeserializeOwned + Clone + ' static ,
523527 Value ,
524528 P : StoragePersistence < Value = Option < Value > > ,
525- E : StorageEncoder < T , Value = Value > ,
529+ E : StorageEncoder < T , EncodedValue = Value > ,
526530> StorageBacking < T > for LayeredStorage < T , P , E >
527531{
528532 type Key = P :: Key ;
@@ -544,28 +548,27 @@ impl<
544548 Value ,
545549 Key ,
546550 P : StoragePersistence < Value = Option < Value > , Key = Key >
547- + StorageSubscriber < P >
548- + StorageBacking < Key = Key > ,
549- E : StorageEncoder < Value = Value > ,
550- > StorageSubscriber < LayeredStorage < P , E > > for LayeredStorage < P , E >
551+ + StorageSubscriber < P , T >
552+ + StorageBacking < T , Key = Key > ,
553+ E : StorageEncoder < T , EncodedValue = Value > ,
554+ T : DeserializeOwned + Send + Sync + Clone + ' static ,
555+ > StorageSubscriber < LayeredStorage < T , P , E > , T > for LayeredStorage < T , P , E >
551556{
552- fn subscribe < T : DeserializeOwned + Send + Sync + Clone + ' static > (
553- key : & <LayeredStorage < P , E > as StorageBacking >:: Key ,
557+ fn subscribe (
558+ key : & <LayeredStorage < T , P , E > as StorageBacking < T > >:: Key ,
554559 ) -> Receiver < StorageChannelPayload > {
555- P :: subscribe :: < T > ( key)
560+ P :: subscribe ( key)
556561 }
557562
558- fn unsubscribe ( key : & <LayeredStorage < P , E > as StorageBacking >:: Key ) {
563+ fn unsubscribe ( key : & <LayeredStorage < T , P , E > as StorageBacking < T > >:: Key ) {
559564 P :: unsubscribe ( key)
560565 }
561566}
562567
563568/// A trait for a subscriber to events from a storage backing
564- pub trait StorageSubscriber < S : StorageBacking > {
569+ pub trait StorageSubscriber < S : StorageBacking < T > , T : Clone + ' static > {
565570 /// Subscribes to events from a storage backing for the given key
566- fn subscribe < T : DeserializeOwned + Send + Sync + Clone + ' static > (
567- key : & S :: Key ,
568- ) -> Receiver < StorageChannelPayload > ;
571+ fn subscribe ( key : & S :: Key ) -> Receiver < StorageChannelPayload > ;
569572 /// Unsubscribes from events from a storage backing for the given key
570573 fn unsubscribe ( key : & S :: Key ) ;
571574}
@@ -581,14 +584,14 @@ pub struct StorageSubscription {
581584
582585impl StorageSubscription {
583586 pub fn new <
584- S : StorageBacking < T > + StorageSubscriber < S > ,
587+ S : StorageBacking < T > + StorageSubscriber < S , T > ,
585588 T : DeserializeOwned + Send + Sync + Clone + ' static ,
586589 > (
587590 tx : Sender < StorageChannelPayload > ,
588591 key : S :: Key ,
589592 ) -> Self {
590593 let getter = move || {
591- let data = S :: get :: < T > ( & key) . unwrap ( ) ;
594+ let data = S :: get ( & key) . unwrap ( ) ;
592595 StorageChannelPayload :: new ( data)
593596 } ;
594597 Self {
@@ -722,14 +725,14 @@ struct DefaultEncoder;
722725impl < T : DeserializeOwned + Clone + ' static + Serialize + Send + Sync > StorageEncoder < T >
723726 for DefaultEncoder
724727{
725- type Value = String ;
728+ type EncodedValue = String ;
726729
727- fn deserialize ( loaded : & Self :: Value ) -> T {
730+ fn deserialize ( loaded : & Self :: EncodedValue ) -> T {
728731 // TODO: handle errors
729732 try_serde_from_string ( loaded) . unwrap ( )
730733 }
731734
732- fn serialize ( value : & T ) -> Self :: Value {
735+ fn serialize ( value : & T ) -> Self :: EncodedValue {
733736 serde_to_string ( value)
734737 }
735738}
0 commit comments