Skip to main content

qubit_function/transformers/transformer_once/
unary_operator_once.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 `UnaryOperatorOnce` public type.
12
13use super::TransformerOnce;
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///
54pub trait UnaryOperatorOnce<T>: TransformerOnce<T, T> {}
55
56/// Blanket implementation of UnaryOperatorOnce for all TransformerOnce<T, T>
57///
58/// This automatically implements `UnaryOperatorOnce<T>` for any type that
59/// implements `TransformerOnce<T, T>`.
60///
61impl<F, T> UnaryOperatorOnce<T> for F
62where
63    F: TransformerOnce<T, T>,
64{
65    // empty
66}