Struct Signal

Source
pub struct Signal<T, S = SyncStorage>
where S: Storage<T>,
{ /* private fields */ }
Expand description

A wrapper for any kind of arena-allocated reactive signal: a ReadSignal, Memo, RwSignal, or derived signal closure, or a plain value of the same type

This allows you to create APIs that take T or any reactive value that returns T as an argument, rather than adding a generic F: Fn() -> T.

Values can be accessed with the same function call, read(), with(), and get() APIs as other signals.

§Important Notes about Derived Signals

Signal::derive() is simply a way to box and type-erase a “derived signal,” which is a plain closure that accesses one or more signals. It does not cache the value of that computation. Accessing the value of a Signal<_> that is created using Signal::derive() will run the closure again every time you call .read(), .with(), or .get().

If you want the closure to run the minimal number of times necessary to update its state, and then to cache its value, you should use a Memo (and convert it into a Signal<_>) rather than using Signal::derive().

Note that for many computations, it is nevertheless less expensive to use a derived signal than to create a separate memo and to cache the value: creating a new reactive node and taking the lock on that cached value whenever you access the signal is more expensive than simply re-running the calculation in many cases.

Implementations§

Source§

impl<T> Signal<T>
where T: Send + Sync + 'static,

Source

pub fn derive(derived_signal: impl Fn() -> T + Send + Sync + 'static) -> Self

Wraps a derived signal, i.e., any computation that accesses one or more reactive signals.

let (count, set_count) = signal(2);
let double_count = Signal::derive(move || count.get() * 2);

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

assert_eq!(above_3(&count.into()), false);
assert_eq!(above_3(&double_count), true);
Source

pub fn stored(value: T) -> Self

Moves a static, nonreactive value into a signal, backed by ArcStoredValue.

Source§

impl<T> Signal<T, LocalStorage>
where T: 'static,

Source

pub fn derive_local(derived_signal: impl Fn() -> T + 'static) -> Self

Wraps a derived signal. Works like Signal::derive but uses LocalStorage.

Source

pub fn stored_local(value: T) -> Self

Moves a static, nonreactive value into a signal, backed by ArcStoredValue. Works like Signal::stored but uses LocalStorage.

Trait Implementations§

Source§

impl<T, S> Clone for Signal<T, S>
where S: Storage<T>,

Source§

fn clone(&self) -> Self

Returns a duplicate 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, S> Debug for Signal<T, S>
where S: Debug + Storage<T>,

Source§

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

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

impl<T> Default for Signal<T>
where T: Send + Sync + Default + 'static,

Source§

fn default() -> Self

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

impl<T> Default for Signal<T, LocalStorage>
where T: Default + 'static,

Source§

fn default() -> Self

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

impl<T, S> DefinedAt for Signal<T, S>
where S: Storage<T>,

Source§

fn defined_at(&self) -> Option<&'static Location<'static>>

Returns the location at which the signal was defined. This is usually simply None in release mode.
Source§

impl<T, S> Dispose for Signal<T, S>
where S: Storage<T>,

Source§

fn dispose(self)

Disposes of the signal. This: Read more
Source§

impl<T, S> Fn() for Signal<T, S>
where Signal<T, S>: Get, S: Storage<T> + Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>>,

Source§

extern "rust-call" fn call(&self, _args: ()) -> Self::Output

🔬This is a nightly-only experimental API. (fn_traits)
Performs the call operation.
Source§

impl<T, S> FnMut() for Signal<T, S>
where Signal<T, S>: Get, S: Storage<T> + Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>>,

Source§

extern "rust-call" fn call_mut(&mut self, _args: ()) -> Self::Output

🔬This is a nightly-only experimental API. (fn_traits)
Performs the call operation.
Source§

impl<T, S> FnOnce() for Signal<T, S>
where Signal<T, S>: Get, S: Storage<T> + Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>>,

Source§

type Output = <Signal<T, S> as Get>::Value

The returned type after the call operator is used.
Source§

extern "rust-call" fn call_once(self, _args: ()) -> Self::Output

🔬This is a nightly-only experimental API. (fn_traits)
Performs the call operation.
Source§

impl From<&str> for Signal<Option<String>>

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for Signal<Option<String>, LocalStorage>

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for Signal<String>

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for Signal<String, LocalStorage>

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl<T> From<ArcMappedSignal<T>> for Signal<T>
where T: Clone + Send + Sync + 'static,

Source§

fn from(value: ArcMappedSignal<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<ArcMemo<T>> for Signal<T>
where T: Send + Sync + 'static,

Source§

fn from(value: ArcMemo<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<ArcMemo<T, LocalStorage>> for Signal<T, LocalStorage>
where T: Send + Sync + 'static,

Source§

fn from(value: ArcMemo<T, LocalStorage>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<ArcReadSignal<T>> for Signal<T>
where T: Send + Sync + 'static,

Source§

fn from(value: ArcReadSignal<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<ArcReadSignal<T>> for Signal<T, LocalStorage>
where T: Send + Sync + 'static,

Source§

fn from(value: ArcReadSignal<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<ArcRwSignal<T>> for Signal<T>
where T: Send + Sync + 'static,

Source§

fn from(value: ArcRwSignal<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<ArcRwSignal<T>> for Signal<T, LocalStorage>
where T: Send + Sync + 'static,

Source§

fn from(value: ArcRwSignal<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<ArcSignal<T>> for Signal<T>
where T: Send + Sync + 'static,

Source§

fn from(value: ArcSignal<T, SyncStorage>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<MappedSignal<T>> for Signal<T>
where T: Clone + Send + Sync + 'static,

Source§

fn from(value: MappedSignal<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<MaybeSignal<T>> for Signal<Option<T>>
where T: Clone + Send + Sync + 'static,

Source§

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

Converts to this type from the input type.
Source§

impl<T> From<MaybeSignal<T>> for Signal<T>
where T: Send + Sync + 'static,

Source§

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

Converts to this type from the input type.
Source§

impl<T> From<MaybeSignal<T, LocalStorage>> for Signal<Option<T>, LocalStorage>
where T: Clone + Send + Sync + 'static,

Source§

fn from(value: MaybeSignal<T, LocalStorage>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<MaybeSignal<T, LocalStorage>> for Signal<T, LocalStorage>
where T: Send + Sync + 'static,

Source§

fn from(value: MaybeSignal<T, LocalStorage>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Memo<T>> for Signal<T>
where T: Send + Sync + 'static,

Source§

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

Converts to this type from the input type.
Source§

impl<T> From<Memo<T, LocalStorage>> for Signal<T, LocalStorage>
where T: 'static,

Source§

fn from(value: Memo<T, LocalStorage>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<ReadSignal<T>> for Signal<T>
where T: Send + Sync + 'static,

Source§

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

Converts to this type from the input type.
Source§

impl<T> From<ReadSignal<T, LocalStorage>> for Signal<T, LocalStorage>
where T: 'static,

Source§

fn from(value: ReadSignal<T, LocalStorage>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<RwSignal<T>> for Signal<T>
where T: Send + Sync + 'static,

Source§

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

Converts to this type from the input type.
Source§

impl<T> From<RwSignal<T, LocalStorage>> for Signal<T, LocalStorage>
where T: 'static,

Source§

fn from(value: RwSignal<T, LocalStorage>) -> Self

Converts to this type from the input type.
Source§

impl From<Signal<&'static str>> for Signal<Option<String>>

Source§

fn from(value: Signal<&'static str>) -> Self

Converts to this type from the input type.
Source§

impl From<Signal<&'static str>> for Signal<Option<String>, LocalStorage>

Source§

fn from(value: Signal<&'static str>) -> Self

Converts to this type from the input type.
Source§

impl From<Signal<&'static str>> for Signal<String>

Source§

fn from(value: Signal<&'static str>) -> Self

Converts to this type from the input type.
Source§

impl From<Signal<&'static str>> for Signal<String, LocalStorage>

Source§

fn from(value: Signal<&'static str>) -> Self

Converts to this type from the input type.
Source§

impl From<Signal<Option<&'static str>>> for Signal<Option<String>>

Source§

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

Converts to this type from the input type.
Source§

impl From<Signal<Option<&'static str>>> for Signal<Option<String>, LocalStorage>

Source§

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

Converts to this type from the input type.
Source§

impl<T> From<Signal<Option<T>>> for MaybeProp<T>
where T: Send + Sync, SyncStorage: Storage<Option<T>>,

Source§

fn from(value: Signal<Option<T>>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Signal<Option<T>, LocalStorage>> for MaybeProp<T, LocalStorage>

Source§

fn from(value: Signal<Option<T>, LocalStorage>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Signal<T>> for MaybeProp<T>
where T: Send + Sync + Clone,

Source§

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

Converts to this type from the input type.
Source§

impl<T> From<Signal<T>> for Signal<Option<T>>
where T: Clone + Send + Sync + 'static,

Source§

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

Converts to this type from the input type.
Source§

impl<T> From<Signal<T, LocalStorage>> for MaybeProp<T, LocalStorage>
where T: Send + Sync + Clone,

Source§

fn from(value: Signal<T, LocalStorage>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Signal<T, LocalStorage>> for Signal<Option<T>, LocalStorage>
where T: Clone + 'static,

Source§

fn from(value: Signal<T, LocalStorage>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<Signal<T, S>> for ArcSignal<T, S>
where S: Storage<SignalTypes<T, S>> + Storage<T>,

Source§

fn from(value: Signal<T, S>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<Signal<T, S>> for MaybeSignal<T, S>
where S: Storage<T>,

Source§

fn from(value: Signal<T, S>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<T> for Signal<Option<T>>
where T: Send + Sync + 'static,

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T> From<T> for Signal<Option<T>, LocalStorage>
where T: 'static,

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T> From<T> for Signal<T>
where T: Send + Sync + 'static,

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T> From<T> for Signal<T, LocalStorage>
where T: 'static,

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T> FromLocal<ArcSignal<T, LocalStorage>> for Signal<T, LocalStorage>
where T: 'static,

Source§

fn from_local(value: ArcSignal<T, LocalStorage>) -> Self

Converts between the types.
Source§

impl<T, S> PartialEq for Signal<T, S>
where S: Storage<T>,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, S> ReadUntracked for Signal<T, S>
where T: 'static, S: Storage<SignalTypes<T, S>> + Storage<T>,

Source§

fn custom_try_read(&self) -> Option<Option<Self::Value>>

Overriding the default auto implemented Read::try_read to combine read and track, to avoid 2 clones and just have 1 in the SignalTypes::DerivedSignal.

Source§

type Value = ReadGuard<T, SignalReadGuard<T, S>>

The guard type that will be returned, which can be dereferenced to the value.
Source§

fn try_read_untracked(&self) -> Option<Self::Value>

Returns the guard, or None if the signal has already been disposed.
Source§

fn read_untracked(&self) -> Self::Value

Returns the guard. Read more
Source§

impl<T, St> Serialize for Signal<T, St>
where T: Send + Sync + Serialize + 'static, St: Storage<SignalTypes<T, St>> + Storage<T>,

Source§

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

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

impl<T, S> Track for Signal<T, S>
where T: 'static, S: Storage<T> + Storage<SignalTypes<T, S>>,

Source§

fn track(&self)

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

impl<T, S> Copy for Signal<T, S>
where S: Storage<T>,

Source§

impl<T, S> Eq for Signal<T, S>
where S: Storage<T>,

Auto Trait Implementations§

§

impl<T, S> Freeze for Signal<T, S>

§

impl<T, S> RefUnwindSafe for Signal<T, S>

§

impl<T, S> Send for Signal<T, S>

§

impl<T, S> Sync for Signal<T, S>

§

impl<T, S> Unpin for Signal<T, S>

§

impl<T, S> UnwindSafe for Signal<T, S>

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, dest: *mut u8)

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

impl<Func> EffectFunction<(), NoParam> for Func
where Func: FnMut(),

Source§

fn run(&mut self, _: Option<()>)

Call this to execute the function. In case the actual function has no parameters the parameter p will simply be ignored.
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<S, T> FromStream<T> for S
where S: From<ArcReadSignal<Option<T>>> + Send + Sync, T: Send + Sync + 'static,

Source§

fn from_stream(stream: impl Stream<Item = T> + Send + 'static) -> S

Creates a signal that contains the latest value of the stream.
Source§

fn from_stream_unsync(stream: impl Stream<Item = T> + 'static) -> S

Creates a signal that contains the latest value of the stream.
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> Read for T
where T: Track + ReadUntracked,

Source§

type Value = <T as ReadUntracked>::Value

The guard type that will be returned, which can be dereferenced to the value.
Source§

fn try_read(&self) -> Option<<T as Read>::Value>

Subscribes to the signal, and returns the guard, or None if the signal has already been disposed.
Source§

fn read(&self) -> Self::Value

Subscribes to the signal, and returns the guard. Read more
Source§

impl<T> StorageAccess<T> for T

Source§

fn as_borrowed(&self) -> &T

Borrows the value.
Source§

fn into_taken(self) -> T

Takes the value.
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.
Source§

impl<T> With for T
where T: Read,

Source§

type Value = <<T as Read>::Value as Deref>::Target

The type of the value contained in the signal.
Source§

fn try_with<U>(&self, fun: impl FnOnce(&<T as With>::Value) -> U) -> Option<U>

Subscribes to the signal, applies the closure to the value, and returns the result, or None if the signal has already been disposed.
Source§

fn with<U>(&self, fun: impl FnOnce(&Self::Value) -> U) -> U

Subscribes to the signal, applies the closure to the value, and returns the result. Read more
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> WithUntracked for T

Source§

type Value = <<T as ReadUntracked>::Value as Deref>::Target

The type of the value contained in the signal.
Source§

fn try_with_untracked<U>( &self, fun: impl FnOnce(&<T as WithUntracked>::Value) -> U, ) -> Option<U>

Applies the closure to the value, and returns the result, or None if the signal has already been disposed.
Source§

fn with_untracked<U>(&self, fun: impl FnOnce(&Self::Value) -> U) -> U

Applies the closure to the value, and returns the result. Read more