Enum MaybeDyn

Source
pub enum MaybeDyn<T>
where T: Into<Self> + 'static,
{ Static(T), Signal(ReadSignal<T>), Derived(Rc<dyn Fn() -> Self>), }
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() -> Self>)

A derived dynamic value.

Implementations§

Source§

impl<T: Into<Self> + 'static> MaybeDyn<T>

Source

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.

Source

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.

Source

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

Get the value by cloning it.

If the type implements Copy, consider using get instead.

Source

pub fn track(&self)

Track the reactive dependencies, if it is dynamic.

Source

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

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

Trait Implementations§

Source§

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

Source§

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
Source§

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

Source§

fn from(val: &'static str) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(val: &'static str) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(val: &'static str) -> Self

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

fn from(f: F) -> Self

Converts to this type from the input type.
Source§

impl From<JsValue> for MaybeDyn<JsValue>

Source§

fn from(val: JsValue) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(val: Option<&'static str>) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(val: Option<String>) -> Self

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

fn from(val: ReadSignal<U>) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(val: Signal<U>) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(val: String) -> Self

Converts to this type from the input type.
Source§

impl From<String> for MaybeDyn<JsValue>

Source§

fn from(val: String) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(val: String) -> Self

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

impl From<bool> for MaybeDyn<JsValue>

Source§

fn from(val: bool) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for MaybeDyn<bool>

Source§

fn from(val: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for MaybeDyn<JsValue>

Source§

fn from(val: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for MaybeDyn<f32>

Source§

fn from(val: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for MaybeDyn<JsValue>

Source§

fn from(val: f64) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for MaybeDyn<f64>

Source§

fn from(val: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i128> for MaybeDyn<JsValue>

Source§

fn from(val: i128) -> Self

Converts to this type from the input type.
Source§

impl From<i128> for MaybeDyn<i128>

Source§

fn from(val: i128) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for MaybeDyn<JsValue>

Source§

fn from(val: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for MaybeDyn<i16>

Source§

fn from(val: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for MaybeDyn<JsValue>

Source§

fn from(val: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for MaybeDyn<i32>

Source§

fn from(val: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for MaybeDyn<JsValue>

Source§

fn from(val: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for MaybeDyn<i64>

Source§

fn from(val: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for MaybeDyn<JsValue>

Source§

fn from(val: i8) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for MaybeDyn<i8>

Source§

fn from(val: i8) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for MaybeDyn<JsValue>

Source§

fn from(val: isize) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for MaybeDyn<isize>

Source§

fn from(val: isize) -> Self

Converts to this type from the input type.
Source§

impl From<u128> for MaybeDyn<JsValue>

Source§

fn from(val: u128) -> Self

Converts to this type from the input type.
Source§

impl From<u128> for MaybeDyn<u128>

Source§

fn from(val: u128) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for MaybeDyn<JsValue>

Source§

fn from(val: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for MaybeDyn<u16>

Source§

fn from(val: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for MaybeDyn<JsValue>

Source§

fn from(val: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for MaybeDyn<u32>

Source§

fn from(val: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for MaybeDyn<JsValue>

Source§

fn from(val: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for MaybeDyn<u64>

Source§

fn from(val: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for MaybeDyn<JsValue>

Source§

fn from(val: u8) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for MaybeDyn<u8>

Source§

fn from(val: u8) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for MaybeDyn<JsValue>

Source§

fn from(val: usize) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for MaybeDyn<usize>

Source§

fn from(val: usize) -> Self

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 u8)

🔬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.