Skip to main content

qubit_function/functions/bi_function/
rc_bi_function.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `RcBiFunction` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// RcBiFunction - Rc<dyn Fn(&T, &U) -> R>
17// ============================================================================
18
19/// RcBiFunction - single-threaded bi-function wrapper
20///
21/// A single-threaded, clonable bi-function wrapper optimized for scenarios
22/// that require sharing without thread-safety overhead.
23///
24/// # Features
25///
26/// - **Based on**: `Rc<dyn Fn(&T, &U) -> R>`
27/// - **Ownership**: Shared ownership via reference counting (non-atomic)
28/// - **Reusability**: Can be called multiple times (borrows inputs each time)
29/// - **Thread Safety**: Not thread-safe (no `Send + Sync`)
30/// - **Clonable**: Cheap cloning via `Rc::clone`
31///
32/// # Author
33///
34/// Haixing Hu
35pub struct RcBiFunction<T, U, R> {
36    pub(super) function: Rc<dyn Fn(&T, &U) -> R>,
37    pub(super) name: Option<String>,
38}
39
40impl<T, U, R> RcBiFunction<T, U, R> {
41    impl_function_common_methods!(
42        RcBiFunction<T, U, R>,
43        (Fn(&T, &U) -> R + 'static),
44        |f| Rc::new(f)
45    );
46    impl_shared_function_methods!(
47        RcBiFunction<T, U, R>,
48        RcConditionalBiFunction,
49        into_rc,
50        Function,
51        'static
52    );
53}
54
55// Implement BiFunction trait for RcBiFunction
56impl<T, U, R> BiFunction<T, U, R> for RcBiFunction<T, U, R> {
57    fn apply(&self, first: &T, second: &U) -> R {
58        (self.function)(first, second)
59    }
60
61    // Generate into_box(), into_rc(), into_fn(), into_once(), to_box(), to_rc(), to_fn(), to_once()
62    impl_rc_conversions!(
63        RcBiFunction<T, U, R>,
64        BoxBiFunction,
65        BoxBiFunctionOnce,
66        Fn(first: &T, second: &U) -> R
67    );
68}
69
70// Implement constant method for RcBiFunction
71impl_function_constant_method!(RcBiFunction<T, U, R>);
72
73// Implement Debug and Display for RcBiFunction
74impl_function_debug_display!(RcBiFunction<T, U, R>);
75
76// Implement Clone for RcBiFunction
77impl_function_clone!(RcBiFunction<T, U, R>);