sycamore::reactive

Enum MaybeDyn

pub enum MaybeDyn<T>
where T: Into<MaybeDyn<T>> + 'static,
{ Static(T), Signal(ReadSignal<T>), Derived(Rc<dyn Fn() -> MaybeDyn<T>>), }
Expand description

Represents a value that can be either static or dynamic.

This is useful for cases where you want to accept a value that can be either static or dynamic, such as in component props.

A MaybeDyn value can be created from a static value or a closure that returns the value by using the From trait.

§Creating a MaybeDyn

You can create a MaybeDyn from a static value by using the MaybeDyn::Static variant. However, most of the times, you probably want to use the implementation of the From<U> trait for MaybeDyn<T>.

This trait is already implemented globally for signals and closures that return T. However, we cannot provide a blanket implementation for all types T to convert into MaybeDyn<T> because of specialization. Instead, we can only implement it for specific types.

Variants§

§

Static(T)

A static value.

§

Signal(ReadSignal<T>)

A dynamic value backed by a signal.

§

Derived(Rc<dyn Fn() -> MaybeDyn<T>>)

A derived dynamic value.

Implementations§

§

impl<T> MaybeDyn<T>
where T: Into<MaybeDyn<T>> + 'static,

pub fn evaluate(self) -> T
where T: Clone,

Get the value by consuming itself. Unlike get_clone, this method avoids a clone if we are just storing a static value.

pub fn get(&self) -> T
where T: Copy,

Get the value by copying it.

If the type does not implement Copy, consider using get_clone instead.

pub fn get_clone(&self) -> T
where T: Clone,

Get the value by cloning it.

If the type implements Copy, consider using get instead.

pub fn track(&self)

Track the reactive dependencies, if it is dynamic.

pub fn as_static(&self) -> Option<&T>

Tries to get the value statically or returns None if value is dynamic.

Trait Implementations§

§

impl AttributeValue for MaybeDyn<JsValue>

§

