Skip to main content

BoxCallable

Struct BoxCallable 

Source
pub struct BoxCallable<R, E> { /* private fields */ }
Expand description

Box-based one-time callable.

BoxCallable<R, E> stores a Box<dyn FnOnce() -> Result<R, E>> and can be executed only once. It is the boxed concrete implementation of Callable.

§Type Parameters

  • R - The success value returned by the computation.
  • E - The error value returned when the computation fails.

§Examples

use qubit_function::{BoxCallable, Callable};

let task = BoxCallable::new(|| Ok::<i32, String>(42));
assert_eq!(task.call(), Ok(42));

§Author

Haixing Hu

Implementations§

Source§

impl<R, E> BoxCallable<R, E>

Source

pub fn new<F>(function: F) -> Self
where F: FnOnce() -> Result<R, E> + 'static,

Creates a new boxed callable.

§Parameters
  • function - The one-time closure executed by this callable.
§Returns

A new unnamed BoxCallable<R, E>.

Source

pub fn new_with_name<F>(name: &str, function: F) -> Self
where F: FnOnce() -> Result<R, E> + 'static,

Creates a new named boxed callable.

§Parameters
  • name - Name used by Debug and Display.
  • function - The one-time closure executed by this callable.
§Returns

A new named BoxCallable<R, E>.

Source

pub fn new_with_optional_name<F>(function: F, name: Option<String>) -> Self
where F: FnOnce() -> Result<R, E> + 'static,

Creates a new boxed callable with an optional name.

§Parameters
  • function - The one-time closure executed by this callable.
  • name - Optional name used by Debug and Display.
§Returns

A new BoxCallable<R, E>.

Source

pub fn from_supplier<S>(supplier: S) -> Self
where S: SupplierOnce<Result<R, E>> + 'static,

Creates a boxed callable from a one-time supplier.

This is an explicit bridge from SupplierOnce<Result<R, E>> to Callable<R, E>.

§Parameters
  • supplier - The supplier that produces the callable result.
§Returns

A new BoxCallable<R, E>.

Source

pub fn name(&self) -> Option<&str>

Gets the optional callable name.

§Returns

Returns Some(&str) if a name was set, or None otherwise.

Source

pub fn set_name(&mut self, name: &str)

Sets the callable name.

§Parameters
  • name - The new name.
Source

pub fn clear_name(&mut self)

Clears the callable name.

Source

pub fn map<U, M>(self, mapper: M) -> BoxCallable<U, E>
where M: FnOnce(R) -> U + 'static, R: 'static, E: 'static,

Maps the success value of this callable.

§Parameters
  • mapper - Function that transforms the success value.
§Returns

A new callable that applies mapper when this callable succeeds.

Source

pub fn map_err<E2, M>(self, mapper: M) -> BoxCallable<R, E2>
where M: FnOnce(E) -> E2 + 'static, R: 'static, E: 'static,

Maps the error value of this callable.

§Parameters
  • mapper - Function that transforms the error value.
§Returns

A new callable that applies mapper when this callable fails.

Source

pub fn and_then<U, N>(self, next: N) -> BoxCallable<U, E>
where N: FnOnce(R) -> Result<U, E> + 'static, R: 'static, E: 'static,

Chains another fallible computation after this callable succeeds.

§Parameters
  • next - Function that receives the success value and returns the next result.
§Returns

A new callable that runs next only when this callable succeeds.

Trait Implementations§

Source§

impl<R, E> Callable<R, E> for BoxCallable<R, E>

Source§

fn call(self) -> Result<R, E>

Executes the boxed callable.

Source§

fn into_box(self) -> BoxCallable<R, E>

Returns this boxed callable without re-boxing it.

Source§

fn into_fn(self) -> impl FnOnce() -> Result<R, E>

Extracts the underlying one-time closure.

Source§

fn into_runnable(self) -> BoxRunnable<E>
where Self: Sized + 'static,

Converts this boxed callable into a boxed runnable while preserving its name.

Source§

impl<R, E> Debug for BoxCallable<R, E>

Source§

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

Formats this boxed callable for debugging.

Source§

impl<R, E> Display for BoxCallable<R, E>

Source§

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

Formats this boxed callable for display.

Source§

impl<R, E> SupplierOnce<Result<R, E>> for BoxCallable<R, E>

Source§

fn get(self) -> Result<R, E>

Executes the boxed callable as a one-time supplier of Result<R, E>.

Source§

fn into_box(self) -> BoxSupplierOnce<T>
where Self: Sized + 'static,

Converts to BoxSupplierOnce. Read more
Source§

fn into_fn(self) -> impl FnOnce() -> T
where Self: Sized + 'static,

Converts the supplier to a Box<dyn FnOnce() -> T>. Read more

Auto Trait Implementations§

§

impl<R, E> Freeze for BoxCallable<R, E>

§

impl<R, E> !RefUnwindSafe for BoxCallable<R, E>

§

impl<R, E> !Send for BoxCallable<R, E>

§

impl<R, E> !Sync for BoxCallable<R, E>

§

impl<R, E> Unpin for BoxCallable<R, E>

§

impl<R, E> UnsafeUnpin for BoxCallable<R, E>

§

impl<R, E> !UnwindSafe for BoxCallable<R, E>

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.