pub trait CheckedSub<Rhs = Self> {
type Output;
type Error;
// Required method
fn csub(self, rhs: Rhs) -> Result<Self::Output, Self::Error>;
// Provided method
fn saturating_sub(self, rhs: Rhs) -> Self::Output
where Self: Sized,
Rhs: PartialOrd + Zero,
Self::Output: Bounded { ... }
}Expand description
Checked subtraction.
Required Associated Types§
Sourcetype Error
type Error
Usually ArithmeticError.
Required Methods§
Sourcefn csub(self, rhs: Rhs) -> Result<Self::Output, Self::Error>
fn csub(self, rhs: Rhs) -> Result<Self::Output, Self::Error>
Checked subtraction. Returns Err on overflow.
use fixnum::{FixedPoint, typenum::U9, ops::CheckedSub};
type Amount = FixedPoint<i64, U9>;
let a: Amount = "0.3".parse()?;
let b: Amount = "0.1".parse()?;
let c: Amount = "0.2".parse()?;
assert_eq!(a.csub(b)?, c);Provided Methods§
Sourcefn saturating_sub(self, rhs: Rhs) -> Self::Output
fn saturating_sub(self, rhs: Rhs) -> Self::Output
Saturating subtraction. Computes self - rhs, saturating at the numeric bounds
(MIN, MAX) instead of overflowing.
use fixnum::{FixedPoint, typenum::U9, ops::{Bounded, RoundMode::*, CheckedSub}};
type Amount = FixedPoint<i64, U9>;
let a: Amount = "9222001000.00002".parse()?;
let b: Amount = "9222000000".parse()?;
let c: Amount = "1000.00002".parse()?;
// 9222001000.00002 - 9222000000 = 1000.00002
assert_eq!(a.saturating_sub(b), c);
let d: Amount = "-9222000000".parse()?;
// 9222000000 - (-9222000000) = MAX
assert_eq!(b.saturating_sub(d), Amount::MAX);
// -9222000000 - 9222000000 = MIN
assert_eq!(d.saturating_sub(b), Amount::MIN);Implementations on Foreign Types§
Source§impl CheckedSub for i8
impl CheckedSub for i8
Source§impl CheckedSub for i16
impl CheckedSub for i16
Source§impl CheckedSub for i32
impl CheckedSub for i32
Source§impl CheckedSub for i64
impl CheckedSub for i64
Source§impl CheckedSub for i128
impl CheckedSub for i128
Implementors§
Source§impl<P: Precision> CheckedSub for FixedPoint<i16, P>
Available on crate feature i16 only.
impl<P: Precision> CheckedSub for FixedPoint<i16, P>
Available on crate feature
i16 only.type Output = FixedPoint<i16, P>
type Error = ArithmeticError
Source§impl<P: Precision> CheckedSub for FixedPoint<i32, P>
Available on crate feature i32 only.
impl<P: Precision> CheckedSub for FixedPoint<i32, P>
Available on crate feature
i32 only.type Output = FixedPoint<i32, P>
type Error = ArithmeticError
Source§impl<P: Precision> CheckedSub for FixedPoint<i64, P>
Available on crate feature i64 only.
impl<P: Precision> CheckedSub for FixedPoint<i64, P>
Available on crate feature
i64 only.type Output = FixedPoint<i64, P>
type Error = ArithmeticError
Source§impl<P: Precision> CheckedSub for FixedPoint<i128, P>
Available on crate feature i128 only.
impl<P: Precision> CheckedSub for FixedPoint<i128, P>
Available on crate feature
i128 only.