UnaryOperator

Trait UnaryOperator 

Source
pub trait UnaryOperator<T>: Transformer<T, T> { }
Expand description

UnaryOperator trait - marker trait for unary operators

A unary operator transforms a value of type T to another value of the same type T. This trait extends Transformer<T, T> to provide semantic clarity for same-type transformations. Equivalent to Java’s UnaryOperator<T> which extends Function<T, T>.

§Automatic Implementation

This trait is automatically implemented for all types that implement Transformer<T, T>, so you don’t need to implement it manually.

§Type Parameters

  • T - The type of both input and output values

§Examples

§Using in generic constraints

use prism3_function::{UnaryOperator, Transformer};

fn apply_twice<T, O>(value: T, op: O) -> T
where
    O: UnaryOperator<T>,
    T: Clone,
{
    let result = op.apply(value.clone());
    op.apply(result)
}

let increment = |x: i32| x + 1;
assert_eq!(apply_twice(5, increment), 7); // (5 + 1) + 1

§With concrete types

use prism3_function::{BoxUnaryOperator, UnaryOperator, Transformer};

fn create_incrementer() -> BoxUnaryOperator<i32> {
    BoxUnaryOperator::new(|x| x + 1)
}

let op = create_incrementer();
assert_eq!(op.apply(41), 42);

§Author

Hu Haixing

Implementors§

Source§

impl<F, T> UnaryOperator<T> for F
where F: Transformer<T, T>, T: 'static,

Blanket implementation of UnaryOperator for all Transformer<T, T>

This automatically implements UnaryOperator<T> for any type that implements Transformer<T, T>.

§Author

Hu Haixing