Skip to main content

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

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/transformers/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}
More examples
Hide additional examples
examples/transformers/transformer_once_demo.rs (line 38)
27fn main() {
28    println!("=== TransformerOnce Demo ===\n");
29
30    // BoxTransformer TransformerOnce demonstration
31    println!("1. BoxTransformer TransformerOnce demonstration:");
32    let double = BoxTransformer::new(|x: i32| x * 2);
33    let result = double.apply(21);
34    println!("   double.apply(21) = {}", result);
35
36    // Convert to BoxTransformerOnce
37    let double = BoxTransformer::new(|x: i32| x * 2);
38    let boxed = double.into_box();
39    let result = boxed.apply(21);
40    println!("   double.into_box().apply(21) = {}", result);
41
42    // Convert to function
43    let double = BoxTransformer::new(|x: i32| x * 2);
44    let func = double.into_fn();
45    let result = func(21);
46    println!("   double.into_fn()(21) = {}", result);
47
48    println!();
49
50    // RcTransformer TransformerOnce demonstration
51    println!("2. RcTransformer TransformerOnce demonstration:");
52    let uppercase = RcTransformer::new(|s: String| s.to_uppercase());
53    let result = uppercase.apply("hello".to_string());
54    println!("   uppercase.apply(\"hello\") = {}", result);
55
56    // Use after cloning
57    let uppercase = RcTransformer::new(|s: String| s.to_uppercase());
58    let uppercase_clone = uppercase.clone();
59    let result1 = uppercase.apply("world".to_string());
60    let result2 = uppercase_clone.apply("rust".to_string());
61    println!("   uppercase.apply(\"world\") = {}", result1);
62    println!("   uppercase_clone.apply(\"rust\") = {}", result2);
63
64    println!();
65
66    // ArcTransformer TransformerOnce demonstration
67    println!("3. ArcTransformer TransformerOnce demonstration:");
68    let parse_and_double = ArcTransformer::new(|s: String| s.parse::<i32>().unwrap_or(0) * 2);
69    let result = parse_and_double.apply("21".to_string());
70    println!("   parse_and_double.apply(\"21\") = {}", result);
71
72    // Thread safety demonstration
73    println!("4. ArcTransformer thread safety demonstration:");
74    let double = ArcTransformer::new(|x: i32| x * 2);
75    let double_arc = Arc::new(double);
76    let _double_clone = Arc::clone(&double_arc);
77
78    let handle = thread::spawn(move || {
79        // Create a new transformer in the thread to demonstrate thread safety
80        let new_double = ArcTransformer::new(|x: i32| x * 2);
81        new_double.apply(21)
82    });
83
84    let result = handle.join().unwrap();
85    println!("   Executed in thread: new_double.apply(21) = {}", result);
86
87    println!("\n=== Demo completed ===");
88}
examples/transformers/transformer_once_specialized_methods_demo.rs (line 36)
24fn main() {
25    println!("=== TransformerOnce Specialized Methods Demo ===\n");
26
27    // ============================================================================
28    // ArcTransformer TransformerOnce specialized methods
29    // ============================================================================
30
31    println!("1. ArcTransformer TransformerOnce specialized methods:");
32
33    let arc_double = ArcTransformer::new(|x: i32| x * 2);
34
35    // Test into_box - consumes self
36    let boxed_once = arc_double.clone().into_box();
37    println!("   ArcTransformer::into_box(): {}", boxed_once.apply(21));
38
39    // Test into_fn - consumes self
40    let fn_once = arc_double.clone().into_fn();
41    println!("   ArcTransformer::into_fn(): {}", fn_once(21));
42
43    // Test to_box - borrows self
44    let boxed_once_borrowed = arc_double.to_box();
45    println!(
46        "   ArcTransformer::to_box(): {}",
47        boxed_once_borrowed.apply(21)
48    );
49
50    // Test to_fn - borrows self
51    let fn_once_borrowed = arc_double.to_fn();
52    println!("   ArcTransformer::to_fn(): {}", fn_once_borrowed(21));
53
54    // Original transformer still usable after to_xxx methods
55    println!(
56        "   Original ArcTransformer still works: {}",
57        arc_double.apply(21)
58    );
59
60    println!();
61
62    // ============================================================================
63    // RcTransformer TransformerOnce specialized methods
64    // ============================================================================
65
66    println!("2. RcTransformer TransformerOnce specialized methods:");
67
68    let rc_triple = RcTransformer::new(|x: i32| x * 3);
69
70    // Test into_box - consumes self
71    let boxed_once = rc_triple.clone().into_box();
72    println!("   RcTransformer::into_box(): {}", boxed_once.apply(14));
73
74    // Test into_fn - consumes self
75    let fn_once = rc_triple.clone().into_fn();
76    println!("   RcTransformer::into_fn(): {}", fn_once(14));
77
78    // Test to_box - borrows self
79    let boxed_once_borrowed = rc_triple.to_box();
80    println!(
81        "   RcTransformer::to_box(): {}",
82        boxed_once_borrowed.apply(14)
83    );
84
85    // Test to_fn - borrows self
86    let fn_once_borrowed = rc_triple.to_fn();
87    println!("   RcTransformer::to_fn(): {}", fn_once_borrowed(14));
88
89    // Original transformer still usable after to_xxx methods
90    println!(
91        "   Original RcTransformer still works: {}",
92        rc_triple.apply(14)
93    );
94
95    println!();
96
97    // ============================================================================
98    // Comparison with default implementations
99    // ============================================================================
100
101    println!("3. Performance comparison (specialized vs default):");
102
103    let arc_square = ArcTransformer::new(|x: i32| x * x);
104
105    // Using specialized method (more efficient)
106    let specialized_box = arc_square.clone().into_box();
107    println!("   Specialized into_box: {}", specialized_box.apply(5));
108
109    // Using default implementation (less efficient)
110    let default_box = arc_square.clone().into_box();
111    println!("   Default into_box: {}", default_box.apply(5));
112
113    println!();
114
115    // ============================================================================
116    // Thread safety demonstration for ArcTransformer
117    // ============================================================================
118
119    println!("4. Thread safety with ArcTransformer:");
120
121    let arc_shared = ArcTransformer::new(|x: i32| x + 100);
122
123    // Clone for thread safety
124    let arc_clone = arc_shared.clone();
125
126    // Use in different thread context (simulated)
127    let handle = std::thread::spawn(move || {
128        let boxed = arc_clone.into_box();
129        boxed.apply(50)
130    });
131
132    let result = handle.join().unwrap();
133    println!("   Thread-safe ArcTransformer result: {}", result);
134
135    // Original still usable
136    println!(
137        "   Original ArcTransformer still works: {}",
138        arc_shared.apply(50)
139    );
140
141    println!();
142
143    // ============================================================================
144    // String transformation example
145    // ============================================================================
146
147    println!("5. String transformation with specialized methods:");
148
149    let arc_uppercase = ArcTransformer::new(|s: String| s.to_uppercase());
150
151    // Test with string input
152    let test_string = "hello world".to_string();
153
154    // Using specialized methods
155    let boxed_upper = arc_uppercase.clone().into_box();
156    let result = boxed_upper.apply(test_string.clone());
157    println!(
158        "   String transformation: '{}' -> '{}'",
159        test_string, result
160    );
161
162    // Using to_xxx methods (borrowing)
163    let fn_upper = arc_uppercase.to_fn();
164    let result2 = fn_upper(test_string.clone());
165    println!(
166        "   String transformation (borrowed): '{}' -> '{}'",
167        test_string, result2
168    );
169
170    // Original still usable
171    println!(
172        "   Original ArcTransformer still works: '{}'",
173        arc_uppercase.apply(test_string)
174    );
175
176    println!("\n=== Demo completed successfully! ===");
177}
Source

