qubit_function/transformers/bi_transformer/binary_operator.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `BinaryOperator` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
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///
67/// # Author
68///
69/// Haixing Hu
70pub trait BinaryOperator<T>: BiTransformer<T, T, T> {}
71
72/// Blanket implementation of BinaryOperator for all BiTransformer<T, T, T>
73///
74/// This automatically implements `BinaryOperator<T>` for any type that
75/// implements `BiTransformer<T, T, T>`.
76///
77/// # Author
78///
79/// Haixing Hu
80impl<F, T> BinaryOperator<T> for F
81where
82 F: BiTransformer<T, T, T>,
83{
84 // empty
85}