Skip to content

Commit 8b1c690

Browse files
Fix build for desktop (crashes at runtime, web does not compile yet)
1 parent 7bda9ab commit 8b1c690

File tree

4 files changed

+55
-48
lines changed

4 files changed

+55
-48
lines changed

examples/storage/src/main.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ fn Storage() -> Element {
8383
let mut count_session = use_singleton_persistent(|| 0);
8484
let mut count_local = use_synced_storage::<LocalStorage, i32>("synced".to_string(), || 0);
8585

86-
let mut count_local_human = use_synced_storage::<HumanReadableStorage<LocalStorage>, i32>(
86+
let mut count_local_human = use_synced_storage::<HumanReadableStorage<LocalStorage, i32>, i32>(
8787
"synced_human".to_string(),
8888
|| 0,
8989
);
@@ -96,7 +96,7 @@ fn Storage() -> Element {
9696
},
9797
"Click me!"
9898
}
99-
"I persist for the current session. Clicked {count_session} times"
99+
"I persist for the current session. Clicked {count_session} times."
100100
}
101101
div {
102102
button {
@@ -105,7 +105,7 @@ fn Storage() -> Element {
105105
},
106106
"Click me!"
107107
}
108-
"I persist across all sessions. Clicked {count_local} times"
108+
"I persist across all sessions. Clicked {count_local} times."
109109
}
110110
div {
111111
button {
@@ -114,27 +114,29 @@ fn Storage() -> Element {
114114
},
115115
"Click me!"
116116
}
117-
"I persist a human readable value across all sessions. Clicked {count_local_human} times"
117+
"I persist a human readable value across all sessions. Clicked {count_local_human} times."
118118
}
119119
)
120120
}
121121

122122
// Define a "human readable" storage format which is pretty printed JSON instead of a compressed binary format.
123-
type HumanReadableStorage<Storage> = LayeredStorage<Storage, HumanReadableEncoding>;
123+
type HumanReadableStorage<Storage, T> = LayeredStorage<T, Storage, HumanReadableEncoding>;
124124

125125
#[derive(Clone)]
126126
struct HumanReadableEncoding;
127127

128-
impl StorageEncoder for HumanReadableEncoding {
129-
type Value = String;
128+
impl<T: Serialize + DeserializeOwned + Clone + 'static> StorageEncoder<T>
129+
for HumanReadableEncoding
130+
{
131+
type EncodedValue = String;
130132

131-
fn deserialize<T: DeserializeOwned + Clone + 'static>(loaded: &Self::Value) -> T {
133+
fn deserialize(loaded: &Self::EncodedValue) -> T {
132134
let parsed: Result<T, serde_json::Error> = serde_json::from_str(loaded);
133135
// This design probably needs an error handling policy better than panic.
134136
parsed.unwrap()
135137
}
136138

137-
fn serialize<T: Serialize + Send + Sync + Clone + 'static>(value: &T) -> Self::Value {
139+
fn serialize(value: &T) -> Self::EncodedValue {
138140
serde_json::to_string_pretty(value).unwrap()
139141
}
140142
}

packages/storage/src/client_storage/fs.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ impl StoragePersistence for LocalStorage {
9292
// Note that this module contains an optimization that differs from the web version. Dioxus Desktop runs all windows in
9393
// the same thread, meaning that we can just directly notify the subscribers via the same channels, rather than using the
9494
// storage event listener.
95-
impl StorageSubscriber<LocalStorage> for LocalStorage {
96-
fn subscribe<T: DeserializeOwned + Serialize + Send + Sync + Clone + 'static>(
95+
impl<T: DeserializeOwned + Serialize + Send + Sync + Clone + 'static>
96+
StorageSubscriber<LocalStorage, T> for LocalStorage
97+
{
98+
fn subscribe(
9799
key: &<LocalStorage as StorageBacking<T>>::Key,
98100
) -> Receiver<StorageChannelPayload> {
99101
// Initialize the subscriptions map if it hasn't been initialized yet.
@@ -118,7 +120,7 @@ impl StorageSubscriber<LocalStorage> for LocalStorage {
118120
}
119121
}
120122

121-
fn unsubscribe(key: &<LocalStorage as StorageBacking>::Key) {
123+
fn unsubscribe(key: &<LocalStorage as StorageBacking<T>>::Key) {
122124
trace!("Unsubscribing from \"{}\"", key);
123125

124126
// Fail silently if unsubscribe is called but the subscriptions map isn't initialized yet.

packages/storage/src/client_storage/memory.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ use crate::StorageBacking;
1010
#[derive(Clone)]
1111
pub struct SessionStorage;
1212

13-
impl StorageBacking for SessionStorage {
13+
impl<T: Clone + 'static> StorageBacking<T> for SessionStorage {
1414
type Key = String;
1515

16-
fn set<T: Clone + 'static>(key: String, value: &T) {
16+
fn set(key: String, value: &T) {
1717
let session = SessionStore::get_current_session();
1818
session.borrow_mut().insert(key, Arc::new(value.clone()));
1919
}
2020

21-
fn get<T: Clone + 'static>(key: &String) -> Option<T> {
21+
fn get(key: &String) -> Option<T> {
2222
let session = SessionStore::get_current_session();
2323
let read_binding = session.borrow();
2424
let value_any = read_binding.get(key)?;

packages/storage/src/lib.rs

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
146146
pub fn use_synced_storage<S, T>(key: S::Key, init: impl FnOnce() -> T) -> Signal<T>
147147
where
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.
162162
pub fn new_synced_storage<S, T>(key: S::Key, init: impl FnOnce() -> T) -> Signal<T>
163163
where
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>
205205
where
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>
234234
where
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)]
303303
pub 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

313313
impl<S, T> SyncedStorageEntry<S, T>
314314
where
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

353353
impl<S, T> StorageEntryTrait<S, T> for SyncedStorageEntry<S, T>
354354
where
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

582585
impl 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;
722725
impl<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

Comments
 (0)