Skip to main content

qubit_function/functions/function/
rc_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 `RcFunction` public type.
12
13use super::{
14    BoxFunction,
15    BoxFunctionOnce,
16    Function,
17    Predicate,
18    Rc,
19    RcConditionalFunction,
20    impl_function_clone,
21    impl_function_common_methods,
22    impl_function_constant_method,
23    impl_function_debug_display,
24    impl_function_identity_method,
25    impl_rc_conversions,
26    impl_shared_function_methods,
27};
28
29// ============================================================================
30// RcFunction - Rc<dyn Fn(&T) -> R>
31// ============================================================================
32
33/// RcFunction - single-threaded function wrapper
34///
35/// A single-threaded, clonable function wrapper optimized for scenarios
36/// that require sharing without thread-safety overhead.
37///
38/// # Features
39///
40/// - **Based on**: `Rc<dyn Fn(&T) -> R>`
41/// - **Ownership**: Shared ownership via reference counting (non-atomic)
42/// - **Reusability**: Can be called multiple times (each call consumes its
43///   input)
44/// - **Thread Safety**: Not thread-safe (no `Send + Sync`)
45/// - **Clonable**: Cheap cloning via `Rc::clone`
46///
47pub struct RcFunction<T, R> {
48    pub(super) function: Rc<dyn Fn(&T) -> R>,
49    pub(super) name: Option<String>,
50}
51
52impl<T, R> RcFunction<T, R> {
53    // Generates: new(), new_with_name(), new_with_optional_name(), name(), set_name()
54    impl_function_common_methods!(
55        RcFunction<T, R>,
56        (Fn(&T) -> R + 'static),
57        |f| Rc::new(f)
58    );
59
60    // Generates: when(), and_then(), compose()
61    impl_shared_function_methods!(
62        RcFunction<T, R>,
63        RcConditionalFunction,
64        into_rc,
65        Function,
66        'static
67    );
68}
69
70// Generates: constant() method for RcFunction<T, R>
71impl_function_constant_method!(RcFunction<T, R>, 'static);
72
73// Generates: identity() method for RcFunction<T, T>
74impl_function_identity_method!(RcFunction<T, T>);
75
76// Generates: Clone implementation for RcFunction<T, R>
77impl_function_clone!(RcFunction<T, R>);
78
79// Generates: Debug and Display implementations for RcFunction<T, R>
80impl_function_debug_display!(RcFunction<T, R>);
81
82// Implement Function trait for RcFunction<T, R>
83impl<T, R> Function<T, R> for RcFunction<T, R> {
84    fn apply(&self, t: &T) -> R {
85        (self.function)(t)
86    }
87
88    // Use macro to implement conversion methods
89    impl_rc_conversions!(
90        RcFunction<T, R>,
91        BoxFunction,
92        BoxFunctionOnce,
93        Fn(t: &T) -> R
94    );
95}