logo

Trait rocket::mtls::oid::asn1_rs::nom::lib::std::ops::Sub

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

    fn sub(self, rhs: Rhs) -> Self::Output;
}
Available on crate feature mtls only.
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);

Implementations on Foreign Types

Returns the amount of time elapsed from another instant to this one, or zero duration if that instant is later than this one.

Panics

Previous rust versions panicked when other was later than self. Currently this method saturates. Future versions may reintroduce the panic in some circumstances. See Monotonicity.

Subtract the sub-day time of the Duration from the Time. Wraps on overflow.

assert_eq!(time!(14:00) - 2.hours(), time!(12:00));
assert_eq!(time!(23:59:59) - (-2).seconds(), time!(0:00:01));

Subtract the sub-day time of the std::time::Duration from the Time. Wraps on overflow.

assert_eq!(time!(14:00) - 2.std_hours(), time!(12:00));
assert_eq!(time!(0:00:01) - 2.std_seconds(), time!(23:59:59));

Subtract two Times, returning the Duration between. This assumes both Times are in the same calendar day.

assert_eq!(time!(0:00) - time!(0:00), 0.seconds());
assert_eq!(time!(1:00) - time!(0:00), 1.hours());
assert_eq!(time!(0:00) - time!(1:00), (-1).hours());
assert_eq!(time!(0:00) - time!(23:00), (-23).hours());

Returns the set difference, cloned into a new set.

Values are collected in the same order that they appear in self.

Returns the difference of self and rhs as a new HashSet<T, S>.

Examples
use hashbrown::HashSet;

let a: HashSet<_> = vec![1, 2, 3].into_iter().collect();
let b: HashSet<_> = vec![3, 4, 5].into_iter().collect();

let set = &a - &b;

let mut i = 0;
let expected = [1, 2];
for x in &set {
    assert!(expected.contains(x));
    i += 1;
}
assert_eq!(i, expected.len());

Subtracting unsigned integers. We just do our PrivateSub and then Trim the output.

Z0 - N = P

N(Ul) - P(Ur) = N(Ul + Ur)

PInt - Z0 = PInt

P(Ul) - P(Ur): We resolve this with our PrivateAdd

UTerm - B0 = Term

UInt<U, B0> - B1 = UInt<U - B1, B1>

Z0 - Z0 = Z0

UInt - B0 = UInt

P(Ul) - N(Ur) = P(Ul + Ur)

UTerm - UTerm = UTerm

UInt<U, B1> - B1 = UInt<U, B0>

UInt<UTerm, B1> - B1 = UTerm

N(Ul) - N(Ur): We resolve this with our PrivateAdd

NInt - Z0 = NInt

Z0 - P = N

Implementors