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.
  • Loading branch information
kkysen committed Aug 28, 2024
1 parent 110ebd4 commit c3d2919
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
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 c3d2919

Please sign in to comment.