StatefulTransformer

Trait StatefulTransformer 

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

    // Provided methods
    fn into_box(self) -> BoxStatefulTransformer<T, R>
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn into_rc(self) -> RcStatefulTransformer<T, R>
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn into_arc(self) -> ArcStatefulTransformer<T, R>
       where Self: Sized + Send + 'static,
             T: Send + Sync + 'static,
             R: Send + 'static { ... }
    fn into_fn(self) -> impl FnMut(T) -> R
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn into_once(self) -> BoxTransformerOnce<T, R>
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_box(&self) -> BoxStatefulTransformer<T, R>
       where Self: Sized + Clone + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_rc(&self) -> RcStatefulTransformer<T, R>
       where Self: Sized + Clone + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_arc(&self) -> ArcStatefulTransformer<T, R>
       where Self: Sized + Clone + Send + Sync + 'static,
             T: Send + Sync + 'static,
             R: Send + 'static { ... }
    fn to_fn(&self) -> impl FnMut(T) -> R
       where Self: Sized + Clone + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_once(&self) -> BoxTransformerOnce<T, R>
       where Self: Sized + Clone + 'static,
             T: 'static,
             R: 'static { ... }
}
Expand description

StatefulTransformer trait - transforms values from type T to type R with state

Defines the behavior of a stateful transformation: converting a value of type T to a value of type R by consuming the input while allowing modification of internal state. This is analogous to FnMut(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(&mut 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) -> BoxStatefulTransformer<T, R>
where Self: Sized + 'static, T: 'static, R: 'static,

Converts to BoxStatefulTransformer

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

§Returns

Returns BoxStatefulTransformer<T, R>

§Default Implementation

The default implementation wraps self in a BoxStatefulTransformer by creating a new closure that calls self.apply(). This provides a zero-cost abstraction for most use cases.

§Examples
use prism3_function::{StatefulTransformer, BoxStatefulTransformer};

struct CustomTransformer {
    multiplier: i32,
}

impl StatefulTransformer<i32, i32> for CustomTransformer {
    fn apply(&mut self, input: i32) -> i32 {
        self.multiplier += 1;
        input * self.multiplier
    }
}

let transformer = CustomTransformer { multiplier: 0 };
let mut boxed = transformer.into_box();
assert_eq!(boxed.apply(10), 10);  // 10 * 1
assert_eq!(boxed.apply(10), 20);  // 10 * 2
Source

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

Converts to RcStatefulTransformer

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

§Returns

Returns RcStatefulTransformer<T, R>

§Default Implementation

The default implementation first converts to BoxStatefulTransformer using into_box(), then wraps it in RcStatefulTransformer. Specific implementations may override this for better efficiency.

§Examples
use prism3_function::{StatefulTransformer, RcStatefulTransformer};

struct CustomTransformer {
    multiplier: i32,
}

impl StatefulTransformer<i32, i32> for CustomTransformer {
    fn apply(&mut self, input: i32) -> i32 {
        self.multiplier += 1;
        input * self.multiplier
    }
}

let transformer = CustomTransformer { multiplier: 0 };
let mut rc_transformer = transformer.into_rc();
assert_eq!(rc_transformer.apply(10), 10);  // 10 * 1
assert_eq!(rc_transformer.apply(10), 20);  // 10 * 2
Source

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

Converts to ArcStatefulTransformer

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

§Returns

Returns ArcStatefulTransformer<T, R>

§Default Implementation

The default implementation wraps self in an ArcStatefulTransformer by creating a new closure that calls self.apply(). Note that this requires self to implement Send due to Arc’s thread-safety requirements.

§Examples
use prism3_function::{StatefulTransformer, ArcStatefulTransformer};

struct CustomTransformer {
    multiplier: i32,
}

impl StatefulTransformer<i32, i32> for CustomTransformer {
    fn apply(&mut self, input: i32) -> i32 {
        self.multiplier += 1;
        input * self.multiplier
    }
}

let transformer = CustomTransformer { multiplier: 0 };
let mut arc_transformer = transformer.into_arc();
assert_eq!(arc_transformer.apply(10), 10);  // 10 * 1
assert_eq!(arc_transformer.apply(10), 20);  // 10 * 2
Source

fn into_fn(self) -> impl FnMut(T) -> R
where Self: Sized + 'static, T: 'static, R: 'static,

Converts to a closure implementing FnMut(T) -> R

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

§Returns

Returns an implementation of FnMut(T) -> R

§Default Implementation

The default implementation creates a new closure that calls self.apply(). Specific implementations may override this for better efficiency.

§Examples
use prism3_function::{StatefulTransformer, BoxStatefulTransformer};

let transformer = BoxStatefulTransformer::new(|x: i32| x * 2);
let mut closure = transformer.into_fn();
assert_eq!(closure(10), 20);
assert_eq!(closure(15), 30);
Source

fn into_once(self) -> BoxTransformerOnce<T, R>
where Self: Sized + 'static, T: 'static, R: '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 prism3_function::StatefulTransformer;

let closure = |x: i32| x * 2;
let once = closure.into_once();
assert_eq!(once.apply(5), 10);
Source

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

Non-consuming conversion to BoxStatefulTransformer.

Default implementation requires Self: Clone and wraps a cloned instance in a RefCell so the returned transformer can mutate state across calls.

Source

fn to_rc(&self) -> RcStatefulTransformer<T, R>
where Self: Sized + Clone + 'static, T: 'static, R: 'static,

Non-consuming conversion to RcStatefulTransformer.

Default implementation clones self into an Rc<RefCell<_>> so the resulting transformer can be shared within a single thread.

Source

fn to_arc(&self) -> ArcStatefulTransformer<T, R>
where Self: Sized + Clone + Send + Sync + 'static, T: Send + Sync + 'static, R: Send + 'static,

Non-consuming conversion to ArcStatefulTransformer (thread-safe).

Default implementation requires Self: Clone + Send + Sync and wraps the cloned instance in Arc<Mutex<_>> so it can be used across threads.

Source

fn to_fn(&self) -> impl FnMut(T) -> R
where Self: Sized + Clone + 'static, T: 'static, R: 'static,

Non-consuming conversion to a closure (FnMut(T) -> R).

Default implementation clones self into a RefCell and returns a closure that calls apply on the interior mutable value.

Source

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

Creates a BoxTransformerOnce from a cloned transformer

Uses Clone to obtain an owned copy and converts it into a BoxTransformerOnce. Requires Self: Clone. Custom implementations can override this for better performance.

Implementors§

Source§

impl<T, R> StatefulTransformer<T, R> for ArcStatefulTransformer<T, R>

Source§

impl<T, R> StatefulTransformer<T, R> for BoxStatefulTransformer<T, R>

Source§

impl<T, R> StatefulTransformer<T, R> for RcStatefulTransformer<T, R>

Source§

impl<T, R, F> StatefulTransformer<T, R> for F
where F: FnMut(T) -> R, T: 'static, R: 'static,