Skip to main content

Readable

Trait Readable 

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

    // Required methods
    fn try_read_unchecked(
        &self,
    ) -> Result<<Self::Storage as AnyStorage>::Ref<'static, Self::Target>, BorrowError>
       where Self::Target: 'static;
    fn try_peek_unchecked(
        &self,
    ) -> Result<<Self::Storage as AnyStorage>::Ref<'static, Self::Target>, BorrowError>
       where Self::Target: 'static;
    fn subscribers(&self) -> Subscribers
       where Self::Target: 'static;
}
Available on crate feature prelude only.
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<<Self::Storage as AnyStorage>::Ref<'static, Self::Target>, 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<<Self::Storage as AnyStorage>::Ref<'static, Self::Target>, 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.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<Index, Write, K, V, St> Readable for GetWrite<Index, Write>
where Write: Readable<Target = HashMap<K, V, St>>, Index: Hash + Eq + 'static, K: Borrow<Index> + Eq + Hash + 'static, St: BuildHasher + 'static,

Source§

type Target = V

Source§

type Storage = <Write as Readable>::Storage

Source§

fn try_read_unchecked( &self, ) -> Result<<<GetWrite<Index, Write> as Readable>::Storage as AnyStorage>::Ref<'static, <GetWrite<Index, Write> as Readable>::Target>, BorrowError>
where <GetWrite<Index, Write> as Readable>::Target: 'static,

Source§

fn try_peek_unchecked( &self, ) -> Result<<<GetWrite<Index, Write> as Readable>::Storage as AnyStorage>::Ref<'static, <GetWrite<Index, Write> as Readable>::Target>, BorrowError>
where <GetWrite<Index, Write> as Readable>::Target: 'static,

Source§

fn subscribers(&self) -> Subscribers
where <GetWrite<Index, Write> as Readable>::Target: 'static,

Source§

impl<Index, Write, K, V> Readable for GetWrite<Index, Write>
where Write: Readable<Target = BTreeMap<K, V>>, Index: Ord + 'static, K: Borrow<Index> + Ord + 'static,

Source§

type Target = V

Source§

type Storage = <Write as Readable>::Storage

Source§

fn try_read_unchecked( &self, ) -> Result<<<GetWrite<Index, Write> as Readable>::Storage as AnyStorage>::Ref<'static, <GetWrite<Index, Write> as Readable>::Target>, BorrowError>
where <GetWrite<Index, Write> as Readable>::Target: 'static,

Source§

fn try_peek_unchecked( &self, ) -> Result<<<GetWrite<Index, Write> as Readable>::Storage as AnyStorage>::Ref<'static, <GetWrite<Index, Write> as Readable>::Target>, BorrowError>
where <GetWrite<Index, Write> as Readable>::Target: 'static,

Source§

fn subscribers(&self) -> Subscribers
where <GetWrite<Index, Write> as Readable>::Target: 'static,

Source§

impl<Index, Write> Readable for IndexWrite<Index, Write>
where Write: Readable, <Write as Readable>::Target: Index<Index> + 'static, Index: Clone,

Source§

type Target = <<Write as Readable>::Target as Index<Index>>::Output

Source§

type Storage = <Write as Readable>::Storage

Source§

fn try_read_unchecked( &self, ) -> Result<<<IndexWrite<Index, Write> as Readable>::Storage as AnyStorage>::Ref<'static, <IndexWrite<Index, Write> as Readable>::Target>, BorrowError>
where <IndexWrite<Index, Write> as Readable>::Target: 'static,

Source§

fn try_peek_unchecked( &self, ) -> Result<<<IndexWrite<Index, Write> as Readable>::Storage as AnyStorage>::Ref<'static, <IndexWrite<Index, Write> as Readable>::Target>, BorrowError>
where <IndexWrite<Index, Write> as Readable>::Target: 'static,

Source§

fn subscribers(&self) -> Subscribers
where <IndexWrite<Index, Write> as Readable>::Target: 'static,

Source§

impl<Lens> Readable for SelectorScope<Lens>
where Lens: Readable,

Source§

type Target = <Lens as Readable>::Target

Source§

type Storage = <Lens as Readable>::Storage

Source§

fn try_read_unchecked( &self, ) -> Result<<<Lens as Readable>::Storage as AnyStorage>::Ref<'static, <Lens as Readable>::Target>, BorrowError>

Source§

fn try_peek_unchecked( &self, ) -> Result<<<Lens as Readable>::Storage as AnyStorage>::Ref<'static, <Lens as Readable>::Target>, BorrowError>

Source§

fn subscribers(&self) -> Subscribers

Source§

impl<Write, T> Readable for VecGetWrite<Write>
where Write: Readable<Target = Vec<T>>, T: 'static,

Source§

type Target = T

Source§

type Storage = <Write as Readable>::Storage

Source§

fn try_read_unchecked( &self, ) -> Result<<<VecGetWrite<Write> as Readable>::Storage as AnyStorage>::Ref<'static, <VecGetWrite<Write> as Readable>::Target>, BorrowError>
where <VecGetWrite<Write> as Readable>::Target: 'static,

Source§

fn try_peek_unchecked( &self, ) -> Result<<<VecGetWrite<Write> as Readable>::Storage as AnyStorage>::Ref<'static, <VecGetWrite<Write> as Readable>::Target>, BorrowError>
where <VecGetWrite<Write> as Readable>::Target: 'static,

Source§

fn subscribers(&self) -> Subscribers
where <VecGetWrite<Write> as Readable>::Target: 'static,

Implementors§

Source§

impl Readable for UseFuture

Source§

impl<T, Lens> Readable for Store<T, Lens>
where Lens: Readable<Target = T>, T: 'static + ?Sized,

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

impl<T> Readable for Resource<T>

Source§

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

Source§

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