Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trait Pixels: Add fn wrapping_*as_{mut_,}ptr methods for non-bounds checked versions #1337

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading