Struct svelte_store::Readable
source · [−]pub struct Readable<T> { /* private fields */ }
Expand description
Rust-managed Readable
Svelte store.
Implementations
sourceimpl<T: 'static> Readable<T>
impl<T: 'static> Readable<T>
sourcepub fn new(initial_value: T) -> Self where
T: Clone + Into<JsValue>,
pub fn new(initial_value: T) -> Self where
T: Clone + Into<JsValue>,
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);
sourcepub fn new_mapped<F>(initial_value: T, mapping_fn: F) -> Self where
F: FnMut(&T) -> JsValue + 'static,
pub fn new_mapped<F>(initial_value: T, mapping_fn: F) -> Self where
F: FnMut(&T) -> JsValue + 'static,
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()
});
Trait Implementations
Auto Trait Implementations
impl<T> !RefUnwindSafe for Readable<T>
impl<T> !Send for Readable<T>
impl<T> !Sync for Readable<T>
impl<T> Unpin for Readable<T>
impl<T> !UnwindSafe for Readable<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more