pub trait FunctionOnce<T, R> {
// Required method
fn apply(self, t: &T) -> R;
// Provided methods
fn into_box(self) -> BoxFunctionOnce<T, R>
where Self: Sized + 'static,
T: 'static,
R: 'static { ... }
fn into_fn(self) -> impl FnOnce(&T) -> R
where Self: Sized + 'static,
T: 'static,
R: 'static { ... }
fn to_box(&self) -> BoxFunctionOnce<T, R>
where Self: Clone + 'static,
T: 'static,
R: 'static { ... }
fn to_fn(&self) -> impl FnOnce(&T) -> R
where Self: Clone + 'static,
T: 'static,
R: 'static { ... }
}Expand description
FunctionOnce trait - consuming function that takes ownership
Defines the behavior of a consuming function: computing a value of
type R from a reference to type T by taking ownership of self.
This trait is analogous to FnOnce(&T) -> R.
§Type Parameters
T- The type of the input value (borrowed)R- The type of the output value
§Author
Haixing Hu
Required Methods§
Provided Methods§
Sourcefn into_box(self) -> BoxFunctionOnce<T, R>where
Self: Sized + 'static,
T: 'static,
R: 'static,
fn into_box(self) -> BoxFunctionOnce<T, R>where
Self: Sized + 'static,
T: 'static,
R: 'static,
Sourcefn into_fn(self) -> impl FnOnce(&T) -> Rwhere
Self: Sized + 'static,
T: 'static,
R: 'static,
fn into_fn(self) -> impl FnOnce(&T) -> Rwhere
Self: Sized + 'static,
T: 'static,
R: 'static,
Converts function to a closure
⚠️ Consumes self: The original function becomes unavailable
after calling this method.
§Returns
Returns a closure that implements FnOnce(&T) -> R
§Examples
use prism3_function::FunctionOnce;
let double = |x: &i32| x * 2;
let func = double.into_fn();
assert_eq!(func(&21), 42);Sourcefn to_box(&self) -> BoxFunctionOnce<T, R>where
Self: Clone + 'static,
T: 'static,
R: 'static,
fn to_box(&self) -> BoxFunctionOnce<T, R>where
Self: Clone + 'static,
T: 'static,
R: 'static,
Converts to BoxFunctionOnce without consuming self
📌 Borrows &self: The original function remains usable
after calling this method.
§Default Implementation
The default implementation creates a new BoxFunctionOnce that
captures a clone. Types implementing Clone can override this method
to provide more efficient conversions.
§Returns
Returns BoxFunctionOnce<T, R>
§Examples
use prism3_function::FunctionOnce;
let double = |x: &i32| x * 2;
let boxed = double.to_box();
assert_eq!(boxed.apply(&21), 42);Sourcefn to_fn(&self) -> impl FnOnce(&T) -> Rwhere
Self: Clone + 'static,
T: 'static,
R: 'static,
fn to_fn(&self) -> impl FnOnce(&T) -> Rwhere
Self: Clone + 'static,
T: 'static,
R: 'static,
Converts function to a closure without consuming self
📌 Borrows &self: The original function remains usable
after calling this method.
§Default Implementation
The default implementation creates a closure that captures a
clone of self and calls its apply method. Types can
override this method to provide more efficient conversions.
§Returns
Returns a closure that implements FnOnce(&T) -> R
§Examples
use prism3_function::FunctionOnce;
let double = |x: &i32| x * 2;
let func = double.to_fn();
assert_eq!(func(&21), 42);