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§
Sourcefn upcast_add(self, rhs: Self) -> H
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);
Sourcefn upcast_add_mod(self, rhs: Self, modulo: Self) -> Self
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", so this trait is not object safe.