Skip to main content

WritableUtils

Trait WritableUtils 

pub trait WritableUtils<T>
where T: 'static,
{ // Required methods fn write_state(&mut self) -> <UnsyncStorage as AnyStorage>::Mut<'static, T>; fn peek_state(&self) -> <UnsyncStorage as AnyStorage>::Ref<'static, T>; // Provided methods fn set(&mut self, value: T) { ... } fn set_if_modified(&mut self, value: T) where T: PartialEq { ... } fn set_if_modified_and_then(&mut self, value: T, then: impl FnOnce()) where T: PartialEq { ... } fn with_mut( &mut self, with: impl FnOnce(<UnsyncStorage as AnyStorage>::Mut<'static, T>), ) { ... } }
Expand description

A shared interface for types that provide reactive write access to a value of type T.

Implementors must provide WritableUtils::write_state and WritableUtils::peek_state. All convenience methods (WritableUtils::set, WritableUtils::set_if_modified, etc.) are provided as defaults.

Required Methods§

fn write_state(&mut self) -> <UnsyncStorage as AnyStorage>::Mut<'static, T>

Get a mutable reference to the value, notifying subscribers.

fn peek_state(&self) -> <UnsyncStorage as AnyStorage>::Ref<'static, T>

Read the current value without subscribing to changes.

Provided Methods§

fn set(&mut self, value: T)

Replace the current value and notify subscribers.

§Example
let mut status = use_state(|| "idle");

status.set("loading");
status.set("complete");

fn set_if_modified(&mut self, value: T)
where T: PartialEq,

Replace the value only if it differs from the current one.

This prevents unnecessary re-renders when setting the same value repeatedly.

§Example
let mut count = use_state(|| 0);

// This will update and notify subscribers
count.set_if_modified(5);

// This will do nothing (value is already 5)
count.set_if_modified(5);

fn set_if_modified_and_then(&mut self, value: T, then: impl FnOnce())
where T: PartialEq,

Replace the value if modified, then run a callback.

§Example
let mut score = use_state(|| 0);

score.set_if_modified_and_then(100, || {
    println!("High score achieved!");
});

fn with_mut( &mut self, with: impl FnOnce(<UnsyncStorage as AnyStorage>::Mut<'static, T>), )

Modify the value via a closure with a single notification.

§Example
let mut counter = use_state(|| 0);

counter.with_mut(|mut value| {
    *value += 1;
    *value *= 2;
});

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§

§

impl<T> WritableUtils<T> for State<T>
where T: 'static,

§

impl<T> WritableUtils<T> for Writable<T>
where T: 'static,

§

impl<Value, Channel> WritableUtils<Value> for Radio<Value, Channel>
where Channel: RadioChannel<Value>, Value: 'static,

§

impl<Value, SliceValue, Channel> WritableUtils<SliceValue> for RadioSliceMut<Value, SliceValue, Channel>
where Channel: RadioChannel<Value>, Value: 'static, SliceValue: 'static,