TransformerOnce

Trait TransformerOnce 

Source
pub trait TransformerOnce<T, R> {
    // Required methods
    fn transform(self, input: T) -> R;
    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;
}
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 transform(self, input: T) -> R

Transforms the input value, consuming both self and input

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

The transformed output value

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>

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

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.transform("42".to_string()), 42);

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

§Author

Hu Haixing

Source§

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