Skip to main content

UpcastSub

Trait UpcastSub 

Source
pub trait UpcastSub<H: Sub<Output = H> + Rem<Output = H>>:
    Upcast<Higher = H>
    + CheckedSub
    + Copy {
    // Provided methods
    fn upcast_sub(self, rhs: Self) -> H { ... }
    fn upcast_sub_mod(self, rhs: Self, modulo: Self) -> Self { ... }
}
Expand description

Trait to implement upcast sub. (Casting up when type bounds are hit during arithmetic)

Provided Methods§

Source

fn upcast_sub(self, rhs: Self) -> H

performs the operatoin self - rhs and returns value as type H.

let a = i8::MIN;
let b = 10;

let expected = i8::MIN as i16 - 10;

let res = a.upcast_sub(b);
assert_eq!(res, expected);
§Panics

Panics if the operation still exceeds the higher types limit.

let a = 1u8;
let b = 2u8;

let _ = a.upcast_sub(b);
Source

fn upcast_sub_mod(self, rhs: Self, modulo: Self) -> Self

performs the operation (self - rhs) % modulo and returns value as type Self.

let a = i8::MIN;
let b = 10;
let modulo = i8::MIN;

let expected = -10;

let res = a.upcast_sub_mod(b, modulo);
assert_eq!(res, expected);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl UpcastSub<i16> for i8

Source§

impl UpcastSub<i32> for i16

Source§

impl UpcastSub<i64> for i32

Source§

impl UpcastSub<i128> for i64

Source§

impl UpcastSub<u16> for u8

Source§

impl UpcastSub<u32> for u16

Source§

impl UpcastSub<u64> for u32

Source§

impl UpcastSub<u128> for u64

Implementors§