From 357ef46731fe39dd4bb29ba35d7616aa25c1382a Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 12 Feb 2023 12:14:10 +0100 Subject: [PATCH] types: add helper for division + remainder We frequently require these operations together when converting between bit and byte offsets, or similar purposes. --- src/types.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/types.rs b/src/types.rs index 9aa4b7d..9f0cf0d 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,6 +1,6 @@ use std::{ fmt::Debug, - ops::{Index, IndexMut}, + ops::{Div, Index, IndexMut, Rem}, }; use itertools::iproduct; @@ -61,3 +61,25 @@ impl IndexMut for ChunkArray { &mut self.0[index.z.0 as usize][index.x.0 as usize] } } + +pub trait DivRem { + type DivOutput; + type RemOutput; + + fn div_rem(self, rhs: Rhs) -> (Self::DivOutput, Self::RemOutput); +} + +impl DivRem for Lhs +where + Self: Div, + Self: Rem, + Self: Copy, + Rhs: Copy, +{ + type DivOutput = >::Output; + type RemOutput = >::Output; + + fn div_rem(self, rhs: Rhs) -> (Self::DivOutput, Self::RemOutput) { + (self / rhs, self % rhs) + } +}