Skip to main content

Callable

Trait Callable 

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

    // Provided methods
    fn into_box(self) -> BoxCallable<R, E>
       where Self: Sized + 'static { ... }
    fn into_rc(self) -> RcCallable<R, E>
       where Self: Sized + 'static { ... }
    fn into_arc(self) -> ArcCallable<R, E>
       where Self: Sized + Send + 'static { ... }
    fn into_fn(self) -> impl FnMut() -> Result<R, E>
       where Self: Sized + 'static { ... }
    fn to_box(&self) -> BoxCallable<R, E>
       where Self: Clone + Sized + 'static { ... }
    fn to_rc(&self) -> RcCallable<R, E>
       where Self: Clone + Sized + 'static { ... }
    fn to_arc(&self) -> ArcCallable<R, E>
       where Self: Clone + Send + Sized + 'static { ... }
    fn to_fn(&self) -> impl FnMut() -> Result<R, E>
       where Self: Clone + Sized + 'static { ... }
    fn into_once(self) -> BoxCallableOnce<R, E>
       where Self: Sized + 'static { ... }
    fn to_once(&self) -> BoxCallableOnce<R, E>
       where Self: Clone + Sized + 'static { ... }
    fn into_runnable(self) -> BoxRunnable<E>
       where Self: Sized + 'static { ... }
}
Expand description

A fallible, reusable zero-argument computation.

Conceptually this is the same shape as FnMut() -> Result<R, E>: call takes &mut self and returns Result<R, E>, but the API uses task-oriented naming and helpers. In this crate it aligns with Supplier of Result<R, E>—a fallible supplier—while emphasizing executable work rather than plain value production.

Choose Callable when callers need the success value R. When only success or failure matters, use Runnable, 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::Callable;

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

§Author

Haixing Hu

Required Methods§

Source

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

Executes the computation, borrowing self mutably.

§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) -> BoxCallable<R, E>
where Self: Sized + 'static,

Converts this callable into a boxed callable.

§Returns

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

Source

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

Converts this callable into an Rc callable.

§Returns

A RcCallable<R, E>.

Source

fn into_arc(self) -> ArcCallable<R, E>
where Self: Sized + Send + 'static,

Converts this callable into an Arc callable.

§Returns

An ArcCallable<R, E>.

Source

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

Converts this callable into a mutable closure.

§Returns

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

Source

fn to_box(&self) -> BoxCallable<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 BoxCallable<R, E> built from a clone of this callable.

Source

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

Converts this callable into an Rc callable without consuming self.

The method clones self and wraps the clone.

§Returns

A RcCallable<R, E>.

Source

fn to_arc(&self) -> ArcCallable<R, E>
where Self: Clone + Send + Sized + 'static,

Converts this callable into an Arc callable without consuming self.

The method clones self and wraps the clone.

§Returns

An ArcCallable<R, E>.

Source

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

Converts this callable into a mutable closure without consuming self.

The method clones self and returns a closure that executes the clone on each call.

§Returns

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

Source

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

Converts this callable into a one-time callable.

The returned callable consumes itself on each invocation.

§Returns

A BoxCallableOnce<R, E>.

Source

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

Converts this callable into a one-time callable without consuming self.

The method clones self and returns a one-time callable.

§Returns

A BoxCallableOnce<R, E>.

Source

fn into_runnable(self) -> BoxRunnable<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 BoxRunnable<E> that executes this callable and discards its success value.

Implementors§

Source§

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

Source§

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

Source§

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

Source§

impl<R, E, F> Callable<R, E> for F
where F: FnMut() -> Result<R, E>,