pub struct Readable<T> { /* private fields */ }
Expand description

Rust-managed Readable Svelte store.

Implementations

Creates a Readable Svelte store.

This function is only implemented for types that can be converted into JsValue. This includes all types annotated with #[wasm_bindgen]. If your type does not provide an Into<JsValue> implementation, use Readable::new_mapped instead.

Examples

Using a type that already provides an implementation of Into<JsValue>.

use svelte_store::Readable;

let store = Readable::new(0u8);

Using a type annotated with #[wasm_bindgen].

use svelte_store::Readable;
use wasm_bindgen::prelude::*;

#[derive(Clone)]
#[wasm_bindgen]
pub struct MyStruct;

let store = Readable::new(MyStruct);

Creates a new Readable Svelte store which calls its mapping fn each time the store is set, to produce a JsValue.

This method should be used whenever Readable::new cannot be, due to lacking trait compatibility.

Examples

Creating a store of Vec<u8>.

use svelte_store::Readable;
use wasm_bindgen::prelude::*;

let values = vec![7u8; 7];

let store = Readable::new_mapped(values, |values: &Vec<u8>| {
    values
        .iter()
        .cloned()
        .map(JsValue::from)
        .collect::<js_sys::Array>()
        .into()
});

Sets the value of the store, notifying all store listeners the value has changed.

Calls the provided f with a &mut T, returning whatever f returns. After this function is called, the store will be updated and all store listeners will be notified.

Trait Implementations

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Readable relies on the fact that only one instance can exist at a time to provide transparent dereferencing to the inner value. As a result, it is unsound to implement Clone. If you need shared mutability, try using Rc and RefCell.

The resulting type after dereferencing.

Dereferences the value.

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.