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