mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-24 00:31:34 +11:00
Use better trait bounds for numbers
This commit is contained in:
parent
1b5a6421fd
commit
0b8eda336d
|
@ -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;
|
type Output = Self;
|
||||||
fn add(self, rhs: Num<N>) -> Self::Output {
|
fn add(self, rhs: T) -> Self::Output {
|
||||||
Num(self.0 + rhs.0)
|
Num(self.0 + rhs.into().0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> AddAssign for Num<N> {
|
impl<T, const N: usize> AddAssign<T> for Num<N>
|
||||||
fn add_assign(&mut self, rhs: Self) {
|
where
|
||||||
self.0 += rhs.0
|
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;
|
type Output = Self;
|
||||||
fn sub(self, rhs: Num<N>) -> Self::Output {
|
fn sub(self, rhs: T) -> Self::Output {
|
||||||
Num(self.0 - rhs.0)
|
Num(self.0 - rhs.into().0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> SubAssign for Num<N> {
|
impl<T, const N: usize> SubAssign<T> for Num<N>
|
||||||
fn sub_assign(&mut self, rhs: Self) {
|
where
|
||||||
self.0 -= rhs.0
|
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;
|
type Output = Self;
|
||||||
fn mul(self, rhs: Num<N>) -> Self::Output {
|
fn mul(self, rhs: T) -> Self::Output {
|
||||||
Num((self.0 * rhs.0) >> N)
|
Num((self.0 * rhs.into().0) >> N)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> MulAssign for Num<N> {
|
impl<T, const N: usize> MulAssign<T> for Num<N>
|
||||||
fn mul_assign(&mut self, rhs: Self) {
|
where
|
||||||
self.0 = (*self * rhs).0
|
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;
|
type Output = Self;
|
||||||
fn div(self, rhs: Num<N>) -> Self::Output {
|
fn div(self, rhs: T) -> Self::Output {
|
||||||
Num((self.0 << N) / rhs.0)
|
Num((self.0 << N) / rhs.into().0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> DivAssign for Num<N> {
|
impl<T, const N: usize> DivAssign<T> for Num<N>
|
||||||
fn div_assign(&mut self, rhs: Self) {
|
where
|
||||||
self.0 = (*self / rhs).0
|
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) {
|
fn test_numbers(_gba: &mut super::Gba) {
|
||||||
// test addition
|
// test addition
|
||||||
let n: Num<8> = 1.into();
|
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
|
// test multiplication
|
||||||
let n: Num<8> = 5.into();
|
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
|
// test division
|
||||||
let n: Num<8> = 30.into();
|
let n: Num<8> = 30.into();
|
||||||
let p: Num<8> = 3.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");
|
assert_ne!(n, p, "testing that 30 != 3");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue