From d7a51ceffd8744889f8de62836c861994c0bb123 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Tue, 27 Feb 2024 19:16:45 -0800 Subject: [PATCH] `fn BitDepth::pxstride`: Implement generically over `stride` type, e.x. `isize` or `usize`. This way we don't have to do `BD::pxstride(stride as usize) as isize`, which is verbose and wrong for negative strides. There may still be some places where strides are always positive, though, so it's kept generic. This also lets us fix #636 gradually; this is the first major step in fixing #636. --- include/common/bitdepth.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/include/common/bitdepth.rs b/include/common/bitdepth.rs index 79ee15062..4970f02a5 100644 --- a/include/common/bitdepth.rs +++ b/include/common/bitdepth.rs @@ -8,8 +8,11 @@ use std::fmt::Display; use std::fmt::Formatter; use std::mem; use std::ops::Add; +use std::ops::Div; use std::ops::Mul; +use std::ops::Rem; use std::ops::Shr; +use to_method::To as _; pub trait FromPrimitive { fn from_prim(t: T) -> Self; @@ -174,9 +177,13 @@ pub trait BitDepth: Clone + Copy { clip(pixel, 0.into(), self.bitdepth_max()) } - fn pxstride(n: usize) -> usize { - let scale = mem::size_of::(); - debug_assert!(n % scale == 0); + /// `T` is generally meant to be `usize` or `isize`. + fn pxstride(n: T) -> T + where + T: Copy + Eq + TryFrom + From + Div + Rem, + { + let scale = mem::size_of::().try_to::().ok().unwrap(); + debug_assert!(n % scale == 0.into()); n / scale }