qubit-function 0.11.0

Functional programming traits and Box/Rc/Arc adapters for Rust, inspired by Java functional interfaces
Documentation
/*******************************************************************************
 *
 *    Copyright (c) 2025 - 2026.
 *    Haixing Hu, Qubit Co. Ltd.
 *
 *    All rights reserved.
 *
 ******************************************************************************/
//! Defines the `RcRunnable` public type.

#![allow(unused_imports)]

use super::*;

// ============================================================================
// RcRunnable
// ============================================================================

/// Single-threaded shared runnable.
///
/// `RcRunnable<E>` stores a `Rc<RefCell<dyn FnMut() -> Result<(), E>>>` and can
/// be called repeatedly through shared ownership.
///
/// # Type Parameters
///
/// * `E` - The error value returned when the action fails.
///
/// # Author
///
/// Haixing Hu
pub struct RcRunnable<E> {
    /// The stateful closure executed by this runnable.
    pub(super) function: Rc<RefCell<dyn FnMut() -> Result<(), E>>>,
    /// The optional name of this runnable.
    pub(super) name: Option<String>,
}

impl<E> Clone for RcRunnable<E> {
    #[inline]
    fn clone(&self) -> Self {
        Self {
            function: Rc::clone(&self.function),
            name: self.name.clone(),
        }
    }
}

impl<E> RcRunnable<E> {
    impl_common_new_methods!(
        (FnMut() -> Result<(), E> + 'static),
        |function| Rc::new(RefCell::new(function)),
        "runnable"
    );

    /// Creates a shared runnable from a reusable supplier.
    ///
    /// # Parameters
    ///
    /// * `supplier` - The supplier that produces the runnable result.
    ///
    /// # Returns
    ///
    /// A new `RcRunnable<E>`.
    #[inline]
    pub fn from_supplier<S>(supplier: S) -> Self
    where
        S: Supplier<Result<(), E>> + 'static,
    {
        Self::new(move || supplier.get())
    }

    impl_common_name_methods!("runnable");
}

impl<E> Runnable<E> for RcRunnable<E> {
    /// Executes the shared runnable.
    #[inline]
    fn run(&mut self) -> Result<(), E> {
        (self.function.borrow_mut())()
    }

    impl_rc_conversions!(
        RcRunnable<E>,
        BoxRunnable,
        FnMut() -> Result<(), E>
    );
}