Class Subspace :: sus :: num :: OverflowInteger
An integer type that handles overflow instead of panicing.
The value inside the integer can be accessed or unwrapped like with an
Option
, which will panic if the integer has
overflowed. Or it can be converted into an Option
that will represent the overflow state as None
.
This type is useful for performing a series of operations as a unit, and
then checking for overflow after. It satisfies the
Sum
and Product
concepts
so can be used with sum
and
product
for iterators over integers.
Examples
Using OverflowInteger to sum an iterator of integers and look for overflow after without panicking.
auto a = sus::Array<i32, 2>(2, i32::MAX);
auto maybe_answer =
a.iter().copied().product<sus::num::OverflowInteger<i32>>();
sus_check(maybe_answer.is_overflow());
Static Methods
Constructs an OverflowInteger
from the same subspace integer type.
Implementation note
Because OverflowInteger
is constructible from a smaller integer, but
I
is also constructible from a smaller integer, which can then be used
to construct OverflowInteger
, on MSVC this conversion becomes ambiguous.
So we use convertible_to
to make the constructor a template,
which allows the compiler to choose one.
https://developercommunity.visualstudio.com/t/Ambiguous-conversion-with-two-paths-of-d/10461863
std::constructible_from<OverflowInteger<I>, U>
Satisfies sus::construct::From<OverflowInteger<I>, U>
if the
OverflowInteger
is constructible from U
.
sus::mem::IsMoveRef<decltype(it)>
sus::mem::IsMoveRef<decltype(it)>
Constructs an OverflowInteger
from an Iterator
by computing the
product of all elements in the iterator.
This method should rarely be called directly, as it is used to satisfy the
sus::iter::Product
concept.
This method satisfies sus::iter::Product<OverflowInteger<T>, T>
as well
as sus::iter::Product<OverflowInteger<T>, OverflowInteger<T>>
, for a
subspace integer type T
.
If an iterator yields a subspace integer type, iter.product()
would
panic on overflow. So instead iter.product<OverflowInteger<T>>()
can be
used (for integer type T
) which will perform the product computation and
return an OverflowInteger
without ever panicking.
sus::construct::TryFrom<I, U>
Satisfies sus::construct::TryFrom<OverflowInteger<I>, U>
if the inner
integer type I
satisfies sus::construct::TryFrom<I, U>
.
Methods
Converts the OverflowInteger
into an Option
that contains the integer
value. If overflow has occured, returns None
.