Trait ethers::core::k256::elliptic_curve::ops::AddAssign1.8.0[][src]

pub trait AddAssign<Rhs = Self> {
    fn add_assign(&mut self, rhs: Rhs);
}
Expand description

The addition assignment operator +=.

Examples

This example creates a Point struct that implements the AddAssign trait, and then demonstrates add-assigning to a mutable Point.

use std::ops::AddAssign;

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

impl AddAssign for Point {
    fn add_assign(&mut self, other: Self) {
        *self = Self {
            x: self.x + other.x,
            y: self.y + other.y,
        };
    }
}

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

Required methods

Performs the += operation.

Example
let mut x: u32 = 12;
x += 1;
assert_eq!(x, 13);

Implementations on Foreign Types

Implements the += operator for appending to a String.

This has the same behavior as the push_str method.

Adds another BitVec into self, zero-extending the shorter.

BitVec addition works just like adding numbers longhand on paper. The first bits in the BitVec are the highest, so addition works from right to left, and the shorter BitVec is assumed to be extended to the left with zero.

The output BitVec may be one bit longer than the longer input, if addition overflowed.

Numeric arithmetic is provided on BitVec as a convenience. Serious numeric computation on variable-length integers should use the num_bigint crate instead, which is written specifically for that use case. BitVecs are not intended for arithmetic, and bitvec makes no guarantees about sustained correctness in arithmetic at this time.

Adds another BitVec into self.

Examples
use bitvec::prelude::*;

let mut a = bitvec![1, 0, 0, 1];
let b = bitvec![0, 1, 1, 1];
a += b;
assert_eq!(a, bitvec![1, 0, 0, 0, 0]);

Performs unsigned addition in place on a BitSlice.

If the addend bitstream is shorter than self, the addend is zero-extended at the left (so that its final bit matches with self’s final bit). If the addend is longer, the excess front length is unused.

Addition proceeds from the right ends of each slice towards the left. Because this trait is forbidden from returning anything, the final carry-out bit is discarded.

Note that, unlike BitVec, there is no subtraction implementation until I find a subtraction algorithm that does not require modifying the subtrahend.

Subtraction can be implemented by negating the intended subtrahend yourself and then using addition, or by using BitVecs instead of BitSlices.

Type Parameters

  • I: IntoIterator<Item=bool, IntoIter: DoubleEndedIterator>: The bitstream to add into self. It must be finite and double-ended, since addition operates in reverse.

Performs unsigned wrapping addition in place.

Examples

This example shows addition of a slice wrapping from max to zero.

use bitvec::prelude::*;

let mut src = [0b1110_1111u8, 0b0000_0001];
let bits = src.bits_mut::<Msb0>();
let (nums, one) = bits.split_at_mut(12);
let (accum, steps) = nums.split_at_mut(4);
*accum += one.iter().copied();
assert_eq!(accum, &steps[.. 4]);
*accum += one.iter().copied();
assert_eq!(accum, &steps[4 ..]);

Implementors