fn set_self(self, el: &mut SsrNode, name: Cow<'static, str>)

§

impl AttributeValue for MaybeDyn<Option<Cow<'static, str>>>

§

fn set_self(self, el: &mut SsrNode, name: Cow<'static, str>)

§

impl AttributeValue for MaybeDyn<bool>

§

fn set_self(self, el: &mut SsrNode, name: Cow<'static, str>)

§

impl<T> Clone for MaybeDyn<T>
where T: Clone + Into<MaybeDyn<T>> + 'static,

§

fn clone(&self) -> MaybeDyn<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl From<&'static str> for MaybeDyn<Cow<'static, str>>

§

fn from(val: &'static str) -> MaybeDyn<Cow<'static, str>>

Converts to this type from the input type.
§

impl From<&'static str> for MaybeDyn<JsValue>

§

fn from(val: &'static str) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<&'static str> for MaybeDyn<Option<Cow<'static, str>>>

§

fn from(val: &'static str) -> MaybeDyn<Option<Cow<'static, str>>>

Converts to this type from the input type.
§

impl From<Cow<'static, str>> for MaybeDyn<Cow<'static, str>>

§

fn from(val: Cow<'static, str>) -> MaybeDyn<Cow<'static, str>>

Converts to this type from the input type.
§

impl From<Cow<'static, str>> for MaybeDyn<Option<Cow<'static, str>>>

§

fn from(val: Cow<'static, str>) -> MaybeDyn<Option<Cow<'static, str>>>

Converts to this type from the input type.
§

impl<F, U, T> From<F> for MaybeDyn<T>
where T: Into<MaybeDyn<T>>, F: Fn() -> U + 'static, U: Into<MaybeDyn<T>>,

§

fn from(f: F) -> MaybeDyn<T>

Converts to this type from the input type.
§

impl From<JsValue> for MaybeDyn<JsValue>

§

fn from(val: JsValue) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<Option<&'static str>> for MaybeDyn<Option<Cow<'static, str>>>

§

fn from(val: Option<&'static str>) -> MaybeDyn<Option<Cow<'static, str>>>

Converts to this type from the input type.
§

impl From<Option<String>> for MaybeDyn<Option<Cow<'static, str>>>

§

fn from(val: Option<String>) -> MaybeDyn<Option<Cow<'static, str>>>

Converts to this type from the input type.
§

impl<T> From<Option<T>> for MaybeDyn<Option<T>>

§

fn from(val: Option<T>) -> MaybeDyn<Option<T>>

Converts to this type from the input type.
§

impl<T, U> From<ReadSignal<U>> for MaybeDyn<T>
where T: Into<MaybeDyn<T>>, U: Into<MaybeDyn<T>> + Clone,

§

fn from(val: ReadSignal<U>) -> MaybeDyn<T>

Converts to this type from the input type.
§

impl<T, U> From<Signal<U>> for MaybeDyn<T>
where T: Into<MaybeDyn<T>>, U: Into<MaybeDyn<T>> + Clone,

§

fn from(val: Signal<U>) -> MaybeDyn<T>

Converts to this type from the input type.
§

impl From<String> for MaybeDyn<Cow<'static, str>>

§

fn from(val: String) -> MaybeDyn<Cow<'static, str>>

Converts to this type from the input type.
§

impl From<String> for MaybeDyn<JsValue>

§

fn from(val: String) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<String> for MaybeDyn<Option<Cow<'static, str>>>

§

fn from(val: String) -> MaybeDyn<Option<Cow<'static, str>>>

Converts to this type from the input type.
§

impl<T> From<Vec<T>> for MaybeDyn<Vec<T>>

§

fn from(val: Vec<T>) -> MaybeDyn<Vec<T>>

Converts to this type from the input type.
§

impl From<bool> for MaybeDyn<JsValue>

§

fn from(val: bool) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<bool> for MaybeDyn<bool>

§

fn from(val: bool) -> MaybeDyn<bool>

Converts to this type from the input type.
§

impl From<f32> for MaybeDyn<JsValue>

§

fn from(val: f32) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<f32> for MaybeDyn<f32>

§

fn from(val: f32) -> MaybeDyn<f32>

Converts to this type from the input type.
§

impl From<f64> for MaybeDyn<JsValue>

§

fn from(val: f64) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<f64> for MaybeDyn<f64>

§

fn from(val: f64) -> MaybeDyn<f64>

Converts to this type from the input type.
§

impl From<i128> for MaybeDyn<JsValue>

§

fn from(val: i128) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<i128> for MaybeDyn<i128>

§

fn from(val: i128) -> MaybeDyn<i128>

Converts to this type from the input type.
§

impl From<i16> for MaybeDyn<JsValue>

§

fn from(val: i16) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<i16> for MaybeDyn<i16>

§

fn from(val: i16) -> MaybeDyn<i16>

Converts to this type from the input type.
§

impl From<i32> for MaybeDyn<JsValue>

§

fn from(val: i32) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<i32> for MaybeDyn<i32>

§

fn from(val: i32) -> MaybeDyn<i32>

Converts to this type from the input type.
§

impl From<i64> for MaybeDyn<JsValue>

§

fn from(val: i64) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<i64> for MaybeDyn<i64>

§

fn from(val: i64) -> MaybeDyn<i64>

Converts to this type from the input type.
§

impl From<i8> for MaybeDyn<JsValue>

§

fn from(val: i8) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<i8> for MaybeDyn<i8>

§

fn from(val: i8) -> MaybeDyn<i8>

Converts to this type from the input type.
§

impl From<isize> for MaybeDyn<JsValue>

§

fn from(val: isize) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<isize> for MaybeDyn<isize>

§

fn from(val: isize) -> MaybeDyn<isize>

Converts to this type from the input type.
§

impl From<u128> for MaybeDyn<JsValue>

§

fn from(val: u128) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<u128> for MaybeDyn<u128>

§

fn from(val: u128) -> MaybeDyn<u128>

Converts to this type from the input type.
§

impl From<u16> for MaybeDyn<JsValue>

§

fn from(val: u16) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<u16> for MaybeDyn<u16>

§

fn from(val: u16) -> MaybeDyn<u16>

Converts to this type from the input type.
§

impl From<u32> for MaybeDyn<JsValue>

§

fn from(val: u32) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<u32> for MaybeDyn<u32>

§

fn from(val: u32) -> MaybeDyn<u32>

Converts to this type from the input type.
§

impl From<u64> for MaybeDyn<JsValue>

§

fn from(val: u64) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<u64> for MaybeDyn<u64>

§

fn from(val: u64) -> MaybeDyn<u64>

Converts to this type from the input type.
§

impl From<u8> for MaybeDyn<JsValue>

§

fn from(val: u8) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<u8> for MaybeDyn<u8>

§

fn from(val: u8) -> MaybeDyn<u8>

Converts to this type from the input type.
§

impl From<usize> for MaybeDyn<JsValue>

§

fn from(val: usize) -> MaybeDyn<JsValue>

Converts to this type from the input type.
§

impl From<usize> for MaybeDyn<usize>

§

fn from(val: usize) -> MaybeDyn<usize>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> Freeze for MaybeDyn<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for MaybeDyn<T>

§

impl<T> !Send for MaybeDyn<T>

§

impl<T> !Sync for MaybeDyn<T>

§

impl<T> Unpin for MaybeDyn<T>
where T: Unpin,

§

impl<T> !UnwindSafe for MaybeDyn<T>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.