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 clones!
helper macro to
clone-capture the environment more ergonomically.
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>where
V: 'static,
pub fn premap<V>(&self, f: impl FnMut(V) -> T + 'static) -> Callback<V, U>where
V: 'static,
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>where
V: 'static,
pub fn postmap<V>(&self, f: impl FnMut(U) -> V + 'static) -> Callback<T, V>where
V: 'static,
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.