pub struct BoxFunction<T, R> { /* private fields */ }Expand description
BoxFunction - function wrapper based on Box<dyn Fn>
A function wrapper that provides single ownership with reusable transformation. The function consumes the input and can be called multiple times.
§Features
- Based on:
Box<dyn Fn(&T) -> R> - Ownership: Single ownership, cannot be cloned
- Reusability: Can be called multiple times (each call consumes its input)
- Thread Safety: Not thread-safe (no
Send + Syncrequirement)
§Author
Haixing Hu
Implementations§
Source§impl<T, R> BoxFunction<T, R>
impl<T, R> BoxFunction<T, R>
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Creates a new function.
Wraps the provided closure in the appropriate smart pointer type for this function implementation.
Examples found in repository?
42fn demo_borrowed_functions() {
43 println!("--- Borrowed-input functions ---");
44
45 let len = BoxFunction::new(|value: &String| value.len());
46 let value = String::from("qubit");
47 println!("Function length: {}", len.apply(&value));
48 println!("Original value is still available: {value}");
49
50 let greeting = BoxFunctionOnce::new(|name: &String| format!("hello, {name}"));
51 println!("FunctionOnce greeting: {}", greeting.apply(&value));
52
53 let mut call_count = 0;
54 let mut stateful = BoxStatefulFunction::new(move |input: &i32| {
55 call_count += 1;
56 input + call_count
57 });
58 println!("StatefulFunction first call: {}", stateful.apply(&40));
59 println!("StatefulFunction second call: {}", stateful.apply(&40));
60 println!();
61}Sourcepub fn new_with_name<F>(name: &str, f: F) -> Self
pub fn new_with_name<F>(name: &str, f: F) -> Self
Creates a new named function.
Wraps the provided closure and assigns it a name, which is useful for debugging and logging purposes.
Sourcepub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
Creates a new named function with an optional name.
Wraps the provided closure and assigns it an optional name.
Sourcepub fn clear_name(&mut self)
pub fn clear_name(&mut self)
Clears the name of this function.
Sourcepub fn when<P>(self, predicate: P) -> BoxConditionalFunction<T, R>where
T: 'static,
R: 'static,
P: Predicate<T> + 'static,
pub fn when<P>(self, predicate: P) -> BoxConditionalFunction<T, R>where
T: 'static,
R: 'static,
P: Predicate<T> + 'static,
Creates a conditional function that executes based on predicate result.
§Parameters
predicate- The predicate to determine whether to execute the function operation
§Returns
Returns a conditional function that only executes when the
predicate returns true.
§Examples
use qubit_function::{BoxFunction, Function};
fn or_else_zero(_: &i32) -> i32 {
0
}
let double = BoxFunction::new(|x: &i32| x * 2);
let conditional = double.when(|value: &i32| *value > 0);
assert_eq!(conditional.or_else(or_else_zero).apply(&5), 10); // executed
let double = BoxFunction::new(|x: &i32| x * 2);
let conditional = double.when(|value: &i32| *value > 0);
assert_eq!(conditional.or_else(or_else_zero).apply(&-3), 0); // not executedSourcepub fn and_then<S, F>(self, after: F) -> BoxFunction<T, S>where
T: 'static,
R: 'static,
S: 'static,
F: Function<R, S> + 'static,
pub fn and_then<S, F>(self, after: F) -> BoxFunction<T, S>where
T: 'static,
R: 'static,
S: 'static,
F: Function<R, S> + 'static,
Chains execution with another function, executing the current function first, then the subsequent function.
§Parameters
after- The subsequent function to execute after the current function completes
§Returns
Returns a new function that executes the current function and the subsequent function in sequence.
§Examples
use qubit_function::{BoxFunction, Function};
let double = BoxFunction::new(|x: &i32| x * 2);
let to_string = BoxFunction::new(|x: &i32| x.to_string());
let chained = double.and_then(to_string);
assert_eq!(chained.apply(&5), "10".to_string());Source§impl<T, R> BoxFunction<T, R>
impl<T, R> BoxFunction<T, R>
Sourcepub fn constant(value: R) -> BoxFunction<T, R>where
R: Clone + 'static,
pub fn constant(value: R) -> BoxFunction<T, R>where
R: Clone + 'static,
Creates a constant function
§Examples
/// ignore /// use qubit_function::{BoxFunction, Function}; /// /// let constant = BoxFunction::constant("hello"); /// assert_eq!(constant.apply(123), "hello"); ///
Source§impl<T> BoxFunction<T, T>
impl<T> BoxFunction<T, T>
Sourcepub fn identity() -> BoxFunction<T, T>where
T: Clone,
pub fn identity() -> BoxFunction<T, T>where
T: Clone,
Creates an identity function
§Examples
/// rust /// use qubit_function::BoxFunction; /// /// let identity = BoxFunction::<i32, i32>::identity(); /// assert_eq!(identity.apply(&42), 42); ///