From c3d2919877eb4f0e55f95b51c698dfdf0cfb2946 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Wed, 28 Aug 2024 14:02:16 -0700 Subject: [PATCH] `trait Pixels`: Add `fn wrapping_*as_{mut_,}ptr` methods for non-bounds checked versions. --- src/pixels.rs | 22 ++++++++++++++++++---- src/with_offset.rs | 8 ++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/pixels.rs b/src/pixels.rs index 5c17f6b65..a52029489 100644 --- a/src/pixels.rs +++ b/src/pixels.rs @@ -28,7 +28,7 @@ pub trait Pixels { self.as_mut_ptr::().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)] @@ -49,12 +49,26 @@ pub trait Pixels { unsafe { self.as_mut_ptr::().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(&self, offset: usize) -> *const BD::Pixel { - self.as_mut_ptr_at::(offset).cast_const() + fn as_ptr_at(&self, pixel_offset: usize) -> *const BD::Pixel { + self.as_mut_ptr_at::(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(&self, pixel_offset: usize) -> *mut BD::Pixel { + self.as_mut_ptr::().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(&self, pixel_offset: usize) -> *const BD::Pixel { + self.as_ptr::().wrapping_add(pixel_offset) } /// Determine if they reference the same data. diff --git a/src/with_offset.rs b/src/with_offset.rs index 1aa76f388..b057d9ad5 100644 --- a/src/with_offset.rs +++ b/src/with_offset.rs @@ -92,6 +92,14 @@ impl WithOffset

{ pub fn as_mut_ptr(&self) -> *mut BD::Pixel { self.data.as_mut_ptr_at::(self.offset) } + + pub fn wrapping_as_ptr(&self) -> *const BD::Pixel { + self.data.wrapping_as_ptr_at::(self.offset) + } + + pub fn wrapping_as_mut_ptr(&self) -> *const BD::Pixel { + self.data.wrapping_as_mut_ptr_at::(self.offset) + } } impl Strided for WithOffset {