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