Struct Checked

Source
pub struct Checked<T, D = WithDeref> { /* private fields */ }
Expand description

A wrapper around a numeric type, containing a valid value, that will perform overflow checks on arithmetic operations.

Any arithmetic operation (like Add, Sub…) will be performed using checked arithmetic and return a Unchecked type which will track overflow state. Additional chaining additional arithmetic operations is possible witoutchecking overflow in intermediate steps. Only when the final result is to be used, it’s neccessary to call Unchecked::check() to convert back to Checked value.

T is the inner type (u8, i16, etc.) wrapped by this type.

D is a marker controlling automatic conversion to inner type. It defaults to WithDeref which results in semi-implicit conversion to T available (like Deref). For values where handling overflow is particularily important and opting out of it could have serious consequences, WithoutDeref can be used, which will require calling an explicit conversion function to convert to the inner type.

Implementations§

Source§

impl<T, D> Checked<T, D>

Source

pub fn into_inner(self) -> T

Source§

impl<T, D> Checked<T, D>
where T: Clone,

Source

pub fn to_inner(&self) -> T

Source§

impl<T> Checked<T, WithDeref>

Source

pub fn new_with_deref(v: T) -> Checked<T, WithDeref>

Source

pub fn new(v: T) -> Self

Source§

impl<T> Checked<T, WithoutDeref>

Trait Implementations§

Source§

impl<T, D, Rhs> Add<Rhs> for Checked<T, D>
where T: CheckedAdd<Rhs>,

Source§

type Output = Unchecked<<T as CheckedAdd<Rhs>>::Output, D>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Rhs) -> Self::Output

Performs the + operation. Read more
Source§

impl<D> CheckedAdd<Checked<i128, D>> for i128

Source§

type Output = i128

Source§

fn checked_add(self, rhs: Checked<i128, D>) -> Option<Self::Output>

Source§

impl<D> CheckedAdd<Checked<i16, D>> for i16

Source§

type Output = i16

Source§

fn checked_add(self, rhs: Checked<i16, D>) -> Option<Self::Output>

Source§

impl<D> CheckedAdd<Checked<i32, D>> for i32

Source§

type Output = i32

Source§

fn checked_add(self, rhs: Checked<i32, D>) -> Option<Self::Output>

Source§

impl<D> CheckedAdd<Checked<i64, D>> for i64

Source§

type Output = i64

Source§

fn checked_add(self, rhs: Checked<i64, D>) -> Option<Self::Output>

Source§

impl<D> CheckedAdd<Checked<i8, D>> for i8

Source§

type Output = i8

Source§

fn checked_add(self, rhs: Checked<i8, D>) -> Option<Self::Output>

Source§

impl<D> CheckedAdd<Checked<isize, D>> for isize

Source§

impl<D> CheckedAdd<Checked<u128, D>> for u128

Source§

type Output = u128

Source§

fn checked_add(self, rhs: Checked<u128, D>) -> Option<Self::Output>

Source§

impl<D> CheckedAdd<Checked<u16, D>> for u16

Source§

type Output = u16

Source§

fn checked_add(self, rhs: Checked<u16, D>) -> Option<Self::Output>

Source§

impl<D> CheckedAdd<Checked<u32, D>> for u32

Source§

type Output = u32

Source§

fn checked_add(self, rhs: Checked<u32, D>) -> Option<Self::Output>

Source§

impl<D> CheckedAdd<Checked<u64, D>> for u64

Source§

type Output = u64

Source§

fn checked_add(self, rhs: Checked<u64, D>) -> Option<Self::Output>

Source§

impl<D> CheckedAdd<Checked<u8, D>> for u8

Source§

type Output = u8

Source§

fn checked_add(self, rhs: Checked<u8, D>) -> Option<Self::Output>

Source§

impl<D> CheckedAdd<Checked<usize, D>> for usize

Source§

impl<D> CheckedDiv<Checked<i128, D>> for i128

Source§

type Output = i128

Source§

fn checked_div(self, rhs: Checked<i128, D>) -> Option<Self::Output>

Source§

impl<D> CheckedDiv<Checked<i16, D>> for i16

Source§

type Output = i16

Source§

fn checked_div(self, rhs: Checked<i16, D>) -> Option<Self::Output>

Source§

impl<D> CheckedDiv<Checked<i32, D>> for i32

Source§

type Output = i32

Source§

fn checked_div(self, rhs: Checked<i32, D>) -> Option<Self::Output>

Source§

impl<D> CheckedDiv<Checked<i64, D>> for i64

Source§

type Output = i64

Source§

fn checked_div(self, rhs: Checked<i64, D>) -> Option<Self::Output>

Source§

impl<D> CheckedDiv<Checked<i8, D>> for i8

Source§

type Output = i8

Source§

fn checked_div(self, rhs: Checked<i8, D>) -> Option<Self::Output>

Source§

