Skip to main content

Callback

Struct Callback 

Source
pub struct Callback<IN, OUT = ()> { /* private fields */ }
Expand description

Universal callback wrapper inspired by Yew’s Callback system.

This provides a type-safe way to pass function callbacks between components, similar to React’s event handlers and Yew’s callback system.

Implementations§

Source§

impl<IN, OUT> Callback<IN, OUT>

Source

pub fn new<F>(func: F) -> Self
where F: Fn(IN) -> OUT + Send + Sync + 'static,

Create a new callback from a function

Source

pub fn emit(&self, input: IN) -> OUT

Emit the callback with the given input value

Source

pub fn reform<F, T>(&self, func: F) -> Callback<T, OUT>
where F: Fn(T) -> IN + Send + Sync + 'static, IN: 'static, OUT: 'static,

Create a reformed callback that transforms the input before calling this callback

Source

pub fn filter_reform<F, T>(&self, func: F) -> Callback<T, Option<OUT>>
where F: Fn(T) -> Option<IN> + Send + Sync + 'static, IN: 'static, OUT: 'static,

Create a reformed callback that optionally transforms the input

Source§

impl<IN> Callback<IN>

Source

pub fn noop() -> Self

Create a no-op callback that does nothing when called Useful for optional callbacks or default values

Source§

impl<IN, OUT> Callback<IN, OUT>
where IN: 'static,

Source

pub fn from_fn<F>(func: F) -> Self
where F: Fn(IN) -> OUT + Send + Sync + 'static,

Create a callback from a closure with explicit type annotation This helps with type inference in complex scenarios

Source

pub fn constant(value: OUT) -> Self
where OUT: Clone + Send + Sync + 'static,

Create a callback that ignores its input and returns a constant value

Source

pub fn debug() -> Self
where IN: Debug, OUT: Default,

Create a callback that just prints its input (useful for debugging)

Source

pub fn then<F, NextOut>(self, next: F) -> Callback<IN, NextOut>
where F: Fn(OUT) -> NextOut + Send + Sync + 'static, OUT: 'static,

Chain this callback with another callback The output of this callback becomes the input of the next

Source

pub fn map<F, NewOut>(self, mapper: F) -> Callback<IN, NewOut>
where F: Fn(OUT) -> NewOut + Send + Sync + 'static, OUT: 'static,

Map the output of this callback to a different type

Source

pub fn filter<F>(self, predicate: F) -> Callback<IN, Option<OUT>>
where F: Fn(&IN) -> bool + Send + Sync + 'static, IN: Clone + 'static, OUT: 'static,

Create a callback that calls this callback only if a condition is met

Source

pub fn catch_unwind(self) -> Callback<IN, Result<OUT, String>>
where IN: 'static, OUT: 'static,

Create a callback that catches panics and returns a Result

Source§

impl<IN, OUT> Callback<IN, OUT>
where IN: 'static,

Source

pub fn always(value: OUT) -> Self
where OUT: Clone + Send + Sync + 'static,

Create a callback that always returns the same value, ignoring input

Source

pub fn from_mut<F>(func: F) -> Self
where F: FnMut(IN) -> OUT + Send + Sync + 'static,

Create a callback from a mutable closure using interior mutability

Trait Implementations§

Source§

impl<IN, OUT> Clone for Callback<IN, OUT>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<IN, OUT> Debug for Callback<IN, OUT>

Source§

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

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

impl<IN, OUT> Default for Callback<IN, OUT>
where OUT: Default,

Source§

fn default() -> Self

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

impl<IN, OUT> From<Arc<dyn Fn(IN) -> OUT + Send + Sync>> for Callback<IN, OUT>

Convert from Arc<dyn Fn> for shared callbacks

Source§

fn from(func: Arc<dyn Fn(IN) -> OUT + Send + Sync>) -> Self

Converts to this type from the input type.
Source§

impl<IN, OUT, F> From<F> for Callback<IN, OUT>
where F: Fn(IN) -> OUT + Send + Sync + 'static,

Source§

fn from(func: F) -> Self

Converts to this type from the input type.
Source§

impl<IN> From<Option<Callback<IN>>> for Callback<IN>

Convert from Option<Callback> - None becomes a noop callback

Source§

fn from(opt: Option<Callback<IN>>) -> Self

Converts to this type from the input type.
Source§

impl<IN, OUT> IntoCallback<IN, OUT> for Callback<IN, OUT>

Source§

fn into_callback(self) -> Callback<IN, OUT>

Source§

impl<IN, OUT> PartialEq for Callback<IN, OUT>

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<IN, OUT = ()> !RefUnwindSafe for Callback<IN, OUT>

§

impl<IN, OUT = ()> !UnwindSafe for Callback<IN, OUT>

§

impl<IN, OUT> Freeze for Callback<IN, OUT>
where Arc<dyn Fn(IN) -> OUT + Send + Sync>: Freeze,

§

impl<IN, OUT> Send for Callback<IN, OUT>
where Arc<dyn Fn(IN) -> OUT + Send + Sync>: Send,

§

impl<IN, OUT> Sync for Callback<IN, OUT>
where Arc<dyn Fn(IN) -> OUT + Send + Sync>: Sync,

§

impl<IN, OUT> Unpin for Callback<IN, OUT>
where Arc<dyn Fn(IN) -> OUT + Send + Sync>: Unpin,

§

impl<IN, OUT> UnsafeUnpin for Callback<IN, OUT>
where Arc<dyn Fn(IN) -> OUT + Send + Sync>: UnsafeUnpin,

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<F, IN, OUT> IntoCallbackProp<Callback<IN, OUT>> for F
where F: IntoCallback<IN, OUT>,

Source§

impl<F, IN, OUT> IntoCallbackProp<Option<Callback<IN, OUT>>> for F
where F: IntoCallback<IN, OUT>,

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more