Transformer

Trait Transformer 

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

Source

fn transform(&self, input: T) -> R

Transforms the input value to produce an output value

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

The transformed output value

Source

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.

§Returns

Returns BoxTransformer<T, R>

Source

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.

§Returns

Returns RcTransformer<T, R>

Source

fn into_arc(self) -> ArcTransformer<T, R>
where Self: Sized + Send + Sync + 'static, T: Send + Sync + 'static, R: Send + Sync + 'static,

Converts to ArcTransformer

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

§Returns

Returns ArcTransformer<T, R>

Source

fn into_fn(self) -> impl Fn(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 Fn(T) -> R

Implementors§

Source§

impl<F, T, R> Transformer<T, R> for F
where 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.transform(21), 42);

let triple = |x: i32| x * 3;
assert_eq!(triple.transform(14), 42);

§Author

Hu Haixing

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>