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,

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>

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>

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<Callback<T, U>> for Callback<T, U>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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 = ()> !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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.