Function

Trait Function 

Source
pub trait Function<T, R> {
    // Required method
    fn apply(&self, t: &T) -> R;

    // Provided methods
    fn into_box(self) -> BoxFunction<T, R>
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn into_rc(self) -> RcFunction<T, R>
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn into_arc(self) -> ArcFunction<T, R>
       where Self: Sized + Send + Sync + 'static,
             T: Send + Sync + 'static,
             R: 'static { ... }
    fn into_fn(self) -> impl Fn(&T) -> R
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn into_once(self) -> BoxFunctionOnce<T, R>
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_box(&self) -> BoxFunction<T, R>
       where Self: Clone + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_rc(&self) -> RcFunction<T, R>
       where Self: Clone + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_arc(&self) -> ArcFunction<T, R>
       where Self: Clone + Send + Sync + 'static,
             T: Send + Sync + 'static,
             R: Send + Sync + 'static { ... }
    fn to_fn(&self) -> impl Fn(&T) -> R
       where Self: Clone + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_once(&self) -> BoxFunctionOnce<T, R>
       where Self: Clone + 'static,
             T: 'static,
             R: 'static { ... }
}
Expand description

Function trait - computes output from input reference

Defines the behavior of a function: computing a value of type R from a reference to type T without consuming the input. This is analogous to Fn(&T) -> R in Rust’s standard library, similar to Java’s Function<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 to produce an output value

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

The computed output value

Provided Methods§

Source

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

Converts to BoxFunction

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

§Default Implementation

The default implementation wraps self in a Box and creates a BoxFunction. Types can override this method to provide more efficient conversions.

§Returns

Returns BoxFunction<T, R>

Source

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

Converts to RcFunction

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

§Default Implementation

The default implementation wraps self in an Rc and creates an RcFunction. Types can override this method to provide more efficient conversions.

§Returns

Returns RcFunction<T, R>

Source

fn into_arc(self) -> ArcFunction<T, R>
where Self: Sized + Send + Sync + 'static, T: Send + Sync + 'static, R: 'static,

Converts to ArcFunction

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

§Default Implementation

The default implementation wraps self in an Arc and creates an ArcFunction. Types can override this method to provide more efficient conversions.

§Returns

Returns ArcFunction<T, R>

Source

fn into_fn(self) -> impl Fn(&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.

§Default Implementation

The default implementation creates a closure that captures self and calls its transform method. Types can override this method to provide more efficient conversions.

§Returns

Returns a closure that implements Fn(&T) -> R

Source

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

Converts to FunctionOnce

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

Converts a reusable function to a one-time function that consumes itself on use. This enables passing Function to functions that require FunctionOnce.

§Returns

Returns a BoxFunctionOnce<T, R>

§Examples

fn takes_once<F: FunctionOnce<i32, i32>>(func: F, value: &i32) -> i32 {
    func.apply(value)
}

let func = BoxFunction::new(|x: &i32| x * 2);
let result = takes_once(func.into_once(), &5);
assert_eq!(result, 10);
Source

fn to_box(&self) -> BoxFunction<T, R>
where Self: Clone + 'static, T: 'static, R: 'static,

Converts to BoxFunction without consuming self

📌 Borrows &self: The original function remains usable after calling this method.

§Default Implementation

The default implementation creates a new BoxFunction that captures a reference-counted clone. Types implementing Clone can override this method to provide more efficient conversions.

§Returns

Returns BoxFunction<T, R>

§Examples
use prism3_function::{ArcFunction, Function};

let double = ArcFunction::new(|x: i32| x * 2);
let boxed = double.to_box();

// Original function still usable
assert_eq!(double.apply(21), 42);
assert_eq!(boxed.apply(21), 42);
Source

fn to_rc(&self) -> RcFunction<T, R>
where Self: Clone + 'static, T: 'static, R: 'static,

Converts to RcFunction without consuming self

📌 Borrows &self: The original function remains usable after calling this method.

§Default Implementation

The default implementation creates a new RcFunction that captures a reference-counted clone. Types implementing Clone can override this method to provide more efficient conversions.

§Returns

Returns RcFunction<T, R>

§Examples
use prism3_function::{ArcFunction, Function};

let double = ArcFunction::new(|x: i32| x * 2);
let rc = double.to_rc();

// Original function still usable
assert_eq!(double.apply(21), 42);
assert_eq!(rc.apply(21), 42);
Source

fn to_arc(&self) -> ArcFunction<T, R>
where Self: Clone + Send + Sync + 'static, T: Send + Sync + 'static, R: Send + Sync + 'static,

Converts to ArcFunction without consuming self

📌 Borrows &self: The original function remains usable after calling this method.

§Default Implementation

The default implementation creates a new ArcFunction that captures a reference-counted clone. Types implementing Clone can override this method to provide more efficient conversions.

§Returns

Returns ArcFunction<T, R>

§Examples
use prism3_function::{ArcFunction, Function};

let double = ArcFunction::new(|x: i32| x * 2);
let arc = double.to_arc();

// Original function still usable
assert_eq!(double.apply(21), 42);
assert_eq!(arc.apply(21), 42);
Source

fn to_fn(&self) -> impl Fn(&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 transform method. Types can override this method to provide more efficient conversions.

§Returns

Returns a closure that implements Fn(&T) -> R

§Examples
use prism3_function::{ArcFunction, Function};

let double = ArcFunction::new(|x: i32| x * 2);
let closure = double.to_fn();

// Original function still usable
assert_eq!(double.apply(21), 42);
assert_eq!(closure(21), 42);
Source

fn to_once(&self) -> BoxFunctionOnce<T, R>
where Self: Clone + 'static, T: 'static, R: 'static,

Convert to FunctionOnce without consuming self

⚠️ Requires Clone: This method requires Self to implement Clone. Clones the current function and converts the clone to a one-time function.

§Returns

Returns a BoxFunctionOnce<T, R>

§Examples

fn takes_once<F: FunctionOnce<i32, i32>>(func: F, value: &i32) -> i32 {
    func.apply(value)
}

let func = BoxFunction::new(|x: &i32| x * 2);
let result = takes_once(func.to_once(), &5);
assert_eq!(result, 10);

Implementors§

Source§

impl<T, R> Function<T, R> for ArcFunction<T, R>

Source§

impl<T, R> Function<T, R> for BoxFunction<T, R>

Source§

impl<T, R> Function<T, R> for RcFunction<T, R>

Source§

impl<T, R, F> Function<T, R> for F
where F: Fn(&T) -> R, T: 'static, R: 'static,