Skip to content

Commit

Permalink
fn BitDepth::pxstride: Implement generically over stride type, e.…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
kkysen committed Feb 28, 2024
1 parent 8105376 commit d7a51ce
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions include/common/bitdepth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
fn from_prim(t: T) -> Self;
Expand Down Expand Up @@ -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::<Self::Pixel>();
debug_assert!(n % scale == 0);
/// `T` is generally meant to be `usize` or `isize`.
fn pxstride<T>(n: T) -> T
where
T: Copy + Eq + TryFrom<usize> + From<u8> + Div<Output = T> + Rem<Output = T>,
{
let scale = mem::size_of::<Self::Pixel>().try_to::<T>().ok().unwrap();
debug_assert!(n % scale == 0.into());
n / scale
}

Expand Down

0 comments on commit d7a51ce

Please sign in to comment.