qubit-function 0.16.0

Functional programming traits and Box/Rc/Arc adapters for Rust, inspired by Java functional interfaces
// =============================================================================
//    Copyright (c) 2025 - 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Defines the `BoxBiConsumerOnce` public type.

use {
    super::BiConsumerOnceFn,
    crate::BiConsumerOnce,
    crate::consumers::macros::impl_box_consumer_methods,
    crate::consumers::macros::impl_consumer_common_methods,
    crate::consumers::macros::impl_consumer_debug_display,
    crate::macros::impl_closure_once_trait,
};
use {
    crate::BiPredicate,
    crate::BoxConditionalBiConsumerOnce,
};

// =======================================================================
// 2. BoxBiConsumerOnce - Single Ownership Implementation
// =======================================================================

/// BoxBiConsumerOnce struct
///
/// A one-time bi-consumer implementation based on
/// `Box<dyn FnOnce(&T, &U)>` for single ownership scenarios. This is the
/// simplest one-time bi-consumer type for truly one-time use.
///
/// # Features
///
/// - **Single Ownership**: Not cloneable, ownership moves on use
/// - **Runtime cost**: One heap allocation and dynamic dispatch; no reference
///   counting or locking
/// - **One-Time Use**: Consumes self on first call
/// - **Builder Pattern**: Method chaining consumes `self` naturally
///
/// # Use Cases
///
/// Choose `BoxBiConsumerOnce` when:
/// - The bi-consumer is truly used only once
/// - Building pipelines where ownership naturally flows
/// - The consumer captures values that should be consumed
/// - Performance is critical and sharing overhead is unacceptable
///
/// # Performance
///
/// `BoxBiConsumerOnce` has the best performance:
/// - No reference counting overhead
/// - No lock acquisition or runtime borrow checking
/// - Direct function call through vtable
/// - Minimal memory footprint (single pointer)
///
/// # Examples
///
/// ```rust
/// use qubit_function::{BiConsumerOnce, BoxBiConsumerOnce};
///
/// let consumer = BoxBiConsumerOnce::new(|x: &i32, y: &i32| {
///     println!("Sum: {}", x + y);
/// });
/// consumer.accept(&5, &3);
/// ```
#[must_use = "callback wrappers do nothing unless stored or invoked"]
pub struct BoxBiConsumerOnce<T, U> {
    /// The wrapped callback implementation.
    pub(super) function: Box<BiConsumerOnceFn<T, U>>,
    /// Diagnostic metadata associated with this callback.
    pub(super) metadata: crate::internal::CallbackMetadata,
}

// All methods require T: 'static and U: 'static because
// Box<dyn FnOnce(&T, &U)> requires it
impl<T, U> BoxBiConsumerOnce<T, U> {
    // Generates: new(), new_with_name(), name(), set_name(), noop()
    impl_consumer_common_methods!(
        BoxBiConsumerOnce<T, U>,
        (FnOnce(&T, &U) + 'static),
        |f| Box::new(f)
    );

    // Generates: when() and and_then() methods that consume self
    impl_box_consumer_methods!(
        BoxBiConsumerOnce<T, U>,
        BoxConditionalBiConsumerOnce,
        BiConsumerOnce
    );
}

impl<T, U> BiConsumerOnce<T, U> for BoxBiConsumerOnce<T, U> {
    #[inline(always)]
    fn accept(self, first: &T, second: &U) {
        (self.function)(first, second)
    }
}

// Use macro to generate Debug and Display implementations
impl_consumer_debug_display!(BoxBiConsumerOnce<T, U>);

// =======================================================================
// 3. Implement BiConsumerOnce trait for closures
// =======================================================================

// Implement BiConsumerOnce for all FnOnce(&T, &U) using macro
impl_closure_once_trait!(
    BiConsumerOnce<T, U>,
    accept,
    BoxBiConsumerOnce,
    FnOnce(first: &T, second: &U)
);