impl<D> CheckedDiv<Checked<isize, D>> for isize

Source§

impl<D> CheckedDiv<Checked<u128, D>> for u128

Source§

type Output = u128

Source§

fn checked_div(self, rhs: Checked<u128, D>) -> Option<Self::Output>

Source§

impl<D> CheckedDiv<Checked<u16, D>> for u16

Source§

type Output = u16

Source§

fn checked_div(self, rhs: Checked<u16, D>) -> Option<Self::Output>

Source§

impl<D> CheckedDiv<Checked<u32, D>> for u32

Source§

type Output = u32

Source§

fn checked_div(self, rhs: Checked<u32, D>) -> Option<Self::Output>

Source§

impl<D> CheckedDiv<Checked<u64, D>> for u64

Source§

type Output = u64

Source§

fn checked_div(self, rhs: Checked<u64, D>) -> Option<Self::Output>

Source§

impl<D> CheckedDiv<Checked<u8, D>> for u8

Source§

type Output = u8

Source§

fn checked_div(self, rhs: Checked<u8, D>) -> Option<Self::Output>

Source§

impl<D> CheckedDiv<Checked<usize, D>> for usize

Source§

impl<D> CheckedMul<Checked<i128, D>> for i128

Source§

type Output = i128

Source§

fn checked_mul(self, rhs: Checked<i128, D>) -> Option<Self::Output>

Source§

impl<D> CheckedMul<Checked<i16, D>> for i16

Source§

type Output = i16

Source§

fn checked_mul(self, rhs: Checked<i16, D>) -> Option<Self::Output>

Source§

impl<D> CheckedMul<Checked<i32, D>> for i32

Source§

type Output = i32

Source§

fn checked_mul(self, rhs: Checked<i32, D>) -> Option<Self::Output>

Source§

impl<D> CheckedMul<Checked<i64, D>> for i64

Source§

type Output = i64

Source§

fn checked_mul(self, rhs: Checked<i64, D>) -> Option<Self::Output>

Source§

impl<D> CheckedMul<Checked<i8, D>> for i8

Source§

type Output = i8

Source§

fn checked_mul(self, rhs: Checked<i8, D>) -> Option<Self::Output>

Source§

impl<D> CheckedMul<Checked<isize, D>> for isize

Source§

impl<D> CheckedMul<Checked<u128, D>> for u128

Source§

type Output = u128

Source§

fn checked_mul(self, rhs: Checked<u128, D>) -> Option<Self::Output>

Source§

impl<D> CheckedMul<Checked<u16, D>> for u16

Source§

type Output = u16

Source§

fn checked_mul(self, rhs: Checked<u16, D>) -> Option<Self::Output>

Source§

impl<D> CheckedMul<Checked<u32, D>> for u32

Source§

type Output = u32

Source§

fn checked_mul(self, rhs: Checked<u32, D>) -> Option<Self::Output>

Source§

impl<D> CheckedMul<Checked<u64, D>> for u64

Source§

type Output = u64

Source§

fn checked_mul(self, rhs: Checked<u64, D>) -> Option<Self::Output>

Source§

impl<D> CheckedMul<Checked<u8, D>> for u8

Source§

type Output = u8

Source§

fn checked_mul(self, rhs: Checked<u8, D>) -> Option<Self::Output>

Source§

impl<D> CheckedMul<Checked<usize, D>> for usize

Source§

impl<D> CheckedRem<Checked<i128, D>> for i128

Source§

type Output = i128

Source§

fn checked_rem(self, rhs: Checked<i128, D>) -> Option<Self::Output>

Source§

impl<D> CheckedRem<Checked<i16, D>> for i16

Source§

type Output = i16

Source§

fn checked_rem(self, rhs: Checked<i16, D>) -> Option<Self::Output>

Source§

impl<D> CheckedRem<Checked<i32, D>> for i32

Source§

type Output = i32

Source§

fn checked_rem(self, rhs: Checked<i32, D>) -> Option<Self::Output>

Source§

impl<D> CheckedRem<Checked<i64, D>> for i64

Source§

type Output = i64

Source§

fn checked_rem(self, rhs: Checked<i64, D>) -> Option<Self::Output>

Source§

impl<D> CheckedRem<Checked<i8, D>> for i8

Source§

type Output = i8

Source§

fn checked_rem(self, rhs: Checked<i8, D>) -> Option<Self::Output>

Source§

impl<D> CheckedRem<Checked<isize, D>> for isize

Source§

impl<D> CheckedRem<Checked<u128, D>> for u128

Source§

type Output = u128

Source§

fn checked_rem(self, rhs: Checked<u128, D>) -> Option<Self::Output>

Source§

impl<D> CheckedRem<Checked<u16, D>> for u16

Source§

type Output = u16

Source§

fn checked_rem(self, rhs: Checked<u16, D>) -> Option<Self::Output>

Source§

impl<D> CheckedRem<Checked<u32, D>> for u32

