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