CheckedAdd

Trait CheckedAdd 

Source
pub trait CheckedAdd: Sized {
    // Required method
    fn checked_add(&self, other: &Self) -> Option<Self>;
}
Expand description

Numeric type supporting overflow-checked addition.

The trait is implemented for all primitive types and can be implemented manually for custom newtypes. Please note that CheckedSum also requires the item type to implement Default.

use checked_sum::{CheckedAdd, CheckedSum};

#[derive(Debug, PartialEq, Default)]
struct MyInt(u32);

impl CheckedAdd for MyInt {
    fn checked_add(&self, other: &Self) -> Option<Self> {
        self.0.checked_add(other.0).map(MyInt)
    }
}

let numbers = vec![MyInt(1), MyInt(2), MyInt(3)];
assert_eq!(numbers.into_iter().checked_sum(), Some(MyInt(6)));

Required Methods§

Source

fn checked_add(&self, other: &Self) -> Option<Self>

Adds two numbers checking for overflow, returns None if overflow occurred.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl CheckedAdd for i8

Source§

fn checked_add(&self, other: &Self) -> Option<Self>

Source§

impl CheckedAdd for i16

Source§

fn checked_add(&self, other: &Self) -> Option<Self>

Source§

impl CheckedAdd for i32

Source§

fn checked_add(&self, other: &Self) -> Option<Self>

Source§

impl CheckedAdd for i64

Source§

fn checked_add(&self, other: &Self) -> Option<Self>

Source§

impl CheckedAdd for i128

Source§

fn checked_add(&self, other: &Self) -> Option<Self>

Source§

impl CheckedAdd for isize

Source§

fn checked_add(&self, other: &Self) -> Option<Self>

Source§

impl CheckedAdd for u8

Source§

fn checked_add(&self, other: &Self) -> Option<Self>

Source§

impl CheckedAdd for u16

Source§

fn checked_add(&self, other: &Self) -> Option<Self>

Source§

impl CheckedAdd for u32

Source§

fn checked_add(&self, other: &Self) -> Option<Self>

Source§

impl CheckedAdd for u64

Source§

fn checked_add(&self, other: &Self) -> Option<Self>

Source§

impl CheckedAdd for u128

Source§

fn checked_add(&self, other: &Self) -> Option<Self>

Source§

impl CheckedAdd for usize

Source§

fn checked_add(&self, other: &Self) -> Option<Self>

Implementors§