Source§

type Output = u32

Source§

fn checked_rem(self, rhs: Checked<u32, D>) -> Option<Self::Output>

Source§

impl<D> CheckedRem<Checked<u64, D>> for u64

Source§

type Output = u64

Source§

fn checked_rem(self, rhs: Checked<u64, D>) -> Option<Self::Output>

Source§

impl<D> CheckedRem<Checked<u8, D>> for u8

Source§

type Output = u8

Source§

fn checked_rem(self, rhs: Checked<u8, D>) -> Option<Self::Output>

Source§

impl<D> CheckedRem<Checked<usize, D>> for usize

Source§

impl<D> CheckedSub<Checked<i128, D>> for i128

Source§

type Output = i128

Source§

fn checked_sub(self, rhs: Checked<i128, D>) -> Option<Self::Output>

Source§

impl<D> CheckedSub<Checked<i16, D>> for i16

Source§

type Output = i16

Source§

fn checked_sub(self, rhs: Checked<i16, D>) -> Option<Self::Output>

Source§

impl<D> CheckedSub<Checked<i32, D>> for i32

Source§

type Output = i32

Source§

fn checked_sub(self, rhs: Checked<i32, D>) -> Option<Self::Output>

Source§

impl<D> CheckedSub<Checked<i64, D>> for i64

Source§

type Output = i64

Source§

fn checked_sub(self, rhs: Checked<i64, D>) -> Option<Self::Output>

Source§

impl<D> CheckedSub<Checked<i8, D>> for i8

Source§

type Output = i8

Source§

fn checked_sub(self, rhs: Checked<i8, D>) -> Option<Self::Output>

Source§

impl<D> CheckedSub<Checked<isize, D>> for isize

Source§

impl<D> CheckedSub<Checked<u128, D>> for u128

Source§

type Output = u128

Source§

fn checked_sub(self, rhs: Checked<u128, D>) -> Option<Self::Output>

Source§

impl<D> CheckedSub<Checked<u16, D>> for u16

Source§

type Output = u16

Source§

fn checked_sub(self, rhs: Checked<u16, D>) -> Option<Self::Output>

Source§

impl<D> CheckedSub<Checked<u32, D>> for u32

Source§

type Output = u32

Source§

fn checked_sub(self, rhs: Checked<u32, D>) -> Option<Self::Output>

Source§

impl<D> CheckedSub<Checked<u64, D>> for u64

Source§

type Output = u64

Source§

fn checked_sub(self, rhs: Checked<u64, D>) -> Option<Self::Output>

Source§

impl<D> CheckedSub<Checked<u8, D>> for u8

Source§

type Output = u8

Source§

fn checked_sub(self, rhs: Checked<u8, D>) -> Option<Self::Output>

Source§

impl<D> CheckedSub<Checked<usize, D>> for usize

Source§

impl<T, D> Clone for Checked<T, D>
where T: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, D: Debug> Debug for Checked<T, D>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for Checked<T, WithDeref>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T, D, Rhs> Div<Rhs> for Checked<T, D>
where T: CheckedDiv<Rhs>,

Source§

type Output = Unchecked<<T as CheckedDiv<Rhs>>::Output, D>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Rhs) -> Self::Output

Performs the / operation. Read more
Source§

impl<T, D> From<T> for Checked<T, D>

Source§

fn from(v: T) -> Self

Converts to this type from the input type.
Source§

impl<T, D, Rhs> Mul<Rhs> for Checked<T, D>
where T: CheckedMul<Rhs>,

Source§

type Output = Unchecked<<T as CheckedMul<Rhs>>::Output, D>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Rhs) -> Self::Output

Performs the * operation. Read more
Source§

impl<T, D1, D2> PartialEq<Checked<T, D1>> for Checked<T, D2>
where T: PartialEq<T>,

Source§

fn eq(&self, other: &Checked<T, D1>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, D, Rhs> Sub<Rhs> for Checked<T, D>
where T: CheckedSub<Rhs>,

Source§

type Output = Unchecked<<T as CheckedSub<Rhs>>::Output, D>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Rhs) -> Self::Output

Performs the - operation. Read more
Source§

impl<T, D> Copy for Checked<T, D>
where T: Copy,

Source§

impl<T, D1> Eq for Checked<T, D1>
where T: PartialEq<T>,

Auto Trait Implementations§

§

impl<T, D> Freeze for Checked<T, D>
where T: Freeze,

§

impl<T, D> RefUnwindSafe for Checked<T, D>

§

impl<T, D> Send for Checked<T, D>
where T: Send, D: Send,

§

impl<T, D> Sync for Checked<T, D>
where T: Sync, D: Sync,

§

impl<T, D> Unpin for Checked<T, D>
where T: Unpin, D: Unpin,

§

impl<T, D> UnwindSafe for Checked<T, D>
where T: UnwindSafe, D: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.