Skip to main content

UpcastAdd

Trait UpcastAdd 

Source
pub trait UpcastAdd<H: Add<Output = H> + Rem<Output = H>>:
    Upcast<Higher = H>
    + CheckedAdd
    + Copy {
    // Provided methods
    fn upcast_add(self, rhs: Self) -> H { ... }
    fn upcast_add_mod(self, rhs: Self, modulo: Self) -> Self { ... }
}
Expand description

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

Provided Methods§

Source

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

performs the operation self + rhs and returns value as type H.

let a = u8::MAX;
let b = u8::MAX;

// a + b would panic
let res = a.upcast_add(b);

let expected = (a as u16) * 2;
assert_eq!(res, expected);
Source

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

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

let a = u8::MAX;
let b = 10;
let modulo = u8::MAX;

let expected = b;

let res = a.upcast_add_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 UpcastAdd<i16> for i8

Source§

impl UpcastAdd<i32> for i16

Source§

impl UpcastAdd<i64> for i32

Source§

impl UpcastAdd<i128> for i64

Source§

impl UpcastAdd<u16> for u8

Source§

impl UpcastAdd<u32> for u16

Source§

impl UpcastAdd<u64> for u32

Source§

impl UpcastAdd<u128> for u64

Implementors§