Struct 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 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,

Source

pub fn new(f: impl FnMut(T) -> U + 'static) -> Self

Creates a new Callback from a Rust closure.

Source

pub fn to_closure(&self) -> impl FnMut(T) -> U + 'static

Returns a Rust closure from the callback.

Source

pub fn call(&self, arg: T) -> U

Calls the callback with the given argument.

Source

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.

Source

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.

Source

pub fn as_js(&self) -> Ref<'_, JsValue>
where T: FromWasmAbi, U: IntoWasmAbi,

Returns a reference to JsValue of the callback.

Source§

impl<T: 'static> Callback<T>

Source

pub fn noop() -> Self

Returns a new Callback that does nothing.

Trait Implementations§

Source§

impl<T, U> Clone for Callback<T, U>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, U> Debug for Callback<T, U>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, U> Default for Callback<T, U>
where T: 'static, U: Default + 'static,

Source§

fn default() -> Self

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

impl<F, T, U> From<F> for Callback<T, U>
where F: FnMut(T) -> U + 'static, T: 'static, U: 'static,

Source§

fn from(value: F) -> Self

Converts to this type from the input type.
Source§

impl<T, U> PartialEq for Callback<T, U>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U> Eq for Callback<T, U>

Auto Trait Implementations§

§

impl<T, U> Freeze for Callback<T, U>

§

impl<T, U = ()> !RefUnwindSafe for Callback<T, U>

§

impl<T, U = ()> !Send for Callback<T, U>

§

impl<T, U = ()> !Sync for Callback<T, U>

§

impl<T, U> Unpin for Callback<T, U>

§

impl<T, U = ()> !UnwindSafe for Callback<T, U>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.