Struct wasm_react::Callback
source · pub struct Callback<T, U = ()> { /* private fields */ }
Expand description
This is a simplified, reference-counted wrapper around an FnMut(T) -> U
Rust closure that may be called from JS when T
and U
allow.
You can also use the callback!
macro to create a Callback
which supports clone-capturing the environment.
It only supports closures with exactly one input argument and some return
value. Memory management is handled by Rust. Whenever Rust drops all clones
of the Callback
, the closure will be dropped and the function cannot be
called from JS anymore.
Use Void
to simulate a callback with no arguments.
Implementations§
source§impl<T, U> Callback<T, U>where
T: 'static,
U: 'static,
impl<T, U> Callback<T, U>where T: 'static, U: 'static,
sourcepub fn new(f: impl FnMut(T) -> U + 'static) -> Self
pub fn new(f: impl FnMut(T) -> U + 'static) -> Self
Creates a new Callback
from a Rust closure.
sourcepub fn to_closure(&self) -> impl FnMut(T) -> U + 'static
pub fn to_closure(&self) -> impl FnMut(T) -> U + 'static
Returns a Rust closure from the callback.
sourcepub fn premap<V>(&self, f: impl FnMut(V) -> T + 'static) -> Callback<V, U>
pub fn premap<V>(&self, f: impl FnMut(V) -> T + 'static) -> Callback<V, U>
Returns a new Callback
by prepending the given closure to the callback.
sourcepub fn postmap<V>(&self, f: impl FnMut(U) -> V + 'static) -> Callback<T, V>
pub fn postmap<V>(&self, f: impl FnMut(U) -> V + 'static) -> Callback<T, V>
Returns a new Callback
by appending the given closure to the callback.
sourcepub fn as_js(&self) -> Ref<'_, JsValue>where
T: FromWasmAbi,
U: IntoWasmAbi,
pub fn as_js(&self) -> Ref<'_, JsValue>where T: FromWasmAbi, U: IntoWasmAbi,
Returns a reference to JsValue
of the callback.