Mapper

Trait Mapper 

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

    // Provided methods
    fn into_box(self) -> BoxMapper<T, R>
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn into_rc(self) -> RcMapper<T, R>
       where Self: Sized + 'static,
             T: 'static,
             R: 'static { ... }
    fn into_arc(self) -> ArcMapper<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 to_box(&self) -> BoxMapper<T, R>
       where Self: Sized + Clone + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_rc(&self) -> RcMapper<T, R>
       where Self: Sized + Clone + 'static,
             T: 'static,
             R: 'static { ... }
    fn to_arc(&self) -> ArcMapper<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 { ... }
}
Expand description

Mapper 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 mapping 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) -> BoxMapper<T, R>
where Self: Sized + 'static, T: 'static, R: 'static,

Converts to BoxMapper

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

§Returns

Returns BoxMapper<T, R>

§Default Implementation

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

§Examples
use prism3_function::{Mapper, BoxMapper};

struct CustomMapper {
    multiplier: i32,
}

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

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

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

Converts to RcMapper

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

§Returns

Returns RcMapper<T, R>

§Default Implementation

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

§Examples
use prism3_function::{Mapper, RcMapper};

struct CustomMapper {
    multiplier: i32,
}

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

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

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

Converts to ArcMapper

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

§Returns

Returns ArcMapper<T, R>

§Default Implementation

The default implementation wraps self in an ArcMapper 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::{Mapper, ArcMapper};

struct CustomMapper {
    multiplier: i32,
}

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

let mapper = CustomMapper { multiplier: 0 };
let mut arc_mapper = mapper.into_arc();
assert_eq!(arc_mapper.apply(10), 10);  // 10 * 1
assert_eq!(arc_mapper.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 mapper 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::{Mapper, BoxMapper};

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

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

Non-consuming conversion to BoxMapper.

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

Source

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

Non-consuming conversion to RcMapper.

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

Source

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

Non-consuming conversion to ArcMapper (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.

Implementors§

Source§

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

Implement Mapper<T, R> for any type that implements FnMut(T) -> R

This allows closures to be used directly with our Mapper trait without wrapping.

§Examples

use prism3_function::Mapper;

let mut counter = 0;
let mut mapper = |x: i32| {
    counter += 1;
    x + counter
};

assert_eq!(mapper.apply(10), 11);
assert_eq!(mapper.apply(10), 12);

§Author

Haixing Hu

Source§

impl<T, R> Mapper<T, R> for ArcMapper<T, R>

Source§

impl<T, R> Mapper<T, R> for BoxMapper<T, R>

Source§

impl<T, R> Mapper<T, R> for RcMapper<T, R>