fn into_rc(self) -> RcTransformer<T, R>
where Self: Sized + '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/transformers/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,

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/transformers/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,

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

Examples found in repository?
examples/transformers/transformer_once_demo.rs (line 44)
27fn main() {
28    println!("=== TransformerOnce Demo ===\n");
29
30    // BoxTransformer TransformerOnce demonstration
31    println!("1. BoxTransformer TransformerOnce demonstration:");
32    let double = BoxTransformer::new(|x: i32| x * 2);
33    let result = double.apply(21);
34    println!("   double.apply(21) = {}", result);
35
36    // Convert to BoxTransformerOnce
37    let double = BoxTransformer::new(|x: i32| x * 2);
38    let boxed = double.into_box();
39    let result = boxed.apply(21);
40    println!("   double.into_box().apply(21) = {}", result);
41
42    // Convert to function
43    let double = BoxTransformer::new(|x: i32| x * 2);
44    let func = double.into_fn();
45    let result = func(21);
46    println!("   double.into_fn()(21) = {}", result);
47
48    println!();
49
50    // RcTransformer TransformerOnce demonstration
51    println!("2. RcTransformer TransformerOnce demonstration:");
52    let uppercase = RcTransformer::new(|s: String| s.to_uppercase());
53    let result = uppercase.apply("hello".to_string());
54    println!("   uppercase.apply(\"hello\") = {}", result);
55
56    // Use after cloning
57    let uppercase = RcTransformer::new(|s: String| s.to_uppercase());
58    let uppercase_clone = uppercase.clone();
59    let result1 = uppercase.apply("world".to_string());
60    let result2 = uppercase_clone.apply("rust".to_string());
61    println!("   uppercase.apply(\"world\") = {}", result1);
62    println!("   uppercase_clone.apply(\"rust\") = {}", result2);
63
64    println!();
65
66    // ArcTransformer TransformerOnce demonstration
67    println!("3. ArcTransformer TransformerOnce demonstration:");
68    let parse_and_double = ArcTransformer::new(|s: String| s.parse::<i32>().unwrap_or(0) * 2);
69    let result = parse_and_double.apply("21".to_string());
70    println!("   parse_and_double.apply(\"21\") = {}", result);
71
72    // Thread safety demonstration
73    println!("4. ArcTransformer thread safety demonstration:");
74    let double = ArcTransformer::new(|x: i32| x * 2);
75    let double_arc = Arc::new(double);
76    let _double_clone = Arc::clone(&double_arc);
77
78    let handle = thread::spawn(move || {
79        // Create a new transformer in the thread to demonstrate thread safety
80        let new_double = ArcTransformer::new(|x: i32| x * 2);
81        new_double.apply(21)
82    });
83
84    let result = handle.join().unwrap();
85    println!("   Executed in thread: new_double.apply(21) = {}", result);
86
87    println!("\n=== Demo completed ===");
88}
More examples
Hide additional examples
examples/transformers/transformer_once_specialized_methods_demo.rs (line 40)
24fn main() {
25    println!("=== TransformerOnce Specialized Methods Demo ===\n");
26
27    // ============================================================================
28    // ArcTransformer TransformerOnce specialized methods
29    // ============================================================================
30
31    println!("1. ArcTransformer TransformerOnce specialized methods:");
32
33    let arc_double = ArcTransformer::new(|x: i32| x * 2);
34
35    // Test into_box - consumes self
36    let boxed_once = arc_double.clone().into_box();
37    println!("   ArcTransformer::into_box(): {}", boxed_once.apply(21));
38
39    // Test into_fn - consumes self
40    let fn_once = arc_double.clone().into_fn();
41    println!("   ArcTransformer::into_fn(): {}", fn_once(21));
42
43    // Test to_box - borrows self
44    let boxed_once_borrowed = arc_double.to_box();
45    println!(
46        "   ArcTransformer::to_box(): {}",
47        boxed_once_borrowed.apply(21)
48    );
49
50    // Test to_fn - borrows self
51    let fn_once_borrowed = arc_double.to_fn();
52    println!("   ArcTransformer::to_fn(): {}", fn_once_borrowed(21));
53
54    // Original transformer still usable after to_xxx methods
55    println!(
56        "   Original ArcTransformer still works: {}",
57        arc_double.apply(21)
58    );
59
60    println!();
61
62    // ============================================================================
63    // RcTransformer TransformerOnce specialized methods
64    // ============================================================================
65
66    println!("2. RcTransformer TransformerOnce specialized methods:");
67
68    let rc_triple = RcTransformer::new(|x: i32| x * 3);
69
70    // Test into_box - consumes self
71    let boxed_once = rc_triple.clone().into_box();
72    println!("   RcTransformer::into_box(): {}", boxed_once.apply(14));
73
74    // Test into_fn - consumes self
75    let fn_once = rc_triple.clone().into_fn();
76    println!("   RcTransformer::into_fn(): {}", fn_once(14));
77
78    // Test to_box - borrows self
79    let boxed_once_borrowed = rc_triple.to_box();
80    println!(
81        "   RcTransformer::to_box(): {}",
82        boxed_once_borrowed.apply(14)
83    );
84
85    // Test to_fn - borrows self
86    let fn_once_borrowed = rc_triple.to_fn();
87    println!("   RcTransformer::to_fn(): {}", fn_once_borrowed(14));
88
89    // Original transformer still usable after to_xxx methods
90    println!(
91        "   Original RcTransformer still works: {}",
92        rc_triple.apply(14)
93    );
94
95    println!();
96
97    // ============================================================================
98    // Comparison with default implementations
99    // ============================================================================
100
101    println!("3. Performance comparison (specialized vs default):");
102
103    let arc_square = ArcTransformer::new(|x: i32| x * x);
104
105    // Using specialized method (more efficient)
106    let specialized_box = arc_square.clone().into_box();
107    println!("   Specialized into_box: {}", specialized_box.apply(5));
108
109    // Using default implementation (less efficient)
110    let default_box = arc_square.clone().into_box();
111    println!("   Default into_box: {}", default_box.apply(5));
112
113    println!();
114
115    // ============================================================================
116    // Thread safety demonstration for ArcTransformer
117    // ============================================================================
118
119    println!("4. Thread safety with ArcTransformer:");
120
121    let arc_shared = ArcTransformer::new(|x: i32| x + 100);
122
123    // Clone for thread safety
124    let arc_clone = arc_shared.clone();
125
126    // Use in different thread context (simulated)
127    let handle = std::thread::spawn(move || {
128        let boxed = arc_clone.into_box();
129        boxed.apply(50)
130    });
131
132    let result = handle.join().unwrap();
133    println!("   Thread-safe ArcTransformer result: {}", result);
134
135    // Original still usable
136    println!(
137        "   Original ArcTransformer still works: {}",
138        arc_shared.apply(50)
139    );
140
141    println!();
142
143    // ============================================================================
144    // String transformation example
145    // ============================================================================
146
147    println!("5. String transformation with specialized methods:");
148
149    let arc_uppercase = ArcTransformer::new(|s: String| s.to_uppercase());
150
151    // Test with string input
152    let test_string = "hello world".to_string();
153
154    // Using specialized methods
155    let boxed_upper = arc_uppercase.clone().into_box();
156    let result = boxed_upper.apply(test_string.clone());
157    println!(
158        "   String transformation: '{}' -> '{}'",
159        test_string, result
160    );
161
162    // Using to_xxx methods (borrowing)
163    let fn_upper = arc_uppercase.to_fn();
164    let result2 = fn_upper(test_string.clone());
165    println!(
166        "   String transformation (borrowed): '{}' -> '{}'",
167        test_string, result2
168    );
169
170    // Original still usable
171    println!(
172        "   Original ArcTransformer still works: '{}'",
173        arc_uppercase.apply(test_string)
174    );
175
176    println!("\n=== Demo completed successfully! ===");
177}
Source

