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