pub struct MaybeProp<T>(/* private fields */)
where
T: 'static;
Expand description
A wrapping type for an optional component prop, which can either be a signal or a
non-reactive value, and which may or may not have a value. In other words, this is
an Option<MaybeSignal<Option<T>>>
that automatically flattens its getters.
This creates an extremely flexible type for component libraries, etc.
§Core Trait Implementations
.get()
(or calling the signal as a function) clones the current value of the signal. If you call it within an effect, it will cause that effect to subscribe to the signal, and to re-run whenever the value of the signal changes..get_untracked()
clones the value of the signal without reactively tracking it.
.with()
allows you to reactively access the signal’s value without cloning by applying a callback function..with_untracked()
allows you to access the signal’s value without reactively tracking it.
.to_stream()
converts the signal to anasync
stream of values.
§Examples
let (count, set_count) = create_signal(Some(2));
let double = |n| n * 2;
let double_count = MaybeProp::derive(move || count.get().map(double));
let memoized_double_count = create_memo(move |_| count.get().map(double));
let static_value = 5;
// this function takes either a reactive or non-reactive value
fn above_3(arg: &MaybeProp<i32>) -> bool {
// ✅ calling the signal clones and returns the value
// it is a shorthand for arg.get()q
arg.get().map(|arg| arg > 3).unwrap_or(false)
}
assert_eq!(above_3(&None::<i32>.into()), false);
assert_eq!(above_3(&static_value.into()), true);
assert_eq!(above_3(&count.into()), false);
assert_eq!(above_3(&double_count), true);
assert_eq!(above_3(&memoized_double_count.into()), true);
Implementations§
source§impl<T> MaybeProp<T>
impl<T> MaybeProp<T>
§Examples
let (name, set_name) = create_signal("Alice".to_string());
let (maybe_name, set_maybe_name) = create_signal(None);
let name_upper =
MaybeProp::derive(move || Some(name.with(|n| n.to_uppercase())));
let memoized_lower = create_memo(move |_| name.with(|n| n.to_lowercase()));
let static_value: MaybeProp<String> = "Bob".to_string().into();
// this function takes any kind of wrapped signal
fn current_len_inefficient(arg: &MaybeProp<String>) -> usize {
// ❌ unnecessarily clones the string
arg.get().map(|n| n.len()).unwrap_or(0)
}
fn current_len(arg: &MaybeProp<String>) -> usize {
// ✅ gets the length without cloning the `String`
arg.with(|value| value.len()).unwrap_or(0)
}
assert_eq!(current_len(&None::<String>.into()), 0);
assert_eq!(current_len(&maybe_name.into()), 0);
assert_eq!(current_len(&name_upper), 5);
assert_eq!(current_len(&memoized_lower.into()), 5);
assert_eq!(current_len(&static_value), 3);
// Normal signals/memos return T
assert_eq!(name.get(), "Alice".to_string());
assert_eq!(memoized_lower.get(), "alice".to_string());
// MaybeProp::get() returns Option<T>
assert_eq!(name_upper.get(), Some("ALICE".to_string()));
assert_eq!(static_value.get(), Some("Bob".to_string()));
sourcepub fn with<O>(&self, f: impl FnOnce(&T) -> O) -> Option<O>
pub fn with<O>(&self, f: impl FnOnce(&T) -> O) -> Option<O>
Applies a function to the current value, returning the result.
sourcepub fn try_with<O>(&self, f: impl FnOnce(&T) -> O) -> Option<O>
pub fn try_with<O>(&self, f: impl FnOnce(&T) -> O) -> Option<O>
Applies a function to the current value, returning the result. Returns None
if the value has already been disposed.
sourcepub fn with_untracked<O>(&self, f: impl FnOnce(&T) -> O) -> Option<O>
pub fn with_untracked<O>(&self, f: impl FnOnce(&T) -> O) -> Option<O>
Applies a function to the current value, returning the result, without causing the current reactive scope to track changes.
sourcepub fn try_with_untracked<O>(&self, f: impl FnOnce(&T) -> O) -> Option<O>
pub fn try_with_untracked<O>(&self, f: impl FnOnce(&T) -> O) -> Option<O>
Applies a function to the current value, returning the result, without
causing the current reactive scope to track changes. Returns None
if
the value has already been disposed.
source§impl<T> MaybeProp<T>where
T: 'static,
impl<T> MaybeProp<T>where
T: 'static,
sourcepub fn derive(derived_signal: impl Fn() -> Option<T> + 'static) -> MaybeProp<T>
pub fn derive(derived_signal: impl Fn() -> Option<T> + 'static) -> MaybeProp<T>
Wraps a derived signal, i.e., any computation that accesses one or more reactive signals.
let (count, set_count) = create_signal(2);
let double_count = MaybeProp::derive(move || Some(count.get() * 2));
// this function takes any kind of wrapped signal
fn above_3(arg: &MaybeProp<i32>) -> bool {
arg.get().unwrap_or(0) > 3
}
assert_eq!(above_3(&count.into()), false);
assert_eq!(above_3(&double_count.into()), true);
assert_eq!(above_3(&2.into()), false);
Trait Implementations§
source§impl<T> From<ReadSignal<T>> for MaybeProp<T>where
T: Clone,
impl<T> From<ReadSignal<T>> for MaybeProp<T>where
T: Clone,
source§fn from(value: ReadSignal<T>) -> MaybeProp<T>
fn from(value: ReadSignal<T>) -> MaybeProp<T>
source§impl<T> IntoAttribute for MaybeProp<T>where
T: IntoAttribute + Clone,
impl<T> IntoAttribute for MaybeProp<T>where
T: IntoAttribute + Clone,
source§impl<T> IntoProperty for MaybeProp<T>
impl<T> IntoProperty for MaybeProp<T>
source§impl<T> PartialEq for MaybeProp<T>where
T: PartialEq + 'static,
impl<T> PartialEq for MaybeProp<T>where
T: PartialEq + 'static,
source§impl<T> Serialize for MaybeProp<T>where
T: Serialize,
impl<T> Serialize for MaybeProp<T>where
T: Serialize,
source§fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
source§impl<T> SignalGet for MaybeProp<T>where
T: Clone,
impl<T> SignalGet for MaybeProp<T>where
T: Clone,
§Examples
let (count, set_count) = create_signal(Some(2));
let double = |n| n * 2;
let double_count = MaybeProp::derive(move || count.get().map(double));
let memoized_double_count = create_memo(move |_| count.get().map(double));
let static_value = 5;
// this function takes either a reactive or non-reactive value
fn above_3(arg: &MaybeProp<i32>) -> bool {
// ✅ calling the signal clones and returns the value
// it is a shorthand for arg.get()q
arg.get().map(|arg| arg > 3).unwrap_or(false)
}
assert_eq!(above_3(&None::<i32>.into()), false);
assert_eq!(above_3(&static_value.into()), true);
assert_eq!(above_3(&count.into()), false);
assert_eq!(above_3(&double_count), true);
assert_eq!(above_3(&memoized_double_count.into()), true);
source§impl<T> SignalGetUntracked for MaybeProp<T>where
T: Clone,
impl<T> SignalGetUntracked for MaybeProp<T>where
T: Clone,
source§fn get_untracked(&self) -> Option<T>
fn get_untracked(&self) -> Option<T>
impl<T> Copy for MaybeProp<T>where
T: Copy,
impl<T> Eq for MaybeProp<T>where
T: Eq + 'static,
impl<T> StructuralPartialEq for MaybeProp<T>where
T: 'static,
Auto Trait Implementations§
impl<T> Freeze for MaybeProp<T>where
T: Freeze,
impl<T> !RefUnwindSafe for MaybeProp<T>
impl<T> !Send for MaybeProp<T>
impl<T> !Sync for MaybeProp<T>
impl<T> Unpin for MaybeProp<T>where
T: Unpin,
impl<T> !UnwindSafe for MaybeProp<T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<CustErr, T, Request> FromReq<Streaming, Request, CustErr> for T
impl<CustErr, T, Request> FromReq<Streaming, Request, CustErr> for T
source§async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
source§impl<CustErr, T, Request> FromReq<StreamingText, Request, CustErr> for T
impl<CustErr, T, Request> FromReq<StreamingText, Request, CustErr> for T
source§async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more