fn into_once(self) -> BoxTransformerOnce<T, R>
where Self: Sized + '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 qubit_function::{Transformer, TransformerOnce};

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,

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 qubit_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);
Examples found in repository?
examples/transformers/transformer_once_specialized_methods_demo.rs (line 44)
24fn main() {
25    println!("=== TransformerOnce Specialized Methods Demo ===\n");
26
27    // ============================================================================
28    // ArcTransformer TransformerOnce specialized methods
29    // ============================================================================
30
31    println!("1. ArcTransformer TransformerOnce specialized methods:");
32
33    let arc_double = ArcTransformer::new(|x: i32| x * 2);
34
35    // Test into_box - consumes self
36    let boxed_once = arc_double.clone().into_box();
37    println!("   ArcTransformer::into_box(): {}", boxed_once.apply(21));
38
39    // Test into_fn - consumes self
40    let fn_once = arc_double.clone().into_fn();
41    println!("   ArcTransformer::into_fn(): {}", fn_once(21));
42
43    // Test to_box - borrows self
44    let boxed_once_borrowed = arc_double.to_box();
45    println!(
46        "   ArcTransformer::to_box(): {}",
47        boxed_once_borrowed.apply(21)
48    );
49
50    // Test to_fn - borrows self
51    let fn_once_borrowed = arc_double.to_fn();
52    println!("   ArcTransformer::to_fn(): {}", fn_once_borrowed(21));
53
54    // Original transformer still usable after to_xxx methods
55    println!(
56        "   Original ArcTransformer still works: {}",
57        arc_double.apply(21)
58    );
59
60    println!();
61
62    // ============================================================================
63    // RcTransformer TransformerOnce specialized methods
64    // ============================================================================
65
66    println!("2. RcTransformer TransformerOnce specialized methods:");
67
68    let rc_triple = RcTransformer::new(|x: i32| x * 3);
69
70    // Test into_box - consumes self
71    let boxed_once = rc_triple.clone().into_box();
72    println!("   RcTransformer::into_box(): {}", boxed_once.apply(14));
73
74    // Test into_fn - consumes self
75    let fn_once = rc_triple.clone().into_fn();
76    println!("   RcTransformer::into_fn(): {}", fn_once(14));
77
78    // Test to_box - borrows self
79    let boxed_once_borrowed = rc_triple.to_box();
80    println!(
81        "   RcTransformer::to_box(): {}",
82        boxed_once_borrowed.apply(14)
83    );
84
85    // Test to_fn - borrows self
86    let fn_once_borrowed = rc_triple.to_fn();
87    println!("   RcTransformer::to_fn(): {}", fn_once_borrowed(14));
88
89    // Original transformer still usable after to_xxx methods
90    println!(
91        "   Original RcTransformer still works: {}",
92        rc_triple.apply(14)
93    );
94
95    println!();
96
97    // ============================================================================
98    // Comparison with default implementations
99    // ============================================================================
100
101    println!("3. Performance comparison (specialized vs default):");
102
103    let arc_square = ArcTransformer::new(|x: i32| x * x);
104
105    // Using specialized method (more efficient)
106    let specialized_box = arc_square.clone().into_box();
107    println!("   Specialized into_box: {}", specialized_box.apply(5));
108
109    // Using default implementation (less efficient)
110    let default_box = arc_square.clone().into_box();
111    println!("   Default into_box: {}", default_box.apply(5));
112
113    println!();
114
115    // ============================================================================
116    // Thread safety demonstration for ArcTransformer
117    // ============================================================================
118
119    println!("4. Thread safety with ArcTransformer:");
120
121    let arc_shared = ArcTransformer::new(|x: i32| x + 100);
122
123    // Clone for thread safety
124    let arc_clone = arc_shared.clone();
125
126    // Use in different thread context (simulated)
127    let handle = std::thread::spawn(move || {
128        let boxed = arc_clone.into_box();
129        boxed.apply(50)
130    });
131
132    let result = handle.join().unwrap();
133    println!("   Thread-safe ArcTransformer result: {}", result);
134
135    // Original still usable
136    println!(
137        "   Original ArcTransformer still works: {}",
138        arc_shared.apply(50)
139    );
140
141    println!();
142
143    // ============================================================================
144    // String transformation example
145    // ============================================================================
146
147    println!("5. String transformation with specialized methods:");
148
149    let arc_uppercase = ArcTransformer::new(|s: String| s.to_uppercase());
150
151    // Test with string input
152    let test_string = "hello world".to_string();
153
154    // Using specialized methods
155    let boxed_upper = arc_uppercase.clone().into_box();
156    let result = boxed_upper.apply(test_string.clone());
157    println!(
158        "   String transformation: '{}' -> '{}'",
159        test_string, result
160    );
161
162    // Using to_xxx methods (borrowing)
163    let fn_upper = arc_uppercase.to_fn();
164    let result2 = fn_upper(test_string.clone());
165    println!(
166        "   String transformation (borrowed): '{}' -> '{}'",
167        test_string, result2
168    );
169
170    // Original still usable
171    println!(
172        "   Original ArcTransformer still works: '{}'",
173        arc_uppercase.apply(test_string)
174    );
175
176    println!("\n=== Demo completed successfully! ===");
177}
Source

