logo

Trait elliptic_curve::ops::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

UTerm - B0 = Term

UInt<UTerm, B1> - B1 = UTerm

Z0 - Z0 = Z0

UTerm - UTerm = UTerm

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

NInt - Z0 = NInt

PInt - Z0 = PInt

Z0 - N = P

Z0 - P = N

UInt - B0 = UInt

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

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

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

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

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

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