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§
Provided Methods§
Sourcefn into_box(self) -> BoxTransformer<T, R>where
Self: Sized + 'static,
T: 'static,
R: 'static,
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?
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}Sourcefn into_rc(self) -> RcTransformer<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,
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?
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}Sourcefn into_arc(self) -> ArcTransformer<T, R>
fn into_arc(self) -> ArcTransformer<T, R>
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?
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}Sourcefn into_fn(self) -> impl Fn(T) -> Rwhere
Self: Sized + 'static,
T: 'static,
R: 'static,
fn into_fn(self) -> impl Fn(T) -> Rwhere
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
Sourcefn to_box(&self) -> BoxTransformer<T, R>where
Self: Clone + 'static,
T: 'static,
R: 'static,
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);Sourcefn to_rc(&self) -> RcTransformer<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,
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);Sourcefn to_arc(&self) -> ArcTransformer<T, R>
fn to_arc(&self) -> ArcTransformer<T, R>
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);Sourcefn to_fn(&self) -> impl Fn(T) -> Rwhere
Self: Clone + 'static,
T: 'static,
R: 'static,
fn to_fn(&self) -> impl Fn(T) -> Rwhere
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§
impl<F, T, R> Transformer<T, R> for Fwhere
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