Struct CheckedInt

Source
#[repr(C)]
pub struct CheckedInt<T> { pub _phantom_0: PhantomData<UnsafeCell<T>>, pub mValue: T, pub mIsValid: bool, }
Expand description

@class CheckedInt @brief Integer wrapper class checking for integer overflow and other errors @param T the integer type to wrap. Can be any type among the following: - any basic integer type such as |int| - any stdint type such as |int8_t|

This class implements guarded integer arithmetic. Do a computation, check that isValid() returns true, you then have a guarantee that no problem, such as integer overflow, happened during this computation, and you can call value() to get the plain integer value.

The arithmetic operators in this class are guaranteed not to raise a signal (e.g. in case of a division by zero).

For example, suppose that you want to implement a function that computes (aX+aY)/aZ, that doesn’t crash if aZ==0, and that reports on error (divide by zero or integer overflow). You could code it as follows: @code bool computeXPlusYOverZ(int aX, int aY, int aZ, int* aResult) { CheckedInt checkedResult = (CheckedInt(aX) + aY) / aZ; if (checkedResult.isValid()) { aResult = checkedResult.value(); return true; } else { return false; } } @endcode

Implicit conversion from plain integers to checked integers is allowed. The plain integer is checked to be in range before being casted to the destination type. This means that the following lines all compile, and the resulting CheckedInts are correctly detected as valid or invalid: @code // 1 is of type int, is found to be in range for uint8_t, x is valid CheckedInt<uint8_t> x(1); // -1 is of type int, is found not to be in range for uint8_t, x is invalid CheckedInt<uint8_t> x(-1); // -1 is of type int, is found to be in range for int8_t, x is valid CheckedInt<int8_t> x(-1); // 1000 is of type int16_t, is found not to be in range for int8_t, // x is invalid CheckedInt<int8_t> x(int16_t(1000)); // 3123456789 is of type uint32_t, is found not to be in range for int32_t, // x is invalid CheckedInt<int32_t> x(uint32_t(3123456789)); @endcode Implicit conversion from checked integers to plain integers is not allowed. As shown in the above example, to get the value of a checked integer as a normal integer, call value().

Arithmetic operations between checked and plain integers is allowed; the result type is the type of the checked integer.

Checked integers of different types cannot be used in the same arithmetic expression.

There are convenience typedefs for all stdint types, of the following form (these are just 2 examples): @code typedef CheckedInt<int32_t> CheckedInt32; typedef CheckedInt<uint16_t> CheckedUint16; @endcode

Fields§

§_phantom_0: PhantomData<UnsafeCell<T>>§mValue: T§mIsValid: bool

Trait Implementations§

Source§

impl<T: Clone> Clone for CheckedInt<T>

Source§

fn clone(&self) -> CheckedInt<T>

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> Debug for CheckedInt<T>

Source§

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

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

impl<T: PartialEq> PartialEq for CheckedInt<T>

Source§

fn eq(&self, other: &CheckedInt<T>) -> 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: Copy> Copy for CheckedInt<T>

Source§

impl<T> StructuralPartialEq for CheckedInt<T>

Auto Trait Implementations§

§

impl<T> Freeze for CheckedInt<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for CheckedInt<T>

§

impl<T> Send for CheckedInt<T>
where T: Send,

§

impl<T> !Sync for CheckedInt<T>

§

impl<T> Unpin for CheckedInt<T>
where T: Unpin,

§

impl<T> UnwindSafe for CheckedInt<T>
where T: 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> Filterable for T

Source§

fn filterable( self, filter_name: &'static str, ) -> RequestFilterDataProvider<T, fn(DataRequest<'_>) -> bool>

Creates a filterable data provider with the given name for debugging. Read more
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<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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T