Skip to content

Commit

Permalink
trait BitDepth: Document fn from_c, which uniformly converts from…
Browse files Browse the repository at this point in the history
… C's repr.
  • Loading branch information
kkysen committed Jul 24, 2023
1 parent 3a617f8 commit 509293c
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions include/common/bitdepth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,15 @@ pub trait BitDepth: Clone + Copy {

fn new(bitdepth_max: Self::BitDepthMax) -> Self;

fn from_c(bitdepth_max: libc::c_int) -> Self;
/// While [`Self::new`] is the implementation specific way to construct a [`BitDepth`],
/// [`Self::from_c`] is a uniform way to construct a [`BitDepth`]
/// from its C representation, which is a [`c_int`].
///
/// Since we're deduplicating [`BitDepth`]-dependent `fn` ptr types through type erasure,
/// and always passing the `bitdepth_max` last argument
/// even for `8bpc` `fn`s where it's superfluous (it's constant),
/// we need to convert from that `bitdepth_max: c_int` arg back to a [`BitDepth`].
fn from_c(bitdepth_max: c_int) -> Self;

fn pixel_copy(dest: &mut [Self::Pixel], src: &[Self::Pixel], n: usize) {
dest[..n].copy_from_slice(&src[..n]);
Expand Down Expand Up @@ -147,7 +155,7 @@ impl BitDepth for BitDepth8 {
Self { bitdepth_max }
}

fn from_c(_bitdepth_max: libc::c_int) -> Self {
fn from_c(_bitdepth_max: c_int) -> Self {
Self::new(())
}

Expand Down Expand Up @@ -195,8 +203,8 @@ impl BitDepth for BitDepth16 {
Self { bitdepth_max }
}

fn from_c(bitdepth_max: libc::c_int) -> Self {
Self::new(bitdepth_max as Self::BitDepthMax)
fn from_c(bitdepth_max: c_int) -> Self {
Self::new(bitdepth_max.as_())
}

fn display(pixel: Self::Pixel) -> Self::DisplayPixel {
Expand Down

0 comments on commit 509293c

Please sign in to comment.