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
13use super::Transformer;
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///
68pub trait UnaryOperator<T>: Transformer<T, T> {}
69
70/// Blanket implementation of UnaryOperator for all Transformer<T, T>
71///
72/// This automatically implements `UnaryOperator<T>` for any type that
73/// implements `Transformer<T, T>`.
74///
75impl<F, T> UnaryOperator<T> for F
76where
77 F: Transformer<T, T>,
78{
79 // empty
80}