Use better trait bounds for numbers

This commit is contained in:
Gwilym Kuiper 2021-06-05 15:54:50 +01:00
parent 1b5a6421fd
commit 0b8eda336d

View file

@ -12,55 +12,79 @@ impl<const N: usize> From<i32> for Num<N> {
}
}
impl<const N: usize> Add for Num<N> {
impl<T, const N: usize> Add<T> for Num<N>
where
T: Into<Num<N>>,
{
type Output = Self;
fn add(self, rhs: Num<N>) -> Self::Output {
Num(self.0 + rhs.0)
fn add(self, rhs: T) -> Self::Output {
Num(self.0 + rhs.into().0)
}
}
impl<const N: usize> AddAssign for Num<N> {
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.0
impl<T, const N: usize> AddAssign<T> for Num<N>
where
T: Into<Num<N>>,
{
fn add_assign(&mut self, rhs: T) {
self.0 += (*self + rhs.into()).0
}
}
impl<const N: usize> Sub for Num<N> {
impl<T, const N: usize> Sub<T> for Num<N>
where
T: Into<Num<N>>,
{
type Output = Self;
fn sub(self, rhs: Num<N>) -> Self::Output {
Num(self.0 - rhs.0)
fn sub(self, rhs: T) -> Self::Output {
Num(self.0 - rhs.into().0)
}
}
impl<const N: usize> SubAssign for Num<N> {
fn sub_assign(&mut self, rhs: Self) {
self.0 -= rhs.0
impl<T, const N: usize> SubAssign<T> for Num<N>
where
T: Into<Num<N>>,
{
fn sub_assign(&mut self, rhs: T) {
self.0 = (*self - rhs.into()).0
}
}
impl<const N: usize> Mul for Num<N> {
impl<T, const N: usize> Mul<T> for Num<N>
where
T: Into<Num<N>>,
{
type Output = Self;
fn mul(self, rhs: Num<N>) -> Self::Output {
Num((self.0 * rhs.0) >> N)
fn mul(self, rhs: T) -> Self::Output {
Num((self.0 * rhs.into().0) >> N)
}
}
impl<const N: usize> MulAssign for Num<N> {
fn mul_assign(&mut self, rhs: Self) {
self.0 = (*self * rhs).0
impl<T, const N: usize> MulAssign<T> for Num<N>
where
T: Into<Num<N>>,
{
fn mul_assign(&mut self, rhs: T) {
self.0 = (*self * rhs.into()).0
}
}
impl<const N: usize> Div for Num<N> {
impl<T, const N: usize> Div<T> for Num<N>
where
T: Into<Num<N>>,
{
type Output = Self;
fn div(self, rhs: Num<N>) -> Self::Output {
Num((self.0 << N) / rhs.0)
fn div(self, rhs: T) -> Self::Output {
Num((self.0 << N) / rhs.into().0)
}
}
impl<const N: usize> DivAssign for Num<N> {
fn div_assign(&mut self, rhs: Self) {
self.0 = (*self / rhs).0
impl<T, const N: usize> DivAssign<T> for Num<N>
where
T: Into<Num<N>>,
{
fn div_assign(&mut self, rhs: T) {
self.0 = (*self / rhs.into()).0
}
}
@ -92,16 +116,16 @@ impl<const N: usize> Num<N> {
fn test_numbers(_gba: &mut super::Gba) {
// test addition
let n: Num<8> = 1.into();
assert_eq!(n + 2.into(), 3.into(), "testing that 1 + 2 == 3");
assert_eq!(n + 2, 3.into(), "testing that 1 + 2 == 3");
// test multiplication
let n: Num<8> = 5.into();
assert_eq!(n * 3.into(), 15.into(), "testing that 5 * 3 == 15");
assert_eq!(n * 3, 15.into(), "testing that 5 * 3 == 15");
// test division
let n: Num<8> = 30.into();
let p: Num<8> = 3.into();
assert_eq!(n / 20.into(), p / 2.into(), "testing that 30 / 20 == 3 / 2");
assert_eq!(n / 20, p / 2, "testing that 30 / 20 == 3 / 2");
assert_ne!(n, p, "testing that 30 != 3");
}