fn to_rc(&self) -> RcTransformer<T, R>
where Self: Clone + '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 qubit_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,

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 qubit_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,

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 qubit_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);
Examples found in repository?
examples/transformers/transformer_once_specialized_methods_demo.rs (line 51)
24fn main() {
25    println!("=== TransformerOnce Specialized Methods Demo ===\n");
26
27    // ============================================================================
28    // ArcTransformer TransformerOnce specialized methods
29    // ============================================================================
30
31    println!("1. ArcTransformer TransformerOnce specialized methods:");
32
33    let arc_double = ArcTransformer::new(|x: i32| x * 2);
34
35    // Test into_box - consumes self
36    let boxed_once = arc_double.clone().into_box();
37    println!("   ArcTransformer::into_box(): {}", boxed_once.apply(21));
38
39    // Test into_fn - consumes self
40    let fn_once = arc_double.clone().into_fn();
41    println!("   ArcTransformer::into_fn(): {}", fn_once(21));
42
43    // Test to_box - borrows self
44    let boxed_once_borrowed = arc_double.to_box();
45    println!(
46        "   ArcTransformer::to_box(): {}",
47        boxed_once_borrowed.apply(21)
48    );
49
50    // Test to_fn - borrows self
51    let fn_once_borrowed = arc_double.to_fn();
52    println!("   ArcTransformer::to_fn(): {}", fn_once_borrowed(21));
53
54    // Original transformer still usable after to_xxx methods
55    println!(
56        "   Original ArcTransformer still works: {}",
57        arc_double.apply(21)
58    );
59
60    println!();
61
62    // ============================================================================
63    // RcTransformer TransformerOnce specialized methods
64    // ============================================================================
65
66    println!("2. RcTransformer TransformerOnce specialized methods:");
67
68    let rc_triple = RcTransformer::new(|x: i32| x * 3);
69
70    // Test into_box - consumes self
71    let boxed_once = rc_triple.clone().into_box();
72    println!("   RcTransformer::into_box(): {}", boxed_once.apply(14));
73
74    // Test into_fn - consumes self
75    let fn_once = rc_triple.clone().into_fn();
76    println!("   RcTransformer::into_fn(): {}", fn_once(14));
77
78    // Test to_box - borrows self
79    let boxed_once_borrowed = rc_triple.to_box();
80    println!(
81        "   RcTransformer::to_box(): {}",
82        boxed_once_borrowed.apply(14)
83    );
84
85    // Test to_fn - borrows self
86    let fn_once_borrowed = rc_triple.to_fn();
87    println!("   RcTransformer::to_fn(): {}", fn_once_borrowed(14));
88
89    // Original transformer still usable after to_xxx methods
90    println!(
91        "   Original RcTransformer still works: {}",
92        rc_triple.apply(14)
93    );
94
95    println!();
96
97    // ============================================================================
98    // Comparison with default implementations
99    // ============================================================================
100
101    println!("3. Performance comparison (specialized vs default):");
102
103    let arc_square = ArcTransformer::new(|x: i32| x * x);
104
105    // Using specialized method (more efficient)
106    let specialized_box = arc_square.clone().into_box();
107    println!("   Specialized into_box: {}", specialized_box.apply(5));
108
109    // Using default implementation (less efficient)
110    let default_box = arc_square.clone().into_box();
111    println!("   Default into_box: {}", default_box.apply(5));
112
113    println!();
114
115    // ============================================================================
116    // Thread safety demonstration for ArcTransformer
117    // ============================================================================
118
119    println!("4. Thread safety with ArcTransformer:");
120
121    let arc_shared = ArcTransformer::new(|x: i32| x + 100);
122
123    // Clone for thread safety
124    let arc_clone = arc_shared.clone();
125
126    // Use in different thread context (simulated)
127    let handle = std::thread::spawn(move || {
128        let boxed = arc_clone.into_box();
129        boxed.apply(50)
130    });
131
132    let result = handle.join().unwrap();
133    println!("   Thread-safe ArcTransformer result: {}", result);
134
135    // Original still usable
136    println!(
137        "   Original ArcTransformer still works: {}",
138        arc_shared.apply(50)
139    );
140
141    println!();
142
143    // ============================================================================
144    // String transformation example
145    // ============================================================================
146
147    println!("5. String transformation with specialized methods:");
148
149    let arc_uppercase = ArcTransformer::new(|s: String| s.to_uppercase());
150
151    // Test with string input
152    let test_string = "hello world".to_string();
153
154    // Using specialized methods
155    let boxed_upper = arc_uppercase.clone().into_box();
156    let result = boxed_upper.apply(test_string.clone());
157    println!(
158        "   String transformation: '{}' -> '{}'",
159        test_string, result
160    );
161
162    // Using to_xxx methods (borrowing)
163    let fn_upper = arc_uppercase.to_fn();
164    let result2 = fn_upper(test_string.clone());
165    println!(
166        "   String transformation (borrowed): '{}' -> '{}'",
167        test_string, result2
168    );
169
170    // Original still usable
171    println!(
172        "   Original ArcTransformer still works: '{}'",
173        arc_uppercase.apply(test_string)
174    );
175
176    println!("\n=== Demo completed successfully! ===");
177}
Source

fn to_once(&self) -> BoxTransformerOnce<T, R>
where Self: Clone + '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,