Readable

Trait Readable 

Source
pub trait Readable {
    type Target: ?Sized;
    type Storage: AnyStorage;

    // Required methods
    fn try_read_unchecked(
        &self,
    ) -> Result<ReadableRef<'static, Self>, BorrowError>
       where Self::Target: 'static;
    fn try_peek_unchecked(
        &self,
    ) -> Result<ReadableRef<'static, Self>, BorrowError>
       where Self::Target: 'static;
    fn subscribers(&self) -> Subscribers
       where Self::Target: 'static;
}
Expand description

A trait for states that can be read from like crate::Signal, crate::GlobalSignal, or crate::ReadSignal. You may choose to accept this trait as a parameter instead of the concrete type to allow for more flexibility in your API. For example, instead of creating two functions, one that accepts a crate::Signal and one that accepts a crate::GlobalSignal, you can create one function that accepts a Readable type.

§Example

fn double(something_readable: &impl Readable<Target = i32>) -> i32 {
    something_readable.cloned() * 2
}

static COUNT: GlobalSignal<i32> = Signal::global(|| 0);

fn MyComponent(count: Signal<i32>) -> Element {
    // Since we defined the function in terms of the readable trait, we can use it with any readable type (Signal, GlobalSignal, ReadSignal, etc)
    let doubled = use_memo(move || double(&count));
    let global_count_doubled = use_memo(|| double(&COUNT));
    rsx! {
        div {
            "Count local: {count}"
            "Doubled local: {doubled}"
            "Count global: {COUNT}"
            "Doubled global: {global_count_doubled}"
        }
    }
}

Required Associated Types§

Source

type Target: ?Sized

The target type of the reference.

Source

type Storage: AnyStorage

The type of the storage this readable uses.

Required Methods§

Source

fn try_read_unchecked(&self) -> Result<ReadableRef<'static, Self>, BorrowError>
where Self::Target: 'static,

Try to get a reference to the value without checking the lifetime. This will subscribe the current scope to the signal.

NOTE: This method is completely safe because borrow checking is done at runtime.

Source

fn try_peek_unchecked(&self) -> Result<ReadableRef<'static, Self>, BorrowError>
where Self::Target: 'static,

Try to peek the current value of the signal without subscribing to updates. If the value has been dropped, this will return an error.

NOTE: This method is completely safe because borrow checking is done at runtime.

Source

fn subscribers(&self) -> Subscribers
where Self::Target: 'static,

Get the underlying subscriber list for this readable. This is used to track when the value changes and notify subscribers.

Implementors§

Source§

impl<T> Readable for Memo<T>
where T: PartialEq,

Source§

impl<T, R> Readable for Global<T, R>
where T: Readable<Target = R> + InitializeFromFunction<R> + Clone + 'static,

Source§

impl<T, S: Storage<SignalData<T>>> Readable for Signal<T, S>

Source§

impl<T, S: Storage<T>> Readable for CopyValue<T, S>

Source§

impl<T: ?Sized, S: BoxedSignalStorage<T>> Readable for ReadSignal<T, S>

Source§

impl<T: ?Sized, S: BoxedSignalStorage<T>> Readable for WriteSignal<T, S>

Source§

impl<V, O, F> Readable for MappedSignal<O, V, F>
where O: ?Sized, V: Readable, V::Target: 'static, F: Fn(&V::Target) -> &O,

Source§

impl<V, O, F, FMut> Readable for MappedMutSignal<O, V, F, FMut>
where O: ?Sized, V: Readable, V::Target: 'static, F: Fn(&V::Target) -> &O,