Add basic definition of rem and remassign

This commit is contained in:
Gwilym Kuiper 2021-06-05 16:53:07 +01:00
parent d3b4e7eacf
commit e78c9abcec

View file

@ -1,6 +1,6 @@
use core::{ use core::{
fmt::Display, fmt::Display,
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}, ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign},
}; };
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
@ -96,6 +96,25 @@ where
} }
} }
impl<T, const N: usize> Rem<T> for Num<N>
where
T: Into<Num<N>>,
{
type Output = Self;
fn rem(self, modulus: T) -> Self::Output {
Num(self.0 % modulus.into().0)
}
}
impl<T, const N: usize> RemAssign<T> for Num<N>
where
T: Into<Num<N>>,
{
fn rem_assign(&mut self, modulus: T) {
self.0 = (*self % modulus).0
}
}
impl<const N: usize> Neg for Num<N> { impl<const N: usize> Neg for Num<N> {
type Output = Self; type Output = Self;
fn neg(self) -> Self::Output { fn neg(self) -> Self::Output {
@ -107,6 +126,7 @@ impl<const N: usize> Num<N> {
pub fn max() -> Self { pub fn max() -> Self {
Num(i32::MAX) Num(i32::MAX)
} }
pub fn min() -> Self { pub fn min() -> Self {
Num(i32::MIN) Num(i32::MIN)
} }