pub struct RcTransformer<T, R> { /* private fields */ }Expand description
RcTransformer - single-threaded transformer wrapper
A single-threaded, clonable transformer wrapper optimized for scenarios that require sharing without thread-safety overhead.
§Features
- Based on:
Rc<dyn Fn(T) -> R> - Ownership: Shared ownership via reference counting (non-atomic)
- Reusability: Can be called multiple times (each call consumes its input)
- Thread Safety: Not thread-safe (no
Send + Sync) - Clonable: Cheap cloning via
Rc::clone
§Author
Hu Haixing
Implementations§
Source§impl<T, R> RcTransformer<T, R>where
T: 'static,
R: 'static,
impl<T, R> RcTransformer<T, R>where
T: 'static,
R: 'static,
Sourcepub fn new<F>(f: F) -> Selfwhere
F: Fn(T) -> R + 'static,
pub fn new<F>(f: F) -> Selfwhere
F: Fn(T) -> R + 'static,
Creates a new RcTransformer
§Parameters
f- The closure or function to wrap
§Examples
use prism3_function::{RcTransformer, Transformer};
let double = RcTransformer::new(|x: i32| x * 2);
assert_eq!(double.transform(21), 42);Examples found in repository?
14fn main() {
15 println!("=== Transformer Demo - Type Transformation (consumes T) ===\n");
16
17 // ====================================================================
18 // Part 1: BoxTransformer - Single ownership, reusable
19 // ====================================================================
20 println!("--- BoxTransformer ---");
21 let double = BoxTransformer::new(|x: i32| x * 2);
22 println!("double.transform(21) = {}", double.transform(21));
23 println!("double.transform(42) = {}", double.transform(42));
24
25 // Identity and constant
26 let identity = BoxTransformer::<i32, i32>::identity();
27 println!("identity.transform(42) = {}", identity.transform(42));
28
29 let constant = BoxTransformer::constant("hello");
30 println!("constant.transform(123) = {}", constant.transform(123));
31 println!();
32
33 // ====================================================================
34 // Part 2: ArcTransformer - Thread-safe, cloneable
35 // ====================================================================
36 println!("--- ArcTransformer ---");
37 let arc_double = ArcTransformer::new(|x: i32| x * 2);
38 let arc_cloned = arc_double.clone();
39
40 println!("arc_double.transform(21) = {}", arc_double.transform(21));
41 println!("arc_cloned.transform(42) = {}", arc_cloned.transform(42));
42
43 // Multi-threaded usage
44 let for_thread = arc_double.clone();
45 let handle = thread::spawn(move || for_thread.transform(100));
46 println!(
47 "In main thread: arc_double.transform(50) = {}",
48 arc_double.transform(50)
49 );
50 println!("In child thread: result = {}", handle.join().unwrap());
51 println!();
52
53 // ====================================================================
54 // Part 3: RcTransformer - Single-threaded, cloneable
55 // ====================================================================
56 println!("--- RcTransformer ---");
57 let rc_double = RcTransformer::new(|x: i32| x * 2);
58 let rc_cloned = rc_double.clone();
59
60 println!("rc_double.transform(21) = {}", rc_double.transform(21));
61 println!("rc_cloned.transform(42) = {}", rc_cloned.transform(42));
62 println!();
63
64 // ====================================================================
65 // Part 4: Practical Examples
66 // ====================================================================
67 println!("=== Practical Examples ===\n");
68
69 // Example 1: String transformation
70 println!("--- String Transformation ---");
71 let to_upper = BoxTransformer::new(|s: String| s.to_uppercase());
72 println!(
73 "to_upper.transform('hello') = {}",
74 to_upper.transform("hello".to_string())
75 );
76 println!(
77 "to_upper.transform('world') = {}",
78 to_upper.transform("world".to_string())
79 );
80 println!();
81
82 // Example 2: Type conversion pipeline
83 println!("--- Type Conversion Pipeline ---");
84 let parse_int = BoxTransformer::new(|s: String| s.parse::<i32>().unwrap_or(0));
85 let double_int = BoxTransformer::new(|x: i32| x * 2);
86 let to_string = BoxTransformer::new(|x: i32| x.to_string());
87
88 let pipeline = parse_int.and_then(double_int).and_then(to_string);
89 println!(
90 "pipeline.transform('21') = {}",
91 pipeline.transform("21".to_string())
92 );
93 println!();
94
95 // Example 3: Shared transformation logic
96 println!("--- Shared Transformation Logic ---");
97 let square = ArcTransformer::new(|x: i32| x * x);
98
99 // Can be shared across different parts of the program
100 let transformer1 = square.clone();
101 let transformer2 = square.clone();
102
103 println!("transformer1.transform(5) = {}", transformer1.transform(5));
104 println!("transformer2.transform(7) = {}", transformer2.transform(7));
105 println!("square.transform(3) = {}", square.transform(3));
106 println!();
107
108 // Example 4: Transformer registry
109 println!("--- Transformer Registry ---");
110 let mut transformers: HashMap<String, RcTransformer<i32, String>> = HashMap::new();
111
112 transformers.insert(
113 "double".to_string(),
114 RcTransformer::new(|x: i32| format!("Doubled: {}", x * 2)),
115 );
116 transformers.insert(
117 "square".to_string(),
118 RcTransformer::new(|x: i32| format!("Squared: {}", x * x)),
119 );
120
121 if let Some(transformer) = transformers.get("double") {
122 println!("Transformer 'double': {}", transformer.transform(7));
123 }
124 if let Some(transformer) = transformers.get("square") {
125 println!("Transformer 'square': {}", transformer.transform(7));
126 }
127 println!();
128
129 // ====================================================================
130 // Part 5: Trait Usage
131 // ====================================================================
132 println!("=== Trait Usage ===\n");
133
134 fn apply_transformer<F: Transformer<i32, String>>(f: &F, x: i32) -> String {
135 f.transform(x)
136 }
137
138 let to_string = BoxTransformer::new(|x: i32| format!("Value: {}", x));
139 println!("Via trait: {}", apply_transformer(&to_string, 42));
140
141 println!("\n=== Demo Complete ===");
142}Sourcepub fn identity() -> RcTransformer<T, T>
pub fn identity() -> RcTransformer<T, T>
Creates an identity transformer
§Examples
use prism3_function::{RcTransformer, Transformer};
let identity = RcTransformer::<i32, i32>::identity();
assert_eq!(identity.transform(42), 42);Sourcepub fn and_then<S, F>(&self, after: F) -> RcTransformer<T, S>where
S: 'static,
F: Transformer<R, S> + 'static,
pub fn and_then<S, F>(&self, after: F) -> RcTransformer<T, S>where
S: 'static,
F: Transformer<R, S> + 'static,
Chain composition - applies self first, then after
Creates a new transformer that applies this transformer first, then applies the after transformer to the result. Uses &self, so original transformer remains usable.
§Type Parameters
S- The output type of the after transformerF- The type of the after transformer (must implement Transformer<R, S>)
§Parameters
after- The transformer to apply after self. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original transformer, clone it first (if it implementsClone). Can be:- A closure:
|x: R| -> S - A function pointer:
fn(R) -> S - A
BoxTransformer<R, S> - An
RcTransformer<R, S>(will be moved) - An
ArcTransformer<R, S> - Any type implementing
Transformer<R, S>
- A closure:
§Returns
A new RcTransformer representing the composition
§Examples
§Direct value passing (ownership transfer)
use prism3_function::{RcTransformer, Transformer};
let double = RcTransformer::new(|x: i32| x * 2);
let to_string = RcTransformer::new(|x: i32| x.to_string());
// to_string is moved here
let composed = double.and_then(to_string);
// Original double transformer still usable (uses &self)
assert_eq!(double.transform(21), 42);
assert_eq!(composed.transform(21), "42");
// to_string.transform(5); // Would not compile - moved§Preserving original with clone
use prism3_function::{RcTransformer, Transformer};
let double = RcTransformer::new(|x: i32| x * 2);
let to_string = RcTransformer::new(|x: i32| x.to_string());
// Clone to preserve original
let composed = double.and_then(to_string.clone());
assert_eq!(composed.transform(21), "42");
// Both originals still usable
assert_eq!(double.transform(21), 42);
assert_eq!(to_string.transform(5), "5");Sourcepub fn compose<S, F>(&self, before: F) -> RcTransformer<S, R>where
S: 'static,
F: Transformer<S, T> + 'static,
pub fn compose<S, F>(&self, before: F) -> RcTransformer<S, R>where
S: 'static,
F: Transformer<S, T> + 'static,
Reverse composition - applies before first, then self
Creates a new transformer that applies the before transformer first, then applies this transformer to the result. Uses &self, so original transformer remains usable.
§Type Parameters
S- The input type of the before transformerF- The type of the before transformer (must implement Transformer<S, T>)
§Parameters
before- The transformer to apply before self. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original transformer, clone it first (if it implementsClone). Can be:- A closure:
|x: S| -> T - A function pointer:
fn(S) -> T - A
BoxTransformer<S, T> - An
RcTransformer<S, T>(will be moved) - An
ArcTransformer<S, T> - Any type implementing
Transformer<S, T>
- A closure:
§Returns
A new RcTransformer representing the composition
§Examples
§Direct value passing (ownership transfer)
use prism3_function::{RcTransformer, Transformer};
let double = RcTransformer::new(|x: i32| x * 2);
let add_one = RcTransformer::new(|x: i32| x + 1);
// add_one is moved here
let composed = double.compose(add_one);
assert_eq!(composed.transform(5), 12); // (5 + 1) * 2
// add_one.transform(3); // Would not compile - moved§Preserving original with clone
use prism3_function::{RcTransformer, Transformer};
let double = RcTransformer::new(|x: i32| x * 2);
let add_one = RcTransformer::new(|x: i32| x + 1);
// Clone to preserve original
let composed = double.compose(add_one.clone());
assert_eq!(composed.transform(5), 12); // (5 + 1) * 2
// Both originals still usable
assert_eq!(double.transform(10), 20);
assert_eq!(add_one.transform(3), 4);Sourcepub fn when<P>(self, predicate: P) -> RcConditionalTransformer<T, R>where
P: Predicate<T> + 'static,
pub fn when<P>(self, predicate: P) -> RcConditionalTransformer<T, R>where
P: Predicate<T> + 'static,
Creates a conditional transformer (single-threaded shared version)
Returns a transformer that only executes when a predicate is satisfied.
You must call or_else() to provide an alternative transformer.
§Parameters
predicate- The condition to check. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original predicate, clone it first (if it implementsClone). Can be:- A closure:
|x: &T| -> bool - A function pointer:
fn(&T) -> bool - A
BoxPredicate<T> - An
RcPredicate<T> - An
ArcPredicate<T> - Any type implementing
Predicate<T>
- A closure:
§Returns
Returns RcConditionalTransformer<T, R>
§Examples
§Basic usage with or_else
use prism3_function::{Transformer, RcTransformer};
let double = RcTransformer::new(|x: i32| x * 2);
let identity = RcTransformer::<i32, i32>::identity();
let conditional = double.when(|x: &i32| *x > 0).or_else(identity);
let conditional_clone = conditional.clone();
assert_eq!(conditional.transform(5), 10);
assert_eq!(conditional_clone.transform(-5), -5);§Preserving predicate with clone
use prism3_function::{Transformer, RcTransformer, RcPredicate};
let double = RcTransformer::new(|x: i32| x * 2);
let is_positive = RcPredicate::new(|x: &i32| *x > 0);
// Clone to preserve original predicate
let conditional = double.when(is_positive.clone())
.or_else(RcTransformer::identity());
assert_eq!(conditional.transform(5), 10);
// Original predicate still usable
assert!(is_positive.test(&3));Source§impl<T, R> RcTransformer<T, R>where
T: 'static,
R: Clone + 'static,
impl<T, R> RcTransformer<T, R>where
T: 'static,
R: Clone + 'static,
Sourcepub fn constant(value: R) -> RcTransformer<T, R>
pub fn constant(value: R) -> RcTransformer<T, R>
Creates a constant transformer
§Examples
use prism3_function::{RcTransformer, Transformer};
let constant = RcTransformer::constant("hello");
assert_eq!(constant.transform(123), "hello");