Skip to main content

qubit_function/transformers/bi_transformer/
binary_operator.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 `BinaryOperator` public type.
12
13#![allow(unused_imports)]
14
15use super::*;
16
17// ============================================================================
18// BinaryOperator Trait - Marker trait for BiTransformer<T, T, T>
19// ============================================================================
20
21/// BinaryOperator trait - marker trait for binary operators
22///
23/// A binary operator takes two values of type `T` and produces a value of the
24/// same type `T`. This trait extends `BiTransformer<T, T, T>` to provide
25/// semantic clarity for same-type binary operations. Equivalent to Java's
26/// `BinaryOperator<T>` which extends `BiFunction<T, T, T>`.
27///
28/// # Automatic Implementation
29///
30/// This trait is automatically implemented for all types that implement
31/// `BiTransformer<T, T, T>`, so you don't need to implement it manually.
32///
33/// # Type Parameters
34///
35/// * `T` - The type of both input values and the output value
36///
37/// # Examples
38///
39/// ## Using in generic constraints
40///
41/// ```rust
42/// use qubit_function::{BinaryOperator, BiTransformer};
43///
44/// fn reduce<T, O>(values: Vec<T>, initial: T, op: O) -> T
45/// where
46///     O: BinaryOperator<T>,
47///     T: Clone,
48/// {
49///     values.into_iter().fold(initial, |acc, val| op.apply(acc, val))
50/// }
51///
52/// let sum = |a: i32, b: i32| a + b;
53/// assert_eq!(reduce(vec![1, 2, 3, 4], 0, sum), 10);
54/// ```
55///
56/// ## With concrete types
57///
58/// ```rust
59/// use qubit_function::{BoxBinaryOperator, BinaryOperator, BiTransformer};
60///
61/// fn create_adder() -> BoxBinaryOperator<i32> {
62///     BoxBinaryOperator::new(|x, y| x + y)
63/// }
64///
65/// let op = create_adder();
66/// assert_eq!(op.apply(20, 22), 42);
67/// ```
68///
69pub trait BinaryOperator<T>: BiTransformer<T, T, T> {}
70
71/// Blanket implementation of BinaryOperator for all BiTransformer<T, T, T>
72///
73/// This automatically implements `BinaryOperator<T>` for any type that
74/// implements `BiTransformer<T, T, T>`.
75///
76impl<F, T> BinaryOperator<T> for F
77where
78    F: BiTransformer<T, T, T>,
79{
80    // empty
81}