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§
Sourcefn into_box(self) -> BoxTransformerOnce<T, R>where
Self: Sized + 'static,
T: 'static,
R: 'static,
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>
Implementors§
impl<F, T, R> TransformerOnce<T, R> for Fwhere
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