Trait hacspec_lib::prelude::Sub

1.0.0 · source · []
pub trait Sub<Rhs = Self> {
    type Output;

    fn sub(self, rhs: Rhs) -> Self::Output;
}
Expand description

The subtraction operator -.

Note that Rhs is Self by default, but this is not mandatory. For example, std::time::SystemTime implements Sub<Duration>, which permits operations of the form SystemTime = SystemTime - Duration.

Examples

Subtractable points

use std::ops::Sub;

#[derive(Debug, Copy, Clone, PartialEq)]
struct Point {
    x: i32,
    y: i32,
}

impl Sub for Point {
    type Output = Self;

    fn sub(self, other: Self) -> Self::Output {
        Self {
            x: self.x - other.x,
            y: self.y - other.y,
        }
    }
}

assert_eq!(Point { x: 3, y: 3 } - Point { x: 2, y: 3 },
           Point { x: 1, y: 0 });

Implementing Sub with generics

Here is an example of the same Point struct implementing the Sub trait using generics.

use std::ops::Sub;

#[derive(Debug, PartialEq)]
struct Point<T> {
    x: T,
    y: T,
}

// Notice that the implementation uses the associated type `Output`.
impl<T: Sub<Output = T>> Sub for Point<T> {
    type Output = Self;

    fn sub(self, other: Self) -> Self::Output {
        Point {
            x: self.x - other.x,
            y: self.y - other.y,
        }
    }
}

assert_eq!(Point { x: 2, y: 3 } - Point { x: 1, y: 0 },
           Point { x: 1, y: 3 });

Required Associated Types

The resulting type after applying the - operator.

Required Methods

Performs the - operation.

Example
assert_eq!(12 - 1, 11);

Implementors

Warning: has wrapping semantics.

Warning: has wrapping semantics.

Warning: has wrapping semantics.

Warning: has wrapping semantics.

Warning: has wrapping semantics.

Warning: has wrapping semantics.

Warning: has wrapping semantics.

Warning: wraps on underflow.

Warning: has wrapping semantics.

Warning: wraps on underflow.

Warning: has wrapping semantics.

Warning: wraps on underflow.

Warning: has wrapping semantics.

Warning: wraps on underflow.

Warning: wraps on underflow.

Warning: wraps on underflow.

Warning: wraps on underflow.

Warning: wraps on underflow.

Polynomial subtraction on ℤ[x]

Polynomial subtraction on ℤ[x]

impl Sub<State> for State

impl Sub<Block> for Block

impl Sub<PolyKey> for PolyKey

impl Sub<State> for State

impl Sub<Row> for Row

impl Sub<Z0> for Z0

impl<U: Unsigned + NonZero> Sub<PInt<U>> for Z0

impl<U: Unsigned + NonZero> Sub<NInt<U>> for Z0

impl<U: Unsigned + NonZero> Sub<Z0> for PInt<U>

impl<U: Unsigned + NonZero> Sub<Z0> for NInt<U>

impl<Ul: Unsigned + NonZero, Ur: Unsigned + NonZero> Sub<NInt<Ur>> for PInt<Ul> where
    Ul: Add<Ur>,
    <Ul as Add<Ur>>::Output: Unsigned + NonZero

impl<Ul: Unsigned + NonZero, Ur: Unsigned + NonZero> Sub<PInt<Ur>> for NInt<Ul> where
    Ul: Add<Ur>,
    <Ul as Add<Ur>>::Output: Unsigned + NonZero

impl<Ul: Unsigned + NonZero, Ur: Unsigned + NonZero> Sub<PInt<Ur>> for PInt<Ul> where
    Ul: Cmp<Ur> + PrivateIntegerAdd<<Ul as Cmp<Ur>>::Output, Ur>, 

impl<Ul: Unsigned + NonZero, Ur: Unsigned + NonZero> Sub<NInt<Ur>> for NInt<Ul> where
    Ur: Cmp<Ul> + PrivateIntegerAdd<<Ur as Cmp<Ul>>::Output, Ul>, 

impl Sub<B0> for UTerm

impl<U: Unsigned, B: Bit> Sub<B0> for UInt<U, B>

impl<U: Unsigned, B: Bit> Sub<B1> for UInt<UInt<U, B>, B1>

impl Sub<B1> for UInt<UTerm, B1>

impl<U: Unsigned> Sub<B1> for UInt<U, B0> where
    U: Sub<B1>,
    Sub1<U>: Unsigned

impl Sub<UTerm> for UTerm

impl<Ul: Unsigned, Bl: Bit, Ur: Unsigned> Sub<Ur> for UInt<Ul, Bl> where
    UInt<Ul, Bl>: PrivateSub<Ur>,
    PrivateSubOut<UInt<Ul, Bl>, Ur>: Trim, 

impl Sub<ATerm> for ATerm

impl<Vl, Al, Vr, Ar> Sub<TArr<Vr, Ar>> for TArr<Vl, Al> where
    Vl: Sub<Vr>,
    Al: Sub<Ar>, 

impl Sub<Block> for Block

impl Sub<Word> for Word

impl Sub<Nonce> for Nonce

impl Sub<SBox> for SBox

impl Sub<RCon> for RCon

impl Sub<Key128> for Key128

impl Sub<Key256> for Key256

impl Sub<Block> for Block

impl Sub<Key> for Key

impl Sub<Tag> for Tag

impl Sub<DigestB> for DigestB

impl Sub<Scalar> for Scalar

impl Sub<Scalar> for Scalar

impl Sub<Scalar> for Scalar

impl Sub<Block> for Block

impl Sub<Digest> for Digest

impl Sub<Hash> for Hash