Struct Signal

Source
pub struct Signal<T: ?Sized + 'static>(/* private fields */);
Expand description

Similar to Rc<dyn Fn() -> &T>, but with added functionality to observe changes in the result.

Use the following methods to create an instance of Signal.

Implementations§

Source§

impl<T: ?Sized + 'static> Signal<T>

Source

pub fn new(f: impl Fn(&mut SignalContext<'_>) -> T + 'static) -> Self
where T: Sized,

Create a new Signal from a function to get a value.

The signal created by this function also sends a notification when f returns the same value as before. new_dedup must be used to avoid sending a notification when f returns the same value as before.

Source

pub fn new_dedup(f: impl Fn(&mut SignalContext<'_>) -> T + 'static) -> Self
where T: Sized + PartialEq,

Creates a new Signal from a function to get a value, with deduplication.

The signal created by this function does not send a notification when f returns the same value as before.

Even if the value is not changed, a “value may have changed” notification is sent to the dependants, so the overhead cannot be zero.

Source

pub fn from_value(value: T) -> Self
where T: Sized,

Create a new Signal that does not change from its value.

Source

pub fn from_value_map<U>(value: U, f: impl Fn(&U) -> &T + 'static) -> Self
where U: 'static,

Create a new Signal that does not change from its value, with a mapping function.

Source

pub fn from_owned(owned: impl Borrow<T> + 'static) -> Self

Creates a new Signal from an owned value.

Source

pub fn from_node(node: Rc<impl SignalNode<Value = T>>) -> Self

Create a new Signal by specifying the internal implementation.

Source

pub fn from_borrow<U>( this: U, borrow: impl for<'s, 'a> Fn(&'a U, &mut SignalContext<'s>, &'a &'s ()) -> StateRef<'a, T> + 'static, ) -> Self
where U: 'static,

Create a new Signal from a function to get StateRef.

Source

pub fn from_static_ref(value: &'static T) -> Self

Create a new Signal that does not change from a static reference.

Source

pub fn from_future(future: impl Future<Output = T> + 'static) -> Signal<Poll<T>>
where T: Sized,

Create a new Signal from a Future.

Source

pub fn from_stream(stream: impl Stream<Item = T> + 'static) -> Signal<Poll<T>>
where T: Sized,

Create a new Signal from a Stream.

Source

pub fn from_async<Fut>( f: impl Fn(AsyncSignalContext) -> Fut + 'static, ) -> Signal<Poll<T>>
where Fut: Future<Output = T> + 'static, T: Sized,

Create a Signal from an asynchronous function to get a value.

Source

pub fn borrow<'a, 's: 'a>( &'a self, sc: &mut SignalContext<'s>, ) -> StateRef<'a, T>

Obtains a reference to the current value and adds a dependency on this Signal to the specified SignalContext.

If the current value has not yet been calculated, it will be calculated.

Source

pub fn get(&self, sc: &mut SignalContext<'_>) -> <T as ToOwned>::Owned
where T: ToOwned,

Gets the current value and adds a dependency on this Signal to the specified SignalContext.

If the current value has not yet been calculated, it will be calculated.

Source

pub fn map<U: ?Sized>(&self, f: impl Fn(&T) -> &U + 'static) -> Signal<U>

Creates a new Signal whose references are transformed by the specified function.

Using SignalBuilder::map, you can create similar Signal more efficiently.

Source

pub fn dedup(&self) -> Signal<T>
where T: ToOwned + PartialEq,

Create a Signal that does not send notifications to the dependants if the value does not change.

Even if the value is not changed, a “value may have changed” notification is sent to the dependants, so the overhead cannot be zero.

Using SignalBuilder::dedup, you can create similar Signal more efficiently.

Source

pub fn keep(&self) -> Signal<T>

Create a Signal that keeps a cache even if the subscriber does not exist.

Normally, Signal discards the cache at the time Runtime::run_discards is called if there are no subscribers. Signals created by this method do not discards the cache even if there are no subscribers.

Using SignalBuilder::keep, you can create similar Signal more efficiently.

Source

pub fn effect(&self, f: impl FnMut(&T) + 'static) -> Subscription

Subscribe to the value of this signal.

First, call the function with the current value, then call the function each time the value changes.

The function is called when Runtime::run_tasks is called with None or Some(TaskKind::default()).

When the Subscription returned by this function is dropped, the subscription is canceled.

Source

pub fn effect_with( &self, f: impl FnMut(&T) + 'static, kind: TaskKind, ) -> Subscription

Subscribe to the value of this signal with specifying TaskKind.

First, call the function with the current value, then call the function each time the value changes.

The function is called when Runtime::run_tasks is called with None or Some(kind).

When the Subscription returned by this function is dropped, the subscription is canceled.

Source

pub fn to_stream(&self) -> impl Stream<Item = T::Owned> + Unpin + 'static
where T: ToOwned,

Create a Stream to subscribe to the value of this signal.

Source

pub fn to_stream_map<U: 'static>( &self, f: impl Fn(&T) -> U + 'static, ) -> impl Stream<Item = U> + Unpin + 'static

Create a Stream that subscribes to the value of this signal by specifying a conversion function.

Source

pub fn ptr_eq(this: &Self, other: &Self) -> bool

Returns true if two Signal instances are equal.

Signal works like Rc; just as Rc::ptr_eq allows you to check if the instance of Rc is the same, this function allows you to check if the instance of Signal is the same.

Source§

impl<T: 'static> Signal<Poll<T>>

Source

pub async fn get_async(&self, sc: &mut AsyncSignalContext) -> T
where T: Clone,

Waits until the current value is Ready and returns that value.

Trait Implementations§

Source§

impl<T: ?Sized + 'static> Clone for Signal<T>
where RawSignal<T>: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: 'static + ?Sized + Debug> Debug for Signal<T>

Source§

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

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

impl<T: ?Sized + 'static> ToSignal for Signal<T>

Source§

type Value = T

Source§

fn to_signal(&self) -> Signal<Self::Value>

Auto Trait Implementations§

§

impl<T> Freeze for Signal<T>
where T: ?Sized,

§

impl<T> !RefUnwindSafe for Signal<T>

§

impl<T> !Send for Signal<T>

§

impl<T> !Sync for Signal<T>

§

impl<T> Unpin for Signal<T>
where T: ?Sized,

§

impl<T> !UnwindSafe for Signal<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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoResult<T> for T

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.