FunctionOnce

Trait FunctionOnce 

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

Source

fn apply(self, t: &T) -> R

Applies the function to the input reference, consuming self

§Parameters
  • t - Reference to the input value
§Returns

The computed output value

Provided Methods§

Source

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

Converts to BoxFunctionOnce

⚠️ Consumes self: The original function becomes unavailable after calling this method.

§Returns

Returns BoxFunctionOnce<T, R>

§Examples
use prism3_function::FunctionOnce;

let double = |x: &i32| x * 2;
let boxed = double.into_box();
assert_eq!(boxed.apply(&21), 42);
Source

fn into_fn(self) -> impl FnOnce(&T) -> R
where 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);
Source

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);
Source

fn to_fn(&self) -> impl FnOnce(&T) -> R
where 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);

Implementors§

Source§

impl<F, T, R> FunctionOnce<T, R> for F
where F: FnOnce(&T) -> R,

Source§

impl<T, R> FunctionOnce<T, R> for BoxFunctionOnce<T, R>