Enum leptos::MaybeSignal

source ·
pub enum MaybeSignal<T>
where T: 'static,
{ Static(T), Dynamic(Signal<T>), }
Expand description

A wrapper for a value that is either T or Signal<T>.

This allows you to create APIs that take either a reactive or a non-reactive value of the same type. This is especially useful for component properties.

§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 an async stream of values.

§Examples

let (count, set_count) = create_signal(2);
let double_count = MaybeSignal::derive(move || count.get() * 2);
let memoized_double_count = create_memo(move |_| count.get() * 2);
let static_value = 5;

// this function takes either a reactive or non-reactive value
fn above_3(arg: &MaybeSignal<i32>) -> bool {
    // ✅ calling the signal clones and returns the value
    //    it is a shorthand for arg.get()
    arg.get() > 3
}

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

Variants§

§

Static(T)

An unchanging value of type T.

§

Dynamic(Signal<T>)

A reactive signal that contains a value of type T.

Implementations§

source§

impl<T> MaybeSignal<T>
where T: 'static,

source

pub fn derive(derived_signal: impl Fn() -> T + 'static) -> MaybeSignal<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 = Signal::derive(move || count.get() * 2);

// this function takes any kind of wrapped signal
fn above_3(arg: &MaybeSignal<i32>) -> bool {
    arg.get() > 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> Clone for MaybeSignal<T>
where T: Clone,

source§

fn clone(&self) -> MaybeSignal<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<T> Debug for MaybeSignal<T>
where T: Debug + 'static,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<T> Default for MaybeSignal<T>
where T: Default,

source§

fn default() -> MaybeSignal<T>

Returns the “default value” for a type. Read more
source§

impl<'de, T> Deserialize<'de> for MaybeSignal<T>
where T: Deserialize<'de>,

source§

fn deserialize<D>( deserializer: D ) -> Result<MaybeSignal<T>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl From<&str> for MaybeSignal<String>

source§

fn from(value: &str) -> MaybeSignal<String>

Converts to this type from the input type.
source§

impl<T> From<MaybeSignal<Option<T>>> for MaybeProp<T>

source§

fn from(value: MaybeSignal<Option<T>>) -> MaybeProp<T>

Converts to this type from the input type.
source§

impl<T> From<Memo<T>> for MaybeSignal<T>

source§

fn from(value: Memo<T>) -> MaybeSignal<T>

Converts to this type from the input type.
source§

impl<T> From<ReadSignal<T>> for MaybeSignal<T>

source§

fn from(value: ReadSignal<T>) -> MaybeSignal<T>

Converts to this type from the input type.
source§

impl<T> From<RwSignal<T>> for MaybeSignal<T>

source§

fn from(value: RwSignal<T>) -> MaybeSignal<T>

Converts to this type from the input type.
source§

impl<T> From<Signal<T>> for MaybeSignal<T>

source§

fn from(value: Signal<T>) -> MaybeSignal<T>

Converts to this type from the input type.
source§

impl<T> From<T> for MaybeSignal<T>

source§

fn from(value: T) -> MaybeSignal<T>

Converts to this type from the input type.
source§

impl<T> IntoAttribute for MaybeSignal<T>
where T: IntoAttribute + Clone,

source§

fn into_attribute(self) -> Attribute

Converts the object into an Attribute.
source§

fn into_attribute_boxed(self: Box<MaybeSignal<T>>) -> Attribute

Helper function for dealing with Box<dyn IntoAttribute>.
source§

impl IntoClass for MaybeSignal<bool>

source§

fn into_class(self) -> Class

Converts the object into a Class.
source§

fn into_class_boxed(self: Box<MaybeSignal<bool>>) -> Class

Helper function for dealing with Box<dyn IntoClass>.
source§

impl<T> IntoProperty for MaybeSignal<T>
where T: Into<JsValue> + Clone,

source§

fn into_property(self) -> Property

Converts the object into a Property.
source§

fn into_property_boxed(self: Box<MaybeSignal<T>>) -> Property

Helper function for dealing with Box<dyn IntoProperty>.
source§

impl<T> IntoStyle for MaybeSignal<T>
where T: IntoStyle + Clone,

source§

fn into_style(self) -> Style

Converts the object into a Style.
source§

fn into_style_boxed(self: Box<MaybeSignal<T>>) -> Style

Helper function for dealing with Box<dyn IntoStyle>.
source§

impl<T> IntoView for MaybeSignal<T>
where T: IntoView + Clone,

source§

fn into_view(self) -> View

Converts the value into View.
source§

impl<T> PartialEq for MaybeSignal<T>
where T: PartialEq + 'static,

source§

fn eq(&self, other: &MaybeSignal<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> Serialize for MaybeSignal<T>
where T: Serialize,

source§

fn serialize<S>( &self, serializer: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<T> SignalGet for MaybeSignal<T>
where T: Clone,

§Examples

let (count, set_count) = create_signal(2);
let double_count = MaybeSignal::derive(move || count.get() * 2);
let memoized_double_count = create_memo(move |_| count.get() * 2);
let static_value: MaybeSignal<i32> = 5.into();

// this function takes any kind of wrapped signal
fn above_3(arg: &MaybeSignal<i32>) -> bool {
    arg.get() > 3
}

assert_eq!(above_3(&count.into()), false);
assert_eq!(above_3(&double_count), true);
assert_eq!(above_3(&memoized_double_count.into()), true);
assert_eq!(above_3(&static_value.into()), true);
§

type Value = T

The value held by the signal.
source§

fn get(&self) -> T

Clones and returns the current value of the signal, and subscribes the running effect to this signal. Read more
source§

fn try_get(&self) -> Option<T>

Clones and returns the signal value, returning Some if the signal is still alive, and None otherwise.
source§

impl<T> SignalGetUntracked for MaybeSignal<T>
where T: Clone,

§

type Value = T

The value held by the signal.
source§

fn get_untracked(&self) -> T

Gets the signal’s value without creating a dependency on the current scope. Read more
source§

fn try_get_untracked(&self) -> Option<T>

Gets the signal’s value without creating a dependency on the current scope. Returns [Some(T)] if the signal is still valid, None otherwise.
source§

impl<T> SignalStream<T> for MaybeSignal<T>
where T: Clone,

source§

fn to_stream(&self) -> Pin<Box<dyn Stream<Item = T>>>

Generates a [Stream] that emits the new value of the signal whenever it changes. Read more
source§

impl<T> SignalWith for MaybeSignal<T>

§Examples

let (name, set_name) = create_signal("Alice".to_string());
let name_upper =
    MaybeSignal::derive(move || name.with(|n| n.to_uppercase()));
let memoized_lower = create_memo(move |_| name.with(|n| n.to_lowercase()));
let static_value: MaybeSignal<String> = "Bob".to_string().into();

// this function takes any kind of wrapped signal
fn current_len_inefficient(arg: &MaybeSignal<String>) -> usize {
    // ❌ unnecessarily clones the string
    arg.get().len()
}

fn current_len(arg: &MaybeSignal<String>) -> usize {
    // ✅ gets the length without cloning the `String`
    arg.with(|value| value.len())
}

assert_eq!(current_len(&name.into()), 5);
assert_eq!(current_len(&name_upper), 5);
assert_eq!(current_len(&memoized_lower.into()), 5);
assert_eq!(current_len(&static_value), 3);

assert_eq!(name.get(), "Alice");
assert_eq!(name_upper.get(), "ALICE");
assert_eq!(memoized_lower.get(), "alice");
assert_eq!(static_value.get(), "Bob");
§

type Value = T

The value held by the signal.
source§

fn with<O>(&self, f: impl FnOnce(&T) -> O) -> O

Applies a function to the current value of the signal, and subscribes the running effect to this signal. Read more
source§

fn try_with<O>(&self, f: impl FnOnce(&T) -> O) -> Option<O>

Applies a function to the current value of the signal, and subscribes the running effect to this signal. Returns Some if the signal is valid and the function ran, otherwise returns None.
source§

fn track(&self)

Subscribes to this signal in the current reactive scope without doing anything with its value.
source§

impl<T> SignalWithUntracked for MaybeSignal<T>

§

type Value = T

The value held by the signal.
source§

fn with_untracked<O>(&self, f: impl FnOnce(&T) -> O) -> O

Runs the provided closure with a reference to the current value without creating a dependency on the current scope. Read more
source§

fn try_with_untracked<O>(&self, f: impl FnOnce(&T) -> O) -> Option<O>

Runs the provided closure with a reference to the current value without creating a dependency on the current scope. Returns [Some(O)] if the signal is still valid, None otherwise.
source§

impl<T> Copy for MaybeSignal<T>
where T: Copy,

source§

impl<T> Eq for MaybeSignal<T>
where T: Eq + 'static,

source§

impl<T> StructuralPartialEq for MaybeSignal<T>
where T: 'static,

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for MaybeSignal<T>

§

impl<T> !Send for MaybeSignal<T>

§

impl<T> !Sync for MaybeSignal<T>

§

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

§

impl<T> !UnwindSafe for MaybeSignal<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
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<CustErr, T, Request> FromReq<Cbor, Request, CustErr> for T
where Request: Req<CustErr> + Send + 'static, T: DeserializeOwned,

source§

async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>

Attempts to deserialize the arguments from a request.
source§

impl<CustErr, T, Request> FromReq<GetUrl, Request, CustErr> for T
where Request: Req<CustErr> + Send + 'static, T: DeserializeOwned,

source§

async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>

Attempts to deserialize the arguments from a request.
source§

impl<CustErr, T, Request> FromReq<Json, Request, CustErr> for T
where Request: Req<CustErr> + Send + 'static, T: DeserializeOwned,

source§

async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>

Attempts to deserialize the arguments from a request.
source§

impl<CustErr, T, Request> FromReq<PostUrl, Request, CustErr> for T
where Request: Req<CustErr> + Send + 'static, T: DeserializeOwned,

source§

async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>

Attempts to deserialize the arguments from a request.
source§

impl<CustErr, T, Request> FromReq<Streaming, Request, CustErr> for T
where Request: Req<CustErr> + Send + 'static, T: From<ByteStream> + 'static,

source§

async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>

Attempts to deserialize the arguments from a request.
source§

impl<CustErr, T, Request> FromReq<StreamingText, Request, CustErr> for T
where Request: Req<CustErr> + Send + 'static, T: From<TextStream> + 'static,

source§

async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>

Attempts to deserialize the arguments from a request.
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Serializable for T

source§

fn ser(&self) -> Result<String, SerializationError>

Serializes the object to a string.
source§

fn de(json: &str) -> Result<T, SerializationError>

Deserializes the object from some bytes.
source§

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

§

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>,

§

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>,

§

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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

source§

impl<El> ElementDescriptorBounds for El
where El: Debug,