Transformer

Trait Transformer 

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

    // Provided methods
    fn into_box(self) -> BoxTransformer<T, R>
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn into_rc(self) -> RcTransformer<T, R>
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn into_arc(self) -> ArcTransformer<T, R>
       where Self: Sized + Send + Sync + 'static,
             T: Send + Sync + 'static,
             R: Send + Sync + 'static { ... }
    fn into_fn(self) -> impl Fn(T) -> R
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn into_once(self) -> BoxTransformerOnce<T, R>
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_box(&self) -> BoxTransformer<T, R>
       where Self: Clone + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_rc(&self) -> RcTransformer<T, R>
       where Self: Clone + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_arc(&self) -> ArcTransformer<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) -> BoxTransformerOnce<T, R>
       where Self: Clone + 'static,
             T: 'static,
             R: 'static { ... }
}
Expand description

Transformer trait - transforms values from type T to type R

Defines the behavior of a transformation: converting a value of type T to a value of type R by consuming the input. This is analogous to Fn(T) -> R in Rust’s standard library.

§Type Parameters

  • T - The type of the input value (consumed)
  • R - The type of the output value

§Author

Haixing Hu

Required Methods§

Source

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

Applies the transformation to the input value to produce an output value

§Parameters
  • input - The input value to transform (consumed)
§Returns

The transformed output value

Provided Methods§

Source

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

Converts to BoxTransformer

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

§Default Implementation

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

§Returns

Returns BoxTransformer<T, R>

Source

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

Converts to RcTransformer

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

§Default Implementation

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

§Returns

Returns RcTransformer<T, R>

Source

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

Converts to ArcTransformer

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

§Default Implementation

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

§Returns

Returns ArcTransformer<T, R>

Source

fn into_fn(self) -> impl Fn(T) -> R
where Self: Sized + 'static, T: 'static, R: 'static,

Converts transformer to a closure

⚠️ Consumes self: The original transformer 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) -> BoxTransformerOnce<T, R>
where Self: Sized + 'static, T: 'static, R: 'static,

Converts to BoxTransformerOnce.

This method has a default implementation that wraps the transformer in a BoxTransformerOnce. Custom implementations can override this method for optimization purposes.

§Returns

A new BoxTransformerOnce<T, R> instance

§Examples
use prism3_function::Transformer;

let closure = |x: i32| x * 2;
let once = closure.into_once();
assert_eq!(once.apply(5), 10);
Source

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

Converts to BoxTransformer without consuming self

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

§Default Implementation

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

§Returns

Returns BoxTransformer<T, R>

§Examples
use prism3_function::{ArcTransformer, Transformer};

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

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

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

Converts to RcTransformer without consuming self

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

§Default Implementation

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

§Returns

Returns RcTransformer<T, R>

§Examples
use prism3_function::{ArcTransformer, Transformer};

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

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

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

Converts to ArcTransformer without consuming self

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

§Default Implementation

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

§Returns

Returns ArcTransformer<T, R>

§Examples
use prism3_function::{ArcTransformer, Transformer};

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

// Original transformer 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 transformer to a closure without consuming self

📌 Borrows &self: The original transformer 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::{ArcTransformer, Transformer};

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

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

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

Converts to BoxTransformerOnce without consuming self

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

§Returns

Returns a BoxTransformerOnce<T, R>

Implementors§

Source§

impl<T, R> Transformer<T, R> for ArcTransformer<T, R>

Source§

impl<T, R> Transformer<T, R> for BoxTransformer<T, R>

Source§

impl<T, R> Transformer<T, R> for RcTransformer<T, R>

Source§

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