pub unsafe trait UnwrapOverflowOpsUnsigned: UnwrapOverflowOps {
type Signed: UnwrapOverflowOpsSigned;
// Required method
fn unwrap_add_signed(self, arg: Self::Signed) -> Self;
}
Expand description
An extension trait for unsigned arithmetic operations that are guaranteed to panic on overflow.
This implements operations specific to unsigned integers,
the UnwrapOverflowOps
trait is for operations supported by all integers.
This is a polyfill for the strict_overflow_ops
feature.
These operations are only implemented for primitive integer types.
§Safety
These methods are guarenteed to check for overflow,
regardless of compiler settings and cfg!(...)
flags.
The correctness of these methods can be relied upon for memory safety.
Required Associated Types§
Sourcetype Signed: UnwrapOverflowOpsSigned
type Signed: UnwrapOverflowOpsSigned
The signed integer type with the same size
Required Methods§
Sourcefn unwrap_add_signed(self, arg: Self::Signed) -> Self
fn unwrap_add_signed(self, arg: Self::Signed) -> Self
Strict addition with a signed integer. Computes self + rhs
, panicking if overflow occurred.
This is a polyfill for the strict_overflow_ops
feature,
which offers equivalent methods for each primitive integer type (ex. u32::strict_add_signed
).
§Panics
This function will always panic on overflow, regardless of whether overflow checks are enabled.
§Examples
Basic usage:
use unwrap_overflow_ops::*;
assert_eq!(1u32.unwrap_add_signed(2), 3);
The following panics because of overflow:
use unwrap_overflow_ops::*;
let _ = 1u32.unwrap_add_signed(-2);
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.