Trait hacspec_lib::prelude::Add

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

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

The addition operator +.

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

Examples

Addable points

use std::ops::Add;

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

impl Add for Point {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        Self {
            x: self.x + other.x,
            y: self.y + other.y,
        }
    }
}

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

Implementing Add with generics

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

use std::ops::Add;

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

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

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

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

Required Associated Types

The resulting type after applying the + operator.

Required Methods

Performs the + operation.

Example
assert_eq!(12 + 1, 13);

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 overflow.

Warning: has wrapping semantics.

Warning: wraps on overflow.

Warning: has wrapping semantics.

Warning: wraps on overflow.

Warning: has wrapping semantics.

Warning: wraps on overflow.

Warning: wraps on overflow.

Warning: wraps on overflow.

Warning: wraps on overflow.

Warning: wraps on overflow.

Implements the + operator for concatenating two strings.

This consumes the String on the left-hand side and re-uses its buffer (growing it if necessary). This is done to avoid allocating a new String and copying the entire contents on every operation, which would lead to O(n^2) running time when building an n-byte string by repeated concatenation.

The string on the right-hand side is only borrowed; its contents are copied into the returned String.

Examples

Concatenating two Strings takes the first by value and borrows the second:

let a = String::from("hello");
let b = String::from(" world");
let c = a + &b;
// `a` is moved and can no longer be used here.

If you want to keep using the first String, you can clone it and append to the clone instead:

let a = String::from("hello");
let b = String::from(" world");
let c = a.clone() + &b;
// `a` is still valid here.

Concatenating &str slices can be done by converting the first to a String:

let a = "hello";
let b = " world";
let c = a.to_string() + b;

Polynomial addition on ℤ[x]

Polynomial addition on ℤ[x]

impl Add<State> for State

impl Add<Block> for Block

impl Add<PolyKey> for PolyKey

impl Add<State> for State

impl Add<Row> for Row

impl<I: Integer> Add<I> for Z0

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

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

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

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

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

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

impl Add<B0> for UTerm

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

impl Add<B1> for UTerm

impl<U: Unsigned> Add<B1> for UInt<U, B0>

impl<U: Unsigned> Add<B1> for UInt<U, B1> where
    U: Add<B1>,
    Add1<U>: Unsigned

impl<U: Unsigned> Add<U> for UTerm

impl<U: Unsigned, B: Bit> Add<UTerm> for UInt<U, B>

impl<Ul: Unsigned, Ur: Unsigned> Add<UInt<Ur, B0>> for UInt<Ul, B0> where
    Ul: Add<Ur>, 

impl<Ul: Unsigned, Ur: Unsigned> Add<UInt<Ur, B1>> for UInt<Ul, B0> where
    Ul: Add<Ur>, 

impl<Ul: Unsigned, Ur: Unsigned> Add<UInt<Ur, B0>> for UInt<Ul, B1> where
    Ul: Add<Ur>, 

impl<Ul: Unsigned, Ur: Unsigned> Add<UInt<Ur, B1>> for UInt<Ul, B1> where
    Ul: Add<Ur>,
    Sum<Ul, Ur>: Add<B1>, 

impl Add<ATerm> for ATerm

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

impl Add<Block> for Block

impl Add<Word> for Word

impl Add<Nonce> for Nonce

impl Add<SBox> for SBox

impl Add<RCon> for RCon

impl Add<Key128> for Key128

impl Add<Key256> for Key256

impl Add<Block> for Block

impl Add<Key> for Key

impl Add<Tag> for Tag

impl Add<DigestB> for DigestB

impl Add<Scalar> for Scalar

impl Add<Scalar> for Scalar

impl Add<Scalar> for Scalar

impl Add<Block> for Block

impl Add<Digest> for Digest

impl Add<Hash> for Hash