Skip to main content

qubit_function/transformers/transformer_once/
unary_operator_once.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `UnaryOperatorOnce` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// UnaryOperatorOnce Trait - Marker trait for TransformerOnce<T, T>
17// ============================================================================
18
19/// UnaryOperatorOnce trait - marker trait for one-time use unary operators
20///
21/// A one-time use unary operator transforms a value of type `T` to another
22/// value of the same type `T`, consuming self in the process. This trait
23/// extends `TransformerOnce<T, T>` to provide semantic clarity for same-type
24/// transformations with consuming semantics. Equivalent to Java's
25/// `UnaryOperator<T>` but with FnOnce semantics.
26///
27/// # Automatic Implementation
28///
29/// This trait is automatically implemented for all types that implement
30/// `TransformerOnce<T, T>`, so you don't need to implement it manually.
31///
32/// # Type Parameters
33///
34/// * `T` - The type of both input and output values
35///
36/// # Examples
37///
38/// ## Using in generic constraints
39///
40/// ```rust
41/// use qubit_function::{UnaryOperatorOnce, TransformerOnce};
42///
43/// fn apply<T, O>(value: T, op: O) -> T
44/// where
45///     O: UnaryOperatorOnce<T>,
46/// {
47///     op.apply(value)
48/// }
49///
50/// let double = |x: i32| x * 2;
51/// assert_eq!(apply(21, double), 42);
52/// ```
53///
54/// # Author
55///
56/// Haixing Hu
57pub trait UnaryOperatorOnce<T>: TransformerOnce<T, T> {}
58
59/// Blanket implementation of UnaryOperatorOnce for all TransformerOnce<T, T>
60///
61/// This automatically implements `UnaryOperatorOnce<T>` for any type that
62/// implements `TransformerOnce<T, T>`.
63///
64/// # Author
65///
66/// Haixing Hu
67impl<F, T> UnaryOperatorOnce<T> for F
68where
69    F: TransformerOnce<T, T>,
70{
71    // empty
72}