Skip to content

Commit

Permalink
trait Pixels: Add fn wrapping_*as_{mut_,}ptr methods for non-boun…
Browse files Browse the repository at this point in the history
…ds checked versions (#1337)

* Fixes #1336.

That specific test should pass now.
  • Loading branch information
kkysen authored Aug 28, 2024
2 parents 110ebd4 + e541a11 commit 4843f6f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/cdef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl cdef::Fn {
let stride = dst.stride();
let left = ptr::from_ref(left).cast();
let top_ptr = top.as_ptr::<BD>().cast();
let bottom_ptr = bottom.as_ptr::<BD>().cast();
let bottom_ptr = bottom.wrapping_as_ptr::<BD>().cast();
let top = FFISafe::new(&top);
let bottom = FFISafe::new(&bottom);
let sec_strength = sec_strength as c_int;
Expand Down
22 changes: 18 additions & 4 deletions src/pixels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub trait Pixels {
self.as_mut_ptr::<BD>().cast_const()
}

/// Absolute ptr to [`BitDepth::Pixel`]s starting at `offset`.
/// Absolute ptr to [`BitDepth::Pixel`]s starting at `pixel_offset`.
///
/// Bounds checked, but not [`DisjointMut`]-checked.
#[cfg_attr(debug_assertions, track_caller)]
Expand All @@ -49,12 +49,26 @@ pub trait Pixels {
unsafe { self.as_mut_ptr::<BD>().add(pixel_offset) }
}

/// Absolute ptr to [`BitDepth::Pixel`]s starting at `offset`.
/// Absolute ptr to [`BitDepth::Pixel`]s starting at `pixel_offset`.
///
/// Bounds checked, but not [`DisjointMut`]-checked.
#[cfg_attr(debug_assertions, track_caller)]
fn as_ptr_at<BD: BitDepth>(&self, offset: usize) -> *const BD::Pixel {
self.as_mut_ptr_at::<BD>(offset).cast_const()
fn as_ptr_at<BD: BitDepth>(&self, pixel_offset: usize) -> *const BD::Pixel {
self.as_mut_ptr_at::<BD>(pixel_offset).cast_const()
}

/// Absolute ptr to [`BitDepth::Pixel`]s starting at `pixel_offset`.
///
/// There is no bounds checking and this ptr may wrap and go out of bounds.
fn wrapping_as_mut_ptr_at<BD: BitDepth>(&self, pixel_offset: usize) -> *mut BD::Pixel {
self.as_mut_ptr::<BD>().wrapping_add(pixel_offset)
}

/// Absolute ptr to [`BitDepth::Pixel`]s starting at `pixel_offset`.
///
/// There is no bounds checking and this ptr may wrap and go out of bounds.
fn wrapping_as_ptr_at<BD: BitDepth>(&self, pixel_offset: usize) -> *const BD::Pixel {
self.as_ptr::<BD>().wrapping_add(pixel_offset)
}

/// Determine if they reference the same data.
Expand Down
8 changes: 8 additions & 0 deletions src/with_offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ impl<P: Pixels> WithOffset<P> {
pub fn as_mut_ptr<BD: BitDepth>(&self) -> *mut BD::Pixel {
self.data.as_mut_ptr_at::<BD>(self.offset)
}

pub fn wrapping_as_ptr<BD: BitDepth>(&self) -> *const BD::Pixel {
self.data.wrapping_as_ptr_at::<BD>(self.offset)
}

pub fn wrapping_as_mut_ptr<BD: BitDepth>(&self) -> *const BD::Pixel {
self.data.wrapping_as_mut_ptr_at::<BD>(self.offset)
}
}

impl<S: Strided> Strided for WithOffset<S> {
Expand Down

0 comments on commit 4843f6f

Please sign in to comment.