Skip to content

Commit

Permalink
Make MaybeProp Copy
Browse files Browse the repository at this point in the history
  • Loading branch information
zakstucke committed Oct 22, 2024
1 parent f420d84 commit 107ff9f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 48 deletions.
8 changes: 4 additions & 4 deletions reactive_graph/src/nightly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
},
traits::{Get, Set},
wrappers::{
read::{ArcSignal, Signal},
read::{ArcSignal, Signal, SignalTypes},
write::SignalSetter,
},
};
Expand Down Expand Up @@ -115,7 +115,7 @@ macro_rules! impl_get_fn_traits_get_arena {
$(
#[cfg(feature = "nightly")]
#[allow(deprecated)]
impl<T, S> FnOnce<()> for $ty<T, S> where $ty<T, S>: Get, S: Storage<T> + Storage<Option<T>> {
impl<T, S> FnOnce<()> for $ty<T, S> where $ty<T, S>: Get, S: Storage<T> + Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>> {
type Output = <Self as Get>::Value;

#[inline(always)]
Expand All @@ -126,7 +126,7 @@ macro_rules! impl_get_fn_traits_get_arena {

#[cfg(feature = "nightly")]
#[allow(deprecated)]
impl<T, S> FnMut<()> for $ty<T, S> where $ty<T, S>: Get, S: Storage<T> + Storage<Option<T>> {
impl<T, S> FnMut<()> for $ty<T, S> where $ty<T, S>: Get, S: Storage<T> + Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>> {
#[inline(always)]
extern "rust-call" fn call_mut(&mut self, _args: ()) -> Self::Output {
self.get()
Expand All @@ -135,7 +135,7 @@ macro_rules! impl_get_fn_traits_get_arena {

#[cfg(feature = "nightly")]
#[allow(deprecated)]
impl<T, S> Fn<()> for $ty<T, S> where $ty<T, S>: Get, S: Storage<T> + Storage<Option<T>> {
impl<T, S> Fn<()> for $ty<T, S> where $ty<T, S>: Get, S: Storage<T> + Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>> {
#[inline(always)]
extern "rust-call" fn call(&self, _args: ()) -> Self::Output {
self.get()
Expand Down
11 changes: 2 additions & 9 deletions reactive_graph/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,8 @@ where
S: serde::Serializer,
{
match &self.0 {
None | Some(MaybeSignal::Static(None)) => {
None::<T>.serialize(serializer)
}
Some(MaybeSignal::Static(Some(value))) => {
value.serialize(serializer)
}
Some(MaybeSignal::Dynamic(signal)) => {
signal.with(|value| value.serialize(serializer))
}
None => None::<T>.serialize(serializer),
Some(signal) => signal.with(|value| value.serialize(serializer)),
}
}
}
Expand Down
80 changes: 45 additions & 35 deletions reactive_graph/src/wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ pub mod read {
{
#[track_caller]
fn from(value: MaybeProp<T>) -> Self {
value.0.map(|mb| mb.into())
value.0
}
}

Expand All @@ -1047,7 +1047,7 @@ pub mod read {
{
#[track_caller]
fn from(value: MaybeProp<T, LocalStorage>) -> Self {
value.0.map(|mb| mb.into())
value.0
}
}

Expand Down Expand Up @@ -1374,28 +1374,29 @@ pub mod read {
#[derive(Debug, PartialEq, Eq)]
#[allow(deprecated)]
pub struct MaybeProp<T: 'static, S = SyncStorage>(
pub(crate) Option<MaybeSignal<Option<T>, S>>,
pub(crate) Option<Signal<Option<T>, S>>,
)
where
S: Storage<Option<T>>;
S: Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>>;

#[allow(deprecated)]
impl<T: Clone, S> Clone for MaybeProp<T, S>
impl<T, S> Clone for MaybeProp<T, S>
where
S: Storage<Option<T>>,
S: Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>>,
{
fn clone(&self) -> Self {
Self(self.0.clone())
*self
}
}

#[allow(deprecated)]
impl<T: Copy, S> Copy for MaybeProp<T, S> where S: Storage<Option<T>> {}
impl<T, S> Copy for MaybeProp<T, S> where
S: Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>>
{
}

#[allow(deprecated)]
impl<T, S> Default for MaybeProp<T, S>
where
S: Storage<Option<T>>,
S: Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>>,
{
fn default() -> Self {
Self(None)
Expand All @@ -1405,7 +1406,7 @@ pub mod read {
#[allow(deprecated)]
impl<T, S> DefinedAt for MaybeProp<T, S>
where
S: Storage<Option<T>>,
S: Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>>,
{
fn defined_at(&self) -> Option<&'static Location<'static>> {
// TODO this can be improved by adding a defined_at field
Expand All @@ -1430,7 +1431,7 @@ pub mod read {
impl<T, S> ReadUntracked for MaybeProp<T, S>
where
T: Clone,
S: Storage<SignalTypes<Option<T>, S>> + Storage<Option<T>>,
S: Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>>,
{
type Value = ReadGuard<Option<T>, SignalReadGuard<Option<T>, S>>;

Expand Down Expand Up @@ -1459,47 +1460,51 @@ pub mod read {
pub fn derive(
derived_signal: impl Fn() -> Option<T> + Send + Sync + 'static,
) -> Self {
Self(Some(MaybeSignal::derive(derived_signal)))
Self(Some(Signal::derive(derived_signal)))
}
}

#[allow(deprecated)]
impl<T> From<T> for MaybeProp<T>
where
T: Send + Sync,
SyncStorage: Storage<Option<T>>,
{
fn from(value: T) -> Self {
Self(Some(MaybeSignal::from(Some(value))))
Self(Some(Signal::stored(Some(value))))
}
}

#[allow(deprecated)]
impl<T> From<Option<T>> for MaybeProp<T>
where
T: Send + Sync,
SyncStorage: Storage<Option<T>>,
{
fn from(value: Option<T>) -> Self {
Self(Some(MaybeSignal::from(value)))
Self(Some(Signal::stored(value)))
}
}

#[allow(deprecated)]
impl<T> From<MaybeSignal<Option<T>>> for MaybeProp<T>
where
T: Send + Sync,
SyncStorage: Storage<Option<T>>,
{
fn from(value: MaybeSignal<Option<T>>) -> Self {
Self(Some(value))
Self(Some(value.into()))
}
}

#[allow(deprecated)]
impl<T> From<Option<MaybeSignal<Option<T>>>> for MaybeProp<T>
where
T: Send + Sync,
SyncStorage: Storage<Option<T>>,
{
fn from(value: Option<MaybeSignal<Option<T>>>) -> Self {
Self(value)
Self(value.map(Into::into))
}
}

Expand Down Expand Up @@ -1536,10 +1541,11 @@ pub mod read {
#[allow(deprecated)]
impl<T> From<Signal<Option<T>>> for MaybeProp<T>
where
T: Send + Sync,
SyncStorage: Storage<Option<T>>,
{
fn from(value: Signal<Option<T>>) -> Self {
Self(Some(value.into()))
Self(Some(value))
}
}

Expand All @@ -1549,7 +1555,7 @@ pub mod read {
T: Send + Sync + Clone,
{
fn from(value: ReadSignal<T>) -> Self {
Self(Some(MaybeSignal::derive(move || Some(value.get()))))
Self(Some(Signal::derive(move || Some(value.get()))))
}
}

Expand All @@ -1559,7 +1565,7 @@ pub mod read {
T: Send + Sync + Clone,
{
fn from(value: RwSignal<T>) -> Self {
Self(Some(MaybeSignal::derive(move || Some(value.get()))))
Self(Some(Signal::derive(move || Some(value.get()))))
}
}

Expand All @@ -1569,7 +1575,7 @@ pub mod read {
T: Send + Sync + Clone,
{
fn from(value: Memo<T>) -> Self {
Self(Some(MaybeSignal::derive(move || Some(value.get()))))
Self(Some(Signal::derive(move || Some(value.get()))))
}
}

Expand All @@ -1579,14 +1585,14 @@ pub mod read {
T: Send + Sync + Clone,
{
fn from(value: Signal<T>) -> Self {
Self(Some(MaybeSignal::derive(move || Some(value.get()))))
Self(Some(Signal::derive(move || Some(value.get()))))
}
}

#[allow(deprecated)]
impl From<&str> for MaybeProp<String> {
fn from(value: &str) -> Self {
Self(Some(MaybeSignal::from(Some(value.to_string()))))
Self(Some(Signal::from(Some(value.to_string()))))
}
}

Expand All @@ -1597,39 +1603,43 @@ pub mod read {
pub fn derive_local(
derived_signal: impl Fn() -> Option<T> + 'static,
) -> Self {
Self(Some(MaybeSignal::derive_local(derived_signal)))
Self(Some(Signal::derive_local(derived_signal)))
}
}

#[allow(deprecated)]
impl<T> FromLocal<T> for MaybeProp<T, LocalStorage> {
fn from_local(value: T) -> Self {
Self(Some(MaybeSignal::from_local(Some(value))))
Self(Some(Signal::stored_local(Some(value))))
}
}

#[allow(deprecated)]
impl<T> FromLocal<Option<T>> for MaybeProp<T, LocalStorage> {
fn from_local(value: Option<T>) -> Self {
Self(Some(MaybeSignal::from_local(value)))
Self(Some(Signal::stored_local(value)))
}
}

#[allow(deprecated)]
impl<T> From<MaybeSignal<Option<T>, LocalStorage>>
for MaybeProp<T, LocalStorage>
where
T: Send + Sync,
{
fn from(value: MaybeSignal<Option<T>, LocalStorage>) -> Self {
Self(Some(value))
Self(Some(value.into()))
}
}

#[allow(deprecated)]
impl<T> From<Option<MaybeSignal<Option<T>, LocalStorage>>>
for MaybeProp<T, LocalStorage>
where
T: Send + Sync,
{
fn from(value: Option<MaybeSignal<Option<T>, LocalStorage>>) -> Self {
Self(value)
Self(value.map(Into::into))
}
}

Expand Down Expand Up @@ -1666,7 +1676,7 @@ pub mod read {
#[allow(deprecated)]
impl<T> From<Signal<Option<T>, LocalStorage>> for MaybeProp<T, LocalStorage> {
fn from(value: Signal<Option<T>, LocalStorage>) -> Self {
Self(Some(value.into()))
Self(Some(value))
}
}

Expand All @@ -1676,7 +1686,7 @@ pub mod read {
T: Send + Sync + Clone,
{
fn from(value: ReadSignal<T, LocalStorage>) -> Self {
Self(Some(MaybeSignal::derive_local(move || Some(value.get()))))
Self(Some(Signal::derive_local(move || Some(value.get()))))
}
}

Expand All @@ -1686,7 +1696,7 @@ pub mod read {
T: Send + Sync + Clone,
{
fn from(value: RwSignal<T, LocalStorage>) -> Self {
Self(Some(MaybeSignal::derive_local(move || Some(value.get()))))
Self(Some(Signal::derive_local(move || Some(value.get()))))
}
}

Expand All @@ -1696,7 +1706,7 @@ pub mod read {
T: Send + Sync + Clone,
{
fn from(value: Memo<T, LocalStorage>) -> Self {
Self(Some(MaybeSignal::derive_local(move || Some(value.get()))))
Self(Some(Signal::derive_local(move || Some(value.get()))))
}
}

Expand All @@ -1706,14 +1716,14 @@ pub mod read {
T: Send + Sync + Clone,
{
fn from(value: Signal<T, LocalStorage>) -> Self {
Self(Some(MaybeSignal::derive_local(move || Some(value.get()))))
Self(Some(Signal::derive_local(move || Some(value.get()))))
}
}

#[allow(deprecated)]
impl From<&str> for MaybeProp<String, LocalStorage> {
fn from(value: &str) -> Self {
Self(Some(MaybeSignal::from_local(Some(value.to_string()))))
Self(Some(Signal::stored_local(Some(value.to_string()))))
}
}

Expand Down

0 comments on commit 107ff9f

Please sign in to comment.