qubit_function/transformers/transformer/unary_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 `UnaryOperator` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// UnaryOperator Trait - Marker trait for Transformer<T, T>
17// ============================================================================
18
19/// UnaryOperator trait - marker trait for unary operators
20///
21/// A unary operator transforms a value of type `T` to another value of the
22/// same type `T`. This trait extends `Transformer<T, T>` to provide semantic
23/// clarity for same-type transformations. Equivalent to Java's `UnaryOperator<T>`
24/// which extends `Function<T, T>`.
25///
26/// # Automatic Implementation
27///
28/// This trait is automatically implemented for all types that implement
29/// `Transformer<T, T>`, so you don't need to implement it manually.
30///
31/// # Type Parameters
32///
33/// * `T` - The type of both input and output values
34///
35/// # Examples
36///
37/// ## Using in generic constraints
38///
39/// ```rust
40/// use qubit_function::{UnaryOperator, Transformer};
41///
42/// fn apply_twice<T, O>(value: T, op: O) -> T
43/// where
44/// O: UnaryOperator<T>,
45/// T: Clone,
46/// {
47/// let result = op.apply(value.clone());
48/// op.apply(result)
49/// }
50///
51/// let increment = |x: i32| x + 1;
52/// assert_eq!(apply_twice(5, increment), 7); // (5 + 1) + 1
53/// ```
54///
55/// ## With concrete types
56///
57/// ```rust
58/// use qubit_function::{BoxUnaryOperator, UnaryOperator, Transformer};
59///
60/// fn create_incrementer() -> BoxUnaryOperator<i32> {
61/// BoxUnaryOperator::new(|x| x + 1)
62/// }
63///
64/// let op = create_incrementer();
65/// assert_eq!(op.apply(41), 42);
66/// ```
67///
68/// # Author
69///
70/// Haixing Hu
71pub trait UnaryOperator<T>: Transformer<T, T> {}
72
73/// Blanket implementation of UnaryOperator for all Transformer<T, T>
74///
75/// This automatically implements `UnaryOperator<T>` for any type that
76/// implements `Transformer<T, T>`.
77///
78/// # Author
79///
80/// Haixing Hu
81impl<F, T> UnaryOperator<T> for F
82where
83 F: Transformer<T, T>,
84{
85 // empty
86}