Trait Readable

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

Show 13 methods // Required methods fn try_read_unchecked( &self, ) -> Result<ReadableRef<'static, Self>, BorrowError>; fn try_peek_unchecked( &self, ) -> Result<ReadableRef<'static, Self>, BorrowError>; // Provided methods fn map<O>( self, f: impl Fn(&Self::Target) -> &O + 'static, ) -> MappedSignal<O, Self::Storage> where Self: Clone + Sized + 'static { ... } fn read(&self) -> ReadableRef<'_, Self> { ... } fn try_read(&self) -> Result<ReadableRef<'_, Self>, BorrowError> { ... } fn read_unchecked(&self) -> ReadableRef<'static, Self> { ... } fn peek(&self) -> ReadableRef<'_, Self> { ... } fn try_peek(&self) -> Result<ReadableRef<'_, Self>, BorrowError> { ... } fn peek_unchecked(&self) -> ReadableRef<'static, Self> { ... } fn cloned(&self) -> Self::Target where Self::Target: Clone { ... } fn with<O>(&self, f: impl FnOnce(&Self::Target) -> O) -> O { ... } fn with_peek<O>(&self, f: impl FnOnce(&Self::Target) -> O) -> O { ... } fn index<I>( &self, index: I, ) -> ReadableRef<'_, Self, <Self::Target as Index<I>>::Output> where Self::Target: Index<I> { ... }
}
Expand description

A trait for states that can be read from like crate::Signal, crate::GlobalSignal, or crate::ReadOnlySignal. 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, ReadOnlySignal, 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 + 'static

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>

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>

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.

Provided Methods§

Source

fn map<O>( self, f: impl Fn(&Self::Target) -> &O + 'static, ) -> MappedSignal<O, Self::Storage>
where Self: Clone + Sized + 'static,

Map the readable type to a new type. This lets you provide a view into a readable type without needing to clone the inner value.

Anything that subscribes to the readable value will be rerun whenever the original value changes, even if the view does not change. If you want to memorize the view, you can use a crate::Memo instead.

§Example
fn List(list: Signal<Vec<i32>>) -> Element {
    rsx! {
        for index in 0..list.len() {
            // We can use the `map` method to provide a view into the single item in the list that the child component will render
            Item { item: list.map(move |v| &v[index]) }
        }
    }
}

// The child component doesn't need to know that the mapped value is coming from a list
#[component]
fn Item(item: MappedSignal<i32>) -> Element {
    rsx! {
        div { "Item: {item}" }
    }
}
Source

fn read(&self) -> ReadableRef<'_, Self>

Get the current value of the state. If this is a signal, this will subscribe the current scope to the signal. If the value has been dropped, this will panic. Calling this on a Signal is the same as using the signal() syntax to read and subscribe to its value

Source

fn try_read(&self) -> Result<ReadableRef<'_, Self>, BorrowError>

Try to get the current value of the state. If this is a signal, this will subscribe the current scope to the signal.

Source

fn read_unchecked(&self) -> ReadableRef<'static, Self>

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 peek(&self) -> ReadableRef<'_, Self>

Get the current value of the state without subscribing to updates. If the value has been dropped, this will panic.

§Example
fn MyComponent(mut count: Signal<i32>) -> Element {
    let mut event_source = use_signal(|| None);
    let doubled = use_memo(move || {
        // We want to log the value of the event_source, but we don't need to rerun the doubled value if the event_source changes (because the value of doubled doesn't depend on the event_source)
        // We can read the value with peek without subscribing to updates
        let source = event_source.peek();
        tracing::info!("Clicked: {source:?}");
        count() * 2
    });
    rsx! {
        div { "Count: {count}" }
        div { "Doubled: {doubled}" }
        button {
            onclick: move |_| {
                event_source.set(Some("Click me button"));
                count += 1;
            },
            "Click me"
        }
        button {
            onclick: move |_| {
                event_source.set(Some("Double me button"));
                count += 1;
            },
            "Double me"
        }
    }
}
Source

fn try_peek(&self) -> Result<ReadableRef<'_, Self>, BorrowError>

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

Source

fn peek_unchecked(&self) -> ReadableRef<'static, Self>

Get the current value of the signal without checking the lifetime. Unlike read, this will not subscribe the current scope to the signal which can cause parts of your UI to not update.

If the signal has been dropped, this will panic.

Source

fn cloned(&self) -> Self::Target
where Self::Target: Clone,

Clone the inner value and return it. If the value has been dropped, this will panic.

Source

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

Run a function with a reference to the value. If the value has been dropped, this will panic.

Source

fn with_peek<O>(&self, f: impl FnOnce(&Self::Target) -> O) -> O

Run a function with a reference to the value. If the value has been dropped, this will panic.

Source

fn index<I>( &self, index: I, ) -> ReadableRef<'_, Self, <Self::Target as Index<I>>::Output>
where Self::Target: Index<I>,

Index into the inner value and return a reference to the result. If the value has been dropped or the index is invalid, this will panic.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<O, S> Readable for MappedSignal<O, S>
where O: ?Sized, S: AnyStorage,

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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