pub unsafe trait UnwrapOverflowOpsSigned: UnwrapOverflowOps {
type Unsigned: UnwrapOverflowOpsUnsigned;
// Required methods
fn unwrap_add_unsigned(self, arg: Self::Unsigned) -> Self;
fn unwrap_sub_unsigned(self, arg: Self::Unsigned) -> Self;
fn unwrap_neg(self) -> Self;
}
Expand description
An extension trait for signed arithmetic operations that are guaranteed to panic on overflow.
This implements operations specific to signed integers,
the UnwrapOverflowOps
trait is for operations supported bya ll 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 Unsigned: UnwrapOverflowOpsUnsigned
type Unsigned: UnwrapOverflowOpsUnsigned
The unsigned integer type with the same size
Required Methods§
Sourcefn unwrap_add_unsigned(self, arg: Self::Unsigned) -> Self
fn unwrap_add_unsigned(self, arg: Self::Unsigned) -> Self
Strict addition with an unsigned 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. i32::strict_add_unsigned
).
§Panics
This function will always panic on overflow, regardless of whether overflow checks are enabled.
§Examples
Basic usage:
use unwrap_overflow_ops::*;
assert_eq!(1i32.unwrap_add_unsigned(2), 3);
The following panics because of overflow:
use unwrap_overflow_ops::*;
let _ = 1u32.unwrap_add_signed(-2);
Sourcefn unwrap_sub_unsigned(self, arg: Self::Unsigned) -> Self
fn unwrap_sub_unsigned(self, arg: Self::Unsigned) -> Self
Strict subtraction with an unsigned 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. i32::strict_sub_unsigned
).
§Panics
This function will always panic on overflow, regardless of whether overflow checks are enabled.
§Examples
Basic usage:
use unwrap_overflow_ops::*;
assert_eq!(1i32.unwrap_sub_unsigned(2), -1);
The following panics because of overflow:
use unwrap_overflow_ops::*;
let _ = (i32::MIN + 2).unwrap_sub_unsigned(3);
Sourcefn unwrap_neg(self) -> Self
fn unwrap_neg(self) -> Self
Strict negation. Computes -self
, panicking if self == MIN
.
This is a polyfill for the strict_overflow_ops
feature,
which offers equivalent methods for each primitive integer type (ex. i32::strict_neg
).
§Panics
This function will always panic on overflow, regardless of whether overflow checks are enabled.
§Examples
Basic usage:
use unwrap_overflow_ops::*;
assert_eq!(5i32.unwrap_neg(), -5);
The following panics because of overflow:
use unwrap_overflow_ops::*;
let _ = i32::MIN.unwrap_neg();
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.