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 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 { ... }
}
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

Hu Haixing

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>

Examples found in repository?
examples/transformer_blanket_impl_demo.rs (line 34)
19fn test_transformer() {
20    // Test function pointer
21    fn double(x: i32) -> i32 {
22        x * 2
23    }
24    assert_eq!(double.apply(21), 42);
25    println!("✓ Function pointer test passed: double.apply(21) = 42");
26
27    // Test closure
28    let triple = |x: i32| x * 3;
29    assert_eq!(triple.apply(14), 42);
30    println!("✓ Closure test passed: triple.apply(14) = 42");
31
32    // Test conversion to BoxTransformer
33    let quad = |x: i32| x * 4;
34    let boxed = Transformer::into_box(quad);
35    assert_eq!(boxed.apply(10), 40);
36    println!("✓ into_box() test passed");
37
38    // Test conversion to RcTransformer
39    let times_five = |x: i32| x * 5;
40    let rc = Transformer::into_rc(times_five);
41    assert_eq!(rc.apply(8), 40);
42    println!("✓ into_rc() test passed");
43
44    // Test conversion to ArcTransformer
45    let times_six = |x: i32| x * 6;
46    let arc = Transformer::into_arc(times_six);
47    assert_eq!(arc.apply(7), 42);
48    println!("✓ into_arc() test passed");
49}
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>

Examples found in repository?
examples/transformer_blanket_impl_demo.rs (line 40)
19fn test_transformer() {
20    // Test function pointer
21    fn double(x: i32) -> i32 {
22        x * 2
23    }
24    assert_eq!(double.apply(21), 42);
25    println!("✓ Function pointer test passed: double.apply(21) = 42");
26
27    // Test closure
28    let triple = |x: i32| x * 3;
29    assert_eq!(triple.apply(14), 42);
30    println!("✓ Closure test passed: triple.apply(14) = 42");
31
32    // Test conversion to BoxTransformer
33    let quad = |x: i32| x * 4;
34    let boxed = Transformer::into_box(quad);
35    assert_eq!(boxed.apply(10), 40);
36    println!("✓ into_box() test passed");
37
38    // Test conversion to RcTransformer
39    let times_five = |x: i32| x * 5;
40    let rc = Transformer::into_rc(times_five);
41    assert_eq!(rc.apply(8), 40);
42    println!("✓ into_rc() test passed");
43
44    // Test conversion to ArcTransformer
45    let times_six = |x: i32| x * 6;
46    let arc = Transformer::into_arc(times_six);
47    assert_eq!(arc.apply(7), 42);
48    println!("✓ into_arc() test passed");
49}
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>

Examples found in repository?
examples/transformer_blanket_impl_demo.rs (line 46)
19fn test_transformer() {
20    // Test function pointer
21    fn double(x: i32) -> i32 {
22        x * 2
23    }
24    assert_eq!(double.apply(21), 42);
25    println!("✓ Function pointer test passed: double.apply(21) = 42");
26
27    // Test closure
28    let triple = |x: i32| x * 3;
29    assert_eq!(triple.apply(14), 42);
30    println!("✓ Closure test passed: triple.apply(14) = 42");
31
32    // Test conversion to BoxTransformer
33    let quad = |x: i32| x * 4;
34    let boxed = Transformer::into_box(quad);
35    assert_eq!(boxed.apply(10), 40);
36    println!("✓ into_box() test passed");
37
38    // Test conversion to RcTransformer
39    let times_five = |x: i32| x * 5;
40    let rc = Transformer::into_rc(times_five);
41    assert_eq!(rc.apply(8), 40);
42    println!("✓ into_rc() test passed");
43
44    // Test conversion to ArcTransformer
45    let times_six = |x: i32| x * 6;
46    let arc = Transformer::into_arc(times_six);
47    assert_eq!(arc.apply(7), 42);
48    println!("✓ into_arc() test passed");
49}
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 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);

Implementors§

Source§

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

Implement Transformer<T, R> for any type that implements Fn(T) -> R

This allows closures and function pointers to be used directly with our Transformer trait without wrapping.

§Examples

use prism3_function::Transformer;

fn double(x: i32) -> i32 { x * 2 }

assert_eq!(double.apply(21), 42);

let triple = |x: i32| x * 3;
assert_eq!(triple.apply(14), 42);

§Author

Hu Haixing

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>