types: add helper for division + remainder

We frequently require these operations together when converting between
bit and byte offsets, or similar purposes.
This commit is contained in:
Matthias Schiffer 2023-02-12 12:14:10 +01:00
parent a174d627cd
commit 357ef46731
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C

View file

@ -1,6 +1,6 @@
use std::{
fmt::Debug,
ops::{Index, IndexMut},
ops::{Div, Index, IndexMut, Rem},
};
use itertools::iproduct;
@ -61,3 +61,25 @@ impl<T> IndexMut<ChunkCoords> for ChunkArray<T> {
&mut self.0[index.z.0 as usize][index.x.0 as usize]
}
}
pub trait DivRem<Rhs> {
type DivOutput;
type RemOutput;
fn div_rem(self, rhs: Rhs) -> (Self::DivOutput, Self::RemOutput);
}
impl<Lhs, Rhs> DivRem<Rhs> for Lhs
where
Self: Div<Rhs>,
Self: Rem<Rhs>,
Self: Copy,
Rhs: Copy,
{
type DivOutput = <Self as Div<Rhs>>::Output;
type RemOutput = <Self as Rem<Rhs>>::Output;
fn div_rem(self, rhs: Rhs) -> (Self::DivOutput, Self::RemOutput) {
(self / rhs, self % rhs)
}
}