Skip to main content

CheckedAdd

Trait CheckedAdd 

Source
pub trait CheckedAdd<Rhs = Self> {
    type Output;
    type Error;

    // Required method
    fn cadd(self, rhs: Rhs) -> Result<Self::Output, Self::Error>;

    // Provided method
    fn saturating_add(self, rhs: Rhs) -> Self::Output
       where Self: Sized,
             Rhs: PartialOrd + Zero,
             Self::Output: Bounded { ... }
}
Expand description

Checked addition.

Required Associated Types§

Source

type Output

Result of addition.

Source

type Error

Usually ArithmeticError.

Required Methods§

Source

fn cadd(self, rhs: Rhs) -> Result<Self::Output, Self::Error>

Checked addition. Returns Err on overflow.

use fixnum::{FixedPoint, typenum::U9, ops::CheckedAdd};

type Amount = FixedPoint<i64, U9>;

let a: Amount = "0.1".parse()?;
let b: Amount = "0.2".parse()?;
let c: Amount = "0.3".parse()?;
assert_eq!(a.cadd(b)?, c);

Provided Methods§

Source

fn saturating_add(self, rhs: Rhs) -> Self::Output
where Self: Sized, Rhs: PartialOrd + Zero, Self::Output: Bounded,

Saturating addition. Computes self + rhs, saturating at the numeric bounds (MIN, MAX) instead of overflowing.

use fixnum::{FixedPoint, typenum::U9, ops::{Bounded, RoundMode::*, CheckedAdd}};

type Amount = FixedPoint<i64, U9>;

let a: Amount = "1000.00002".parse()?;
let b: Amount = "9222000000".parse()?;
let c: Amount = "9222001000.00002".parse()?;
// 1000.00002 + 9222000000 = 9222001000.00002
assert_eq!(a.saturating_add(b), c);

// 9222000000 + 9222000000 = MAX
assert_eq!(c.saturating_add(c), Amount::MAX);

let d: Amount = "-9222000000".parse()?;
// -9222000000 + (-9222000000) = MIN
assert_eq!(d.saturating_add(d), Amount::MIN);

Implementations on Foreign Types§

Source§

impl CheckedAdd for i8

Source§

type Output = i8

Source§

type Error = ArithmeticError

Source§

fn cadd(self, rhs: Self) -> Result<Self::Output, Self::Error>

Source§

fn saturating_add(self, rhs: Self) -> Self::Output

Source§

impl CheckedAdd for i16

Source§

type Output = i16

Source§

type Error = ArithmeticError

Source§

fn cadd(self, rhs: Self) -> Result<Self::Output, Self::Error>

Source§

fn saturating_add(self, rhs: Self) -> Self::Output

Source§

impl CheckedAdd for i32

Source§

type Output = i32

Source§

type Error = ArithmeticError

Source§

fn cadd(self, rhs: Self) -> Result<Self::Output, Self::Error>

Source§

fn saturating_add(self, rhs: Self) -> Self::Output

Source§

impl CheckedAdd for i64

Source§

type Output = i64

Source§

type Error = ArithmeticError

Source§

fn cadd(self, rhs: Self) -> Result<Self::Output, Self::Error>

Source§

fn saturating_add(self, rhs: Self) -> Self::Output

Source§

impl CheckedAdd for i128

Source§

type Output = i128

Source§

type Error = ArithmeticError

Source§

fn cadd(self, rhs: Self) -> Result<Self::Output, Self::Error>

Source§

fn saturating_add(self, rhs: Self) -> Self::Output

Implementors§

Source§

impl<P: Precision> CheckedAdd for FixedPoint<i16, P>

Available on crate feature i16 only.
Source§

impl<P: Precision> CheckedAdd for FixedPoint<i32, P>

Available on crate feature i32 only.
Source§

impl<P: Precision> CheckedAdd for FixedPoint<i64, P>

Available on crate feature i64 only.
Source§

impl<P: Precision> CheckedAdd for FixedPoint<i128, P>

Available on crate feature i128 only.