Skip to main content

CarryingAdd

Trait CarryingAdd 

Source
pub trait CarryingAdd: Sized + Add<Self> {
    // Required method
    fn carrying_add(
        self,
        rhs: Self,
        carry: bool,
    ) -> (<Self as Add<Self>>::Output, bool);
}
Expand description

Performs addition with a carry bit, for chaining multi-word additions.

Required Methods§

Source

fn carrying_add( self, rhs: Self, carry: bool, ) -> (<Self as Add<Self>>::Output, bool)

Calculates self + rhs + carry and returns a tuple containing the sum and the output carry, performing the full addition rather than stopping at the first overflow.

For unsigned types this allows chaining together multiple additions to create a wider addition. For signed types the second tuple field signals two’s-complement overflow instead of a carry.

use const_num_traits::CarryingAdd;

// u8::MAX + 1 + carry
assert_eq!(CarryingAdd::carrying_add(u8::MAX, 1, true), (1, true));
assert_eq!(CarryingAdd::carrying_add(5u8, 2, false), (7, false));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl CarryingAdd for i8

Source§

fn carrying_add(self, rhs: Self, carry: bool) -> (i8, bool)

Source§

impl CarryingAdd for i16

Source§

fn carrying_add(self, rhs: Self, carry: bool) -> (i16, bool)

Source§

impl CarryingAdd for i32

Source§

fn carrying_add(self, rhs: Self, carry: bool) -> (i32, bool)

Source§

impl CarryingAdd for i64

Source§

fn carrying_add(self, rhs: Self, carry: bool) -> (i64, bool)

Source§

impl CarryingAdd for i128

Source§

fn carrying_add(self, rhs: Self, carry: bool) -> (i128, bool)

Source§

impl CarryingAdd for isize

Source§

fn carrying_add(self, rhs: Self, carry: bool) -> (isize, bool)

Source§

impl CarryingAdd for u8

Source§

fn carrying_add(self, rhs: Self, carry: bool) -> (u8, bool)

Source§

impl CarryingAdd for u16

Source§

fn carrying_add(self, rhs: Self, carry: bool) -> (u16, bool)

Source§

impl CarryingAdd for u32

Source§

fn carrying_add(self, rhs: Self, carry: bool) -> (u32, bool)

Source§

impl CarryingAdd for u64

Source§

fn carrying_add(self, rhs: Self, carry: bool) -> (u64, bool)

Source§

impl CarryingAdd for u128

Source§

fn carrying_add(self, rhs: Self, carry: bool) -> (u128, bool)

Source§

impl CarryingAdd for usize

Source§

fn carrying_add(self, rhs: Self, carry: bool) -> (usize, bool)

Implementors§