Trait UnwrapOverflowOps

Source
pub unsafe trait UnwrapOverflowOps:
    Copy
    + Debug
    + Sized
    + Sealed {
    // Required methods
    fn unwrap_add(self, arg: Self) -> Self;
    fn unwrap_sub(self, arg: Self) -> Self;
    fn unwrap_mul(self, arg: Self) -> Self;
    fn unwrap_div(self, arg: Self) -> Self;
    fn unwrap_rem(self, arg: Self) -> Self;
    fn unwrap_shr(self, arg: u32) -> Self;
    fn unwrap_shl(self, arg: u32) -> Self;
    fn unwrap_pow(self, arg: u32) -> Self;
}
Expand description

An extension trait for arithmetic operations that are guaranteed to panic on overflow.

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 Methods§

Source

fn unwrap_add(self, arg: Self) -> Self

Strict integer addition. 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).

§Panics

This function will always panic on overflow, regardless of whether overflow checks are enabled.

§Examples

Basic usage:

use unwrap_overflow_ops::*;
assert_eq!((i32::MAX - 2).unwrap_add(1), i32::MAX - 1);

The following panics because of overflow:

use unwrap_overflow_ops::*;
let _ = (i32::MAX - 2).unwrap_add(3);
Source

fn unwrap_sub(self, arg: Self) -> Self

Strict integer subtraction. 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).

§Panics

This function will always panic on overflow, regardless of whether overflow checks are enabled.

§Examples

Basic usage:

use unwrap_overflow_ops::*;
assert_eq!((i32::MIN + 2).unwrap_sub(1), i32::MIN + 1);

The following panics because of overflow:

use unwrap_overflow_ops::*;
let _ = (i32::MIN + 2).unwrap_sub(3);
Source

fn unwrap_mul(self, arg: Self) -> Self

Strict integer multiplication. 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_mul).

§Panics

This function will always panic on overflow, regardless of whether overflow checks are enabled.

§Examples

Basic usage:

use unwrap_overflow_ops::*;
assert_eq!(i32::MAX.unwrap_mul(1), i32::MAX);

The following panics because of overflow:

use unwrap_overflow_ops::*;
let _ = i32::MAX.unwrap_mul(2);
Source

fn unwrap_div(self, arg: Self) -> Self

Strict integer division. 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_div).

§Panics

This function will always panic on overflow, regardless of whether overflow checks are enabled.

§Examples

Basic usage:

use unwrap_overflow_ops::*;
assert_eq!((i32::MIN + 1).unwrap_div(-1), 2147483647);

The following panics because of overflow:

use unwrap_overflow_ops::*;
let _ = i32::MIN.unwrap_div(-1);
Source

fn unwrap_rem(self, arg: Self) -> Self

Strict integer remainder. Computes self % rhs, panicking if the division results in overflow.

This is a polyfill for the strict_overflow_ops feature, which offers equivalent methods for each primitive integer type (ex. i32::strict_rem).

§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_rem(2), 1);

The following panics because of overflow:

use unwrap_overflow_ops::*;
let _ = 5i32.unwrap_rem(0);
Source

fn unwrap_shr(self, arg: u32) -> Self

Strict shift right. Computes self >> rhs, panicking rhs is larger than or equal to the number of bits in self.

This is a polyfill for the strict_overflow_ops feature, which offers equivalent methods for each primitive integer type (ex. i32::strict_shr).

§Panics

This function will always panic on overflow, regardless of whether overflow checks are enabled.

§Examples

Basic usage:

use unwrap_overflow_ops::*;
assert_eq!(0x10i32.unwrap_shr(4), 0x1);

The following panics because of overflow:

use unwrap_overflow_ops::*;
let _ = 0x10i32.unwrap_shr(128);
Source

fn unwrap_shl(self, arg: u32) -> Self

Strict shift left. Computes self << rhs, panicking if rhs is larger than or equal to the number of bits in self.

This is a polyfill for the strict_overflow_ops feature, which offers equivalent methods for each primitive integer type (ex. i32::strict_shl).

§Panics

This function will always panic on overflow, regardless of whether overflow checks are enabled.

§Examples

Basic usage:

use unwrap_overflow_ops::*;
assert_eq!(0x1i32.unwrap_shl(4), 0x10);

The following panics because of overflow:

use unwrap_overflow_ops::*;
let _ = 0x1i32.unwrap_shl(129);
Source

fn unwrap_pow(self, arg: u32) -> Self

Strict exponentiation. Computes self.pow(exp), 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_pow).

§Panics

This function will always panic on overflow, regardless of whether overflow checks are enabled.

§Examples

Basic usage:

use unwrap_overflow_ops::*;
assert_eq!(8i32.unwrap_pow(2), 64);

The following panics because of overflow:

use unwrap_overflow_ops::*;
let _ = i32::MAX.unwrap_pow(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.

Implementations on Foreign Types§

Source§

impl UnwrapOverflowOps for i8

Source§

fn unwrap_add(self, other: Self) -> Self

Source§

fn unwrap_sub(self, other: Self) -> Self

Source§

fn unwrap_mul(self, other: Self) -> Self

Source§

fn unwrap_div(self, other: Self) -> Self

Source§

fn unwrap_rem(self, other: Self) -> Self

Source§

fn unwrap_shr(self, other: u32) -> Self

Source§

fn unwrap_shl(self, other: u32) -> Self

Source§

fn unwrap_pow(self, other: u32) -> Self

Source§

impl UnwrapOverflowOps for i16

Source§

fn unwrap_add(self, other: Self) -> Self

Source§

fn unwrap_sub(self, other: Self) -> Self

Source§

fn unwrap_mul(self, other: Self) -> Self

Source§

fn unwrap_div(self, other: Self) -> Self

Source§

fn unwrap_rem(self, other: Self) -> Self

Source§

fn unwrap_shr(self, other: u32) -> Self

Source§

fn unwrap_shl(self, other: u32) -> Self

Source§

fn unwrap_pow(self, other: u32) -> Self

Source§

impl UnwrapOverflowOps for i32

Source§

fn unwrap_add(self, other: Self) -> Self

Source§

fn unwrap_sub(self, other: Self) -> Self

Source§

fn unwrap_mul(self, other: Self) -> Self

Source§

fn unwrap_div(self, other: Self) -> Self

Source§

fn unwrap_rem(self, other: Self) -> Self

Source§

fn unwrap_shr(self, other: u32) -> Self

Source§

fn unwrap_shl(self, other: u32) -> Self

Source§

fn unwrap_pow(self, other: u32) -> Self

Source§

impl UnwrapOverflowOps for i64

Source§

fn unwrap_add(self, other: Self) -> Self

Source§

fn unwrap_sub(self, other: Self) -> Self

Source§

fn unwrap_mul(self, other: Self) -> Self

Source§

fn unwrap_div(self, other: Self) -> Self

Source§

fn unwrap_rem(self, other: Self) -> Self

Source§

fn unwrap_shr(self, other: u32) -> Self

Source§

fn unwrap_shl(self, other: u32) -> Self

Source§

fn unwrap_pow(self, other: u32) -> Self

Source§

impl UnwrapOverflowOps for i128

Source§

fn unwrap_add(self, other: Self) -> Self

Source§

fn unwrap_sub(self, other: Self) -> Self

Source§

fn unwrap_mul(self, other: Self) -> Self

Source§

fn unwrap_div(self, other: Self) -> Self

Source§

fn unwrap_rem(self, other: Self) -> Self

Source§

fn unwrap_shr(self, other: u32) -> Self

Source§

fn unwrap_shl(self, other: u32) -> Self

Source§

fn unwrap_pow(self, other: u32) -> Self

Source§

impl UnwrapOverflowOps for isize

Source§

fn unwrap_add(self, other: Self) -> Self

Source§

fn unwrap_sub(self, other: Self) -> Self

Source§

fn unwrap_mul(self, other: Self) -> Self

Source§

fn unwrap_div(self, other: Self) -> Self

Source§

fn unwrap_rem(self, other: Self) -> Self

Source§

fn unwrap_shr(self, other: u32) -> Self

Source§

fn unwrap_shl(self, other: u32) -> Self

Source§

fn unwrap_pow(self, other: u32) -> Self

Source§

impl UnwrapOverflowOps for u8

Source§

fn unwrap_add(self, other: Self) -> Self

Source§

fn unwrap_sub(self, other: Self) -> Self

Source§

fn unwrap_mul(self, other: Self) -> Self

Source§

fn unwrap_div(self, other: Self) -> Self

Source§

fn unwrap_rem(self, other: Self) -> Self

Source§

fn unwrap_shr(self, other: u32) -> Self

Source§

fn unwrap_shl(self, other: u32) -> Self

Source§

fn unwrap_pow(self, other: u32) -> Self

Source§

impl UnwrapOverflowOps for u16

Source§

fn unwrap_add(self, other: Self) -> Self

Source§

fn unwrap_sub(self, other: Self) -> Self

Source§

fn unwrap_mul(self, other: Self) -> Self

Source§

fn unwrap_div(self, other: Self) -> Self

Source§

fn unwrap_rem(self, other: Self) -> Self

Source§

fn unwrap_shr(self, other: u32) -> Self

Source§

fn unwrap_shl(self, other: u32) -> Self

Source§

fn unwrap_pow(self, other: u32) -> Self

Source§

impl UnwrapOverflowOps for u32

Source§

fn unwrap_add(self, other: Self) -> Self

Source§

fn unwrap_sub(self, other: Self) -> Self

Source§

fn unwrap_mul(self, other: Self) -> Self

Source§

fn unwrap_div(self, other: Self) -> Self

Source§

fn unwrap_rem(self, other: Self) -> Self

Source§

fn unwrap_shr(self, other: u32) -> Self

Source§

fn unwrap_shl(self, other: u32) -> Self

Source§

fn unwrap_pow(self, other: u32) -> Self

Source§

impl UnwrapOverflowOps for u64

Source§

fn unwrap_add(self, other: Self) -> Self

Source§

fn unwrap_sub(self, other: Self) -> Self

Source§

fn unwrap_mul(self, other: Self) -> Self

Source§

fn unwrap_div(self, other: Self) -> Self

Source§

fn unwrap_rem(self, other: Self) -> Self

Source§

fn unwrap_shr(self, other: u32) -> Self

Source§

fn unwrap_shl(self, other: u32) -> Self

Source§

fn unwrap_pow(self, other: u32) -> Self

Source§

impl UnwrapOverflowOps for u128

Source§

fn unwrap_add(self, other: Self) -> Self

Source§

fn unwrap_sub(self, other: Self) -> Self

Source§

fn unwrap_mul(self, other: Self) -> Self

Source§

fn unwrap_div(self, other: Self) -> Self

Source§

fn unwrap_rem(self, other: Self) -> Self

Source§

fn unwrap_shr(self, other: u32) -> Self

Source§

fn unwrap_shl(self, other: u32) -> Self

Source§

fn unwrap_pow(self, other: u32) -> Self

Source§

impl UnwrapOverflowOps for usize

Source§

fn unwrap_add(self, other: Self) -> Self

Source§

fn unwrap_sub(self, other: Self) -> Self

Source§

fn unwrap_mul(self, other: Self) -> Self

Source§

fn unwrap_div(self, other: Self) -> Self

Source§

fn unwrap_rem(self, other: Self) -> Self

Source§

fn unwrap_shr(self, other: u32) -> Self

Source§

fn unwrap_shl(self, other: u32) -> Self

Source§

fn unwrap_pow(self, other: u32) -> Self

Implementors§