BoxBiFunction

Struct BoxBiFunction 

Source
pub struct BoxBiFunction<T, U, R> { /* private fields */ }
Expand description

BoxBiFunction - bi-function wrapper based on Box<dyn Fn>

A bi-function wrapper that provides single ownership with reusable computation. Borrows both inputs and can be called multiple times.

§Features

  • Based on: Box<dyn Fn(&T, &U) -> R>
  • Ownership: Single ownership, cannot be cloned
  • Reusability: Can be called multiple times (borrows inputs each time)
  • Thread Safety: Not thread-safe (no Send + Sync requirement)

§Author

Haixing Hu

Implementations§

Source§

impl<T, U, R> BoxBiFunction<T, U, R>
where T: 'static, U: 'static, R: 'static,

Source

pub fn new<F>(f: F) -> Self
where F: Fn(&T, &U) -> R + 'static,

Creates a new bi-function.

Wraps the provided closure in the appropriate smart pointer type for this bi-function implementation.

Source

pub fn new_with_name<F>(name: &str, f: F) -> Self
where F: Fn(&T, &U) -> R + 'static,

Creates a new named bi-function.

Wraps the provided closure and assigns it a name, which is useful for debugging and logging purposes.

Source

pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
where F: Fn(&T, &U) -> R + 'static,

Creates a new named bi-function with an optional name.

Wraps the provided closure and assigns it an optional name.

Source

pub fn name(&self) -> Option<&str>

Gets the name of this bi-function.

§Returns

Returns Some(&str) if a name was set, None otherwise.

Source

pub fn set_name(&mut self, name: &str)

Sets the name of this bi-function.

§Parameters
  • name - The name to set for this bi-function
Source

pub fn when<P>(self, predicate: P) -> BoxConditionalBiFunction<T, U, R>
where P: BiPredicate<T, U> + 'static,

Creates a conditional two-parameter function that executes based on bi-predicate result.

§Parameters
  • predicate - The bi-predicate to determine whether to execute the function operation
§Returns

Returns a conditional two-parameter function that only executes when the predicate returns true.

§Examples
use prism3_function::{BoxBiFunction, BiFunction};

let add = BoxBiFunction::new(|x: i32, y: i32| x + y);
let conditional = add.when(|x: &i32, y: &i32| *x > 0 && *y > 0);
assert_eq!(conditional.or_else(|_, _| 0).apply(2, 3), 5);  // executed
assert_eq!(conditional.or_else(|_, _| 0).apply(-1, 3), 0); // not executed
Source

pub fn and_then<S, F>(self, after: F) -> BoxBiFunction<T, U, S>
where S: 'static, F: Function<R, S> + 'static,

Chains execution with another two-parameter function, executing the current function first, then the subsequent function.

§Parameters
  • after - The subsequent one-parameter function to execute after the current function completes
§Returns

Returns a new two-parameter function that executes the current function and the subsequent function in sequence.

§Examples
use prism3_function::{BoxBiFunction, BoxFunction};

let add = BoxBiFunction::new(|x: i32, y: i32| x + y);
let multiply_by_two = BoxFunction::new(|z: i32| z * 2);

let chained = add.and_then(multiply_by_two);
assert_eq!(chained.apply(2, 3), 10); // (2+3) * 2 = 10
Source§

impl<T, U, R> BoxBiFunction<T, U, R>
where T: 'static, U: 'static, R: Clone + 'static,

Source

pub fn constant(value: R) -> BoxBiFunction<T, U, R>

Creates a constant function

§Examples

/// rust /// use prism3_function::{BoxBiFunction, BiFunction}; /// /// let constant = BoxBiFunction::constant("hello"); /// assert_eq!(constant.apply(123, "test"), "hello"); ///

Trait Implementations§

Source§

impl<T, U, R> BiFunction<T, U, R> for BoxBiFunction<T, U, R>

Source§

fn apply(&self, first: &T, second: &U) -> R

Applies the bi-function to two input references to produce an output value Read more
Source§

fn into_box(self) -> BoxBiFunction<T, U, R>
where T: 'static, U: 'static, R: 'static,

Converts to BoxBiFunction Read more
Source§

fn into_rc(self) -> RcBiFunction<T, U, R>
where T: 'static, U: 'static, R: 'static,

Converts to RcBiFunction Read more
Source§

fn into_fn(self) -> impl Fn(&T, &U) -> R

Converts bi-function to a closure Read more
Source§

fn into_once(self) -> BoxBiFunctionOnce<T, U, R>
where T: 'static, U: 'static, R: 'static,

Converts to BiFunctionOnce Read more
Source§

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

Converts to ArcBiFunction Read more
Source§

impl<T, U, R> Debug for BoxBiFunction<T, U, R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, U, R> Display for BoxBiFunction<T, U, R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, U, R> Freeze for BoxBiFunction<T, U, R>

§

impl<T, U, R> !RefUnwindSafe for BoxBiFunction<T, U, R>

§

impl<T, U, R> !Send for BoxBiFunction<T, U, R>

§

impl<T, U, R> !Sync for BoxBiFunction<T, U, R>

§

impl<T, U, R> Unpin for BoxBiFunction<T, U, R>

§

impl<T, U, R> !UnwindSafe for BoxBiFunction<T, U, R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.