Skip to main content

CallableOnce

Trait CallableOnce 

Source
pub trait CallableOnce<R, E> {
    // Required method
    fn call(self) -> Result<R, E>;

    // Provided methods
    fn into_box(self) -> BoxCallableOnce<R, E>
       where Self: Sized + 'static { ... }
    fn into_fn(self) -> impl FnOnce() -> Result<R, E>
       where Self: Sized + 'static { ... }
    fn to_box(&self) -> BoxCallableOnce<R, E>
       where Self: Clone + Sized + 'static { ... }
    fn to_fn(&self) -> impl FnOnce() -> Result<R, E>
       where Self: Clone + Sized + 'static { ... }
    fn into_runnable(self) -> BoxRunnableOnce<E>
       where Self: Sized + 'static { ... }
}
Expand description

A fallible one-time computation.

Conceptually this matches FnOnce() -> Result<R, E>: call consumes self and returns Result<R, E>, but the surface uses task-oriented naming and helpers instead of closure types. It is a semantic specialization of SupplierOnce<Result<R, E>> for executable computations and deferred tasks.

Choose CallableOnce when callers need the success value R. When only success or failure matters, use RunnableOnce, whose success type is ().

§Type Parameters

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

§Examples

use qubit_function::{CallableOnce, BoxCallableOnce};

let task = || Ok::<i32, String>(21 * 2);
assert_eq!(task.call(), Ok(42));

§Author

Haixing Hu

Required Methods§

Source

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

Executes the computation, consuming self.

§Returns

Returns Ok(R) when the computation succeeds, or Err(E) when it fails. The exact error meaning is defined by the concrete callable.

Provided Methods§

Source

fn into_box(self) -> BoxCallableOnce<R, E>
where Self: Sized + 'static,

Converts this callable into a boxed callable.

§Returns

A BoxCallableOnce<R, E> that executes this callable when call() is invoked.

Source

fn into_fn(self) -> impl FnOnce() -> Result<R, E>
where Self: Sized + 'static,

Converts this callable into a closure.

§Returns

A closure implementing FnOnce() -> Result<R, E>.

Source

fn to_box(&self) -> BoxCallableOnce<R, E>
where Self: Clone + Sized + 'static,

Converts this callable into a boxed callable without consuming self.

The method clones self and boxes the clone. Use this for cloneable callable values that need to be reused after boxing.

§Returns

A new BoxCallableOnce<R, E> built from a clone of this callable.

Source

fn to_fn(&self) -> impl FnOnce() -> Result<R, E>
where Self: Clone + Sized + 'static,

Converts this callable into a closure without consuming self.

The method clones self and returns a one-time closure that executes the clone.

§Returns

A closure implementing FnOnce() -> Result<R, E>.

Source

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

Converts this callable into a runnable by discarding the success value.

The returned runnable preserves errors and maps any Ok(R) to Ok(()).

§Returns

A BoxRunnableOnce<E> that executes this callable and discards its success value.

Implementors§

Source§

impl<F, R, E> CallableOnce<R, E> for F
where F: FnOnce() -> Result<R, E>,

Source§

impl<R, E> CallableOnce<R, E> for BoxCallableOnce<R, E>