fix serializing signals

This commit is contained in:
Evan Almloff 2024-01-24 10:54:17 -06:00
parent 87dc409837
commit 55b37cc381
3 changed files with 29 additions and 7 deletions

View file

@ -15,6 +15,26 @@ impl<T: 'static, S: Storage<SignalData<T>>> From<Signal<T, S>> for ReadOnlySigna
} }
} }
#[cfg(feature = "serde")]
impl<T: serde::Serialize + 'static, Store: Storage<SignalData<T>>> serde::Serialize
for ReadOnlySignal<T, Store>
{
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.read().serialize(serializer)
}
}
#[cfg(feature = "serde")]
impl<'de, T: serde::Deserialize<'de> + 'static, Store: Storage<SignalData<T>>>
serde::Deserialize<'de> for ReadOnlySignal<T, Store>
{
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Ok(Self::new_maybe_sync(Signal::new_maybe_sync(
T::deserialize(deserializer)?,
)))
}
}
impl<T: 'static> ReadOnlySignal<T> { impl<T: 'static> ReadOnlySignal<T> {
/// Create a new read-only signal. /// Create a new read-only signal.
#[track_caller] #[track_caller]

View file

@ -50,7 +50,7 @@ pub struct CopyValue<T: 'static, S: Storage<T> = UnsyncStorage> {
} }
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
impl<T: 'static> serde::Serialize for CopyValue<T> impl<T: 'static, Store: Storage<T>> serde::Serialize for CopyValue<T, Store>
where where
T: serde::Serialize, T: serde::Serialize,
{ {
@ -60,14 +60,14 @@ where
} }
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
impl<'de, T: 'static> serde::Deserialize<'de> for CopyValue<T> impl<'de, T: 'static, Store: Storage<T>> serde::Deserialize<'de> for CopyValue<T, Store>
where where
T: serde::Deserialize<'de>, T: serde::Deserialize<'de>,
{ {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = T::deserialize(deserializer)?; let value = T::deserialize(deserializer)?;
Ok(Self::new(value)) Ok(Self::new_maybe_sync(value))
} }
} }

View file

@ -199,18 +199,20 @@ pub struct Signal<T: 'static, S: Storage<SignalData<T>> = UnsyncStorage> {
pub type SyncSignal<T> = Signal<T, SyncStorage>; pub type SyncSignal<T> = Signal<T, SyncStorage>;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
impl<T: serde::Serialize + 'static, S: Storage<SignalData<T>>> serde::Serialize for Signal<T, S> { impl<T: serde::Serialize + 'static, Store: Storage<SignalData<T>>> serde::Serialize
for Signal<T, Store>
{
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.read().serialize(serializer) self.read().serialize(serializer)
} }
} }
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
impl<'de, T: serde::Deserialize<'de> + 'static, S: Storage<SignalData<T>>> serde::Deserialize<'de> impl<'de, T: serde::Deserialize<'de> + 'static, Store: Storage<SignalData<T>>>
for Signal<T, S> serde::Deserialize<'de> for Signal<T, Store>
{ {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Ok(Self::new(T::deserialize(deserializer)?)) Ok(Self::new_maybe_sync(T::deserialize(deserializer)?))
} }
} }