TransformerOnce

Trait TransformerOnce 

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

    // Provided methods
    fn into_box(self) -> BoxTransformerOnce<T, R>
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn into_fn(self) -> impl FnOnce(T) -> R
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_box(&self) -> BoxTransformerOnce<T, R>
       where Self: Clone + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_fn(&self) -> impl FnOnce(T) -> R
       where Self: Clone + 'static,
             T: 'static,
             R: 'static { ... }
}
Expand description

TransformerOnce trait - consuming transformation that takes ownership

Defines the behavior of a consuming transformer: converting a value of type T to a value of type R by taking ownership of both self and the input. This trait is analogous to FnOnce(T) -> R.

§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

Transforms the input value, consuming both self and input

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

The transformed output value

Provided Methods§

Source

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

Converts to BoxTransformerOnce

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

§Returns

Returns BoxTransformerOnce<T, R>

§Examples
use prism3_function::TransformerOnce;

let double = |x: i32| x * 2;
let boxed = double.into_box();
assert_eq!(boxed.apply(21), 42);
Examples found in repository?
examples/transformer_once_blanket_impl_demo.rs (line 35)
19fn test_transformer_once() {
20    // Test function pointer
21    fn parse(s: String) -> i32 {
22        s.parse().unwrap_or(0)
23    }
24    assert_eq!(parse.apply("42".to_string()), 42);
25    println!("✓ Function pointer test passed: parse.apply(\"42\") = 42");
26
27    // Test closure that consumes ownership
28    let owned_value = String::from("hello");
29    let consume = |s: String| format!("{} world", s);
30    assert_eq!(consume.apply(owned_value), "hello world");
31    println!("✓ Closure that consumes ownership test passed");
32
33    // Test conversion to BoxTransformerOnce
34    let transform = |s: String| s.to_uppercase();
35    let boxed = transform.into_box();
36    assert_eq!(boxed.apply("hello".to_string()), "HELLO");
37    println!("✓ into_box() test passed");
38
39    // Test into_fn
40    let transform2 = |s: String| s.len();
41    let func = transform2.into_fn();
42    assert_eq!(func("hello".to_string()), 5);
43    println!("✓ into_fn() test passed");
44}
Source

fn into_fn(self) -> impl FnOnce(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.

§Returns

Returns a closure that implements FnOnce(T) -> R

§Examples
use prism3_function::TransformerOnce;

let double = |x: i32| x * 2;
let func = double.into_fn();
assert_eq!(func(21), 42);
Examples found in repository?
examples/transformer_once_blanket_impl_demo.rs (line 41)
19fn test_transformer_once() {
20    // Test function pointer
21    fn parse(s: String) -> i32 {
22        s.parse().unwrap_or(0)
23    }
24    assert_eq!(parse.apply("42".to_string()), 42);
25    println!("✓ Function pointer test passed: parse.apply(\"42\") = 42");
26
27    // Test closure that consumes ownership
28    let owned_value = String::from("hello");
29    let consume = |s: String| format!("{} world", s);
30    assert_eq!(consume.apply(owned_value), "hello world");
31    println!("✓ Closure that consumes ownership test passed");
32
33    // Test conversion to BoxTransformerOnce
34    let transform = |s: String| s.to_uppercase();
35    let boxed = transform.into_box();
36    assert_eq!(boxed.apply("hello".to_string()), "HELLO");
37    println!("✓ into_box() test passed");
38
39    // Test into_fn
40    let transform2 = |s: String| s.len();
41    let func = transform2.into_fn();
42    assert_eq!(func("hello".to_string()), 5);
43    println!("✓ into_fn() test passed");
44}
Source

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

Converts to BoxTransformerOnce without consuming self

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

§Default Implementation

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

§Returns

Returns BoxTransformerOnce<T, R>

§Examples
use prism3_function::TransformerOnce;

let double = |x: i32| x * 2;
let boxed = double.to_box();
assert_eq!(boxed.apply(21), 42);
Source

fn to_fn(&self) -> impl FnOnce(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 FnOnce(T) -> R

§Examples
use prism3_function::TransformerOnce;

let double = |x: i32| x * 2;
let func = double.to_fn();
assert_eq!(func(21), 42);

Implementors§

Source§

impl<F, T, R> TransformerOnce<T, R> for F
where F: FnOnce(T) -> R, T: 'static, R: 'static,

Implement TransformerOnce<T, R> for any type that implements FnOnce(T) -> R

This allows once-callable closures and function pointers to be used directly with our TransformerOnce trait without wrapping.

§Examples

use prism3_function::TransformerOnce;

fn parse(s: String) -> i32 {
    s.parse().unwrap_or(0)
}

assert_eq!(parse.apply("42".to_string()), 42);

let owned_value = String::from("hello");
let consume = |s: String| {
    format!("{} world", s)
};
assert_eq!(consume.apply(owned_value), "hello world");

§Author

Hu Haixing

Source§

impl<T, R> TransformerOnce<T, R> for BoxTransformerOnce<T, R>