pub trait Transformer<T, R> {
// Required method
fn apply(&self, input: T) -> R;
// Provided methods
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 { ... }
fn into_once(self) -> BoxTransformerOnce<T, R>
where Self: Sized + 'static,
T: 'static,
R: 'static { ... }
fn to_box(&self) -> BoxTransformer<T, R>
where Self: Clone + 'static,
T: 'static,
R: 'static { ... }
fn to_rc(&self) -> RcTransformer<T, R>
where Self: Clone + 'static,
T: 'static,
R: 'static { ... }
fn to_arc(&self) -> ArcTransformer<T, R>
where Self: Clone + Send + Sync + 'static,
T: Send + Sync + 'static,
R: Send + Sync + 'static { ... }
fn to_fn(&self) -> impl Fn(T) -> R
where Self: Clone + 'static,
T: 'static,
R: 'static { ... }
fn to_once(&self) -> BoxTransformerOnce<T, R>
where Self: Clone + '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
Haixing Hu
Required Methods§
Provided Methods§
Sourcefn into_box(self) -> BoxTransformer<T, R>where
Self: Sized + 'static,
T: 'static,
R: 'static,
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.
§Default Implementation
The default implementation wraps self in a Box and creates a
BoxTransformer. Types can override this method to provide more
efficient conversions.
§Returns
Returns BoxTransformer<T, R>
Sourcefn into_rc(self) -> RcTransformer<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,
Converts to RcTransformer
⚠️ Consumes self: The original transformer becomes
unavailable after calling this method.
§Default Implementation
The default implementation wraps self in an Rc and creates an
RcTransformer. Types can override this method to provide more
efficient conversions.
§Returns
Returns RcTransformer<T, R>
Sourcefn into_arc(self) -> ArcTransformer<T, R>
fn into_arc(self) -> ArcTransformer<T, R>
Converts to ArcTransformer
⚠️ Consumes self: The original transformer becomes
unavailable after calling this method.
§Default Implementation
The default implementation wraps self in an Arc and creates
an ArcTransformer. Types can override this method to provide
more efficient conversions.
§Returns
Returns ArcTransformer<T, R>
Sourcefn into_fn(self) -> impl Fn(T) -> Rwhere
Self: Sized + 'static,
T: 'static,
R: 'static,
fn into_fn(self) -> impl Fn(T) -> Rwhere
Self: Sized + 'static,
T: 'static,
R: 'static,
Converts transformer to a closure
⚠️ Consumes self: The original transformer becomes
unavailable after calling this method.
§Default Implementation
The default implementation creates a closure that captures self
and calls its transform method. Types can override this method
to provide more efficient conversions.
§Returns
Returns a closure that implements Fn(T) -> R
Sourcefn into_once(self) -> BoxTransformerOnce<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,
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::Transformer;
let closure = |x: i32| x * 2;
let once = closure.into_once();
assert_eq!(once.apply(5), 10);Sourcefn to_box(&self) -> BoxTransformer<T, R>where
Self: Clone + 'static,
T: 'static,
R: 'static,
fn to_box(&self) -> BoxTransformer<T, R>where
Self: Clone + 'static,
T: 'static,
R: 'static,
Converts to BoxTransformer without consuming self
📌 Borrows &self: The original transformer remains usable
after calling this method.
§Default Implementation
The default implementation creates a new BoxTransformer that
captures a reference-counted clone. Types implementing Clone
can override this method to provide more efficient conversions.
§Returns
Returns BoxTransformer<T, R>
§Examples
use prism3_function::{ArcTransformer, Transformer};
let double = ArcTransformer::new(|x: i32| x * 2);
let boxed = double.to_box();
// Original transformer still usable
assert_eq!(double.apply(21), 42);
assert_eq!(boxed.apply(21), 42);Sourcefn to_rc(&self) -> RcTransformer<T, R>where
Self: Clone + 'static,
T: 'static,
R: 'static,
fn to_rc(&self) -> RcTransformer<T, R>where
Self: Clone + 'static,
T: 'static,
R: 'static,
Converts to RcTransformer without consuming self
📌 Borrows &self: The original transformer remains usable
after calling this method.
§Default Implementation
The default implementation creates a new RcTransformer that
captures a reference-counted clone. Types implementing Clone
can override this method to provide more efficient conversions.
§Returns
Returns RcTransformer<T, R>
§Examples
use prism3_function::{ArcTransformer, Transformer};
let double = ArcTransformer::new(|x: i32| x * 2);
let rc = double.to_rc();
// Original transformer still usable
assert_eq!(double.apply(21), 42);
assert_eq!(rc.apply(21), 42);Sourcefn to_arc(&self) -> ArcTransformer<T, R>
fn to_arc(&self) -> ArcTransformer<T, R>
Converts to ArcTransformer without consuming self
📌 Borrows &self: The original transformer remains usable
after calling this method.
§Default Implementation
The default implementation creates a new ArcTransformer that
captures a reference-counted clone. Types implementing Clone
can override this method to provide more efficient conversions.
§Returns
Returns ArcTransformer<T, R>
§Examples
use prism3_function::{ArcTransformer, Transformer};
let double = ArcTransformer::new(|x: i32| x * 2);
let arc = double.to_arc();
// Original transformer still usable
assert_eq!(double.apply(21), 42);
assert_eq!(arc.apply(21), 42);Sourcefn to_fn(&self) -> impl Fn(T) -> Rwhere
Self: Clone + 'static,
T: 'static,
R: 'static,
fn to_fn(&self) -> impl Fn(T) -> Rwhere
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 Fn(T) -> R
§Examples
use prism3_function::{ArcTransformer, Transformer};
let double = ArcTransformer::new(|x: i32| x * 2);
let closure = double.to_fn();
// Original transformer still usable
assert_eq!(double.apply(21), 42);
assert_eq!(closure(21), 42);Sourcefn to_once(&self) -> BoxTransformerOnce<T, R>where
Self: Clone + 'static,
T: 'static,
R: 'static,
fn to_once(&self) -> BoxTransformerOnce<T, R>where
Self: Clone + 'static,
T: 'static,
R: 'static,
Converts to BoxTransformerOnce without consuming self
⚠️ Requires Clone: This method requires Self to implement Clone.
Clones the current transformer and converts the clone to a one-time transformer.
§Returns
Returns a BoxTransformerOnce<T, R>