WritableExt

Trait WritableExt 

Source
pub trait WritableExt: Writable {
    // Provided methods
    fn write(&mut self) -> WritableRef<'_, Self>
       where Self::Target: 'static { ... }
    fn try_write(&mut self) -> Result<WritableRef<'_, Self>, BorrowMutError>
       where Self::Target: 'static { ... }
    fn write_unchecked(&self) -> WritableRef<'static, Self>
       where Self::Target: 'static { ... }
    fn map_mut<O, F, FMut>(
        self,
        f: F,
        f_mut: FMut,
    ) -> MappedMutSignal<O, Self, F, FMut>
       where Self: Sized,
             O: ?Sized,
             F: Fn(&Self::Target) -> &O,
             FMut: Fn(&mut Self::Target) -> &mut O { ... }
    fn with_mut<O>(&mut self, f: impl FnOnce(&mut Self::Target) -> O) -> O
       where Self::Target: 'static { ... }
    fn set(&mut self, value: Self::Target)
       where Self::Target: Sized + 'static { ... }
    fn toggle(&mut self)
       where Self::Target: Not<Output = Self::Target> + Clone + 'static { ... }
    fn index_mut<I>(
        &mut self,
        index: I,
    ) -> WritableRef<'_, Self, <Self::Target as Index<I>>::Output>
       where Self::Target: IndexMut<I> + 'static { ... }
    fn take(&mut self) -> Self::Target
       where Self::Target: Default + 'static { ... }
    fn replace(&mut self, value: Self::Target) -> Self::Target
       where Self::Target: Sized + 'static { ... }
}
Expand description

An extension trait for Writable that provides some convenience methods.

Provided Methods§

Source

fn write(&mut self) -> WritableRef<'_, Self>
where Self::Target: 'static,

Get a mutable reference to the value. If the value has been dropped, this will panic.

Source

fn try_write(&mut self) -> Result<WritableRef<'_, Self>, BorrowMutError>
where Self::Target: 'static,

Try to get a mutable reference to the value.

Source

fn write_unchecked(&self) -> WritableRef<'static, Self>
where Self::Target: 'static,

Get a mutable reference to the value without checking the lifetime. This will update any subscribers.

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

Source

fn map_mut<O, F, FMut>( self, f: F, f_mut: FMut, ) -> MappedMutSignal<O, Self, F, FMut>
where Self: Sized, O: ?Sized, F: Fn(&Self::Target) -> &O, FMut: Fn(&mut Self::Target) -> &mut O,

Map the references and mutable references of the writable value to a new type. This lets you provide a view into the writable value without creating a new signal or cloning the value.

Anything that subscribes to the writable value will be rerun whenever the original value changes or you write to this scoped value, even if the view does not change. If you want to memorize the view, you can use a crate::Memo instead. For fine grained scoped updates, use stores 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_mut(move |v| &v[index], move |v| &mut v[index]) }
        }
    }
}

// The child component doesn't need to know that the mapped value is coming from a list
#[component]
fn Item(item: WriteSignal<i32>) -> Element {
    rsx! {
        button {
            onclick: move |_| *item.write() += 1,
            "{item}"
        }
    }
}
Source

fn with_mut<O>(&mut self, f: impl FnOnce(&mut Self::Target) -> O) -> O
where Self::Target: 'static,

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

Source

fn set(&mut self, value: Self::Target)
where Self::Target: Sized + 'static,

Set the value of the signal. This will trigger an update on all subscribers.

Source

fn toggle(&mut self)
where Self::Target: Not<Output = Self::Target> + Clone + 'static,

Invert the boolean value of the signal. This will trigger an update on all subscribers.

Source

fn index_mut<I>( &mut self, index: I, ) -> WritableRef<'_, Self, <Self::Target as Index<I>>::Output>
where Self::Target: IndexMut<I> + 'static,

Index into the inner value and return a reference to the result.

Source

fn take(&mut self) -> Self::Target
where Self::Target: Default + 'static,

Takes the value out of the Signal, leaving a Default in its place.

Source

fn replace(&mut self, value: Self::Target) -> Self::Target
where Self::Target: Sized + 'static,

Replace the value in the Signal, returning the old value.

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§