Repository: bevyengine/bevy
File: crates/bevy_ptr/src/lib.rs (Rust)

Code before:
/// # use core::mem::size_of;
/// # use bevy_ptr::ThinSlicePtr;
/// #
/// let slice: &[u32] = &[2, 4, 8];
/// let thin_slice = ThinSlicePtr::from(slice);
///
/// assert_eq!(*unsafe { thin_slice.get_unchecked(0) }, 2);
/// assert_eq!(*unsafe { thin_slice.get_unchecked(1) }, 4);
/// assert_eq!(*unsafe { thin_slice.get_unchecked(2) }, 8);
/// ```
pub struct ThinSlicePtr<'a, T> {
    ptr: NonNull<T>,
    #[cfg(debug_assertions)]
    len: usize,
    _marker: PhantomData<&'a [T]>,
}

impl<'a, T> ThinSlicePtr<'a, T> {
    /// Indexes the slice without performing bounds checks.
    ///
    /// # Safety
    ///
    /// `index` must be in-bounds.
    #[inline]
    pub unsafe fn get_unchecked(&self, index: usize) -> &'a T {
        debug_assert!(index < self.len, "tried to index out-of-bounds of a slice");

        // SAFETY: The caller guarantees `index` is in-bounds so that the resulting pointer is
        // valid to dereference.
        unsafe { &*self.ptr.add(index).as_ptr() }
    }

    /// Indexes the slice without performing bounds checks.
    ///
    /// # Safety
    ///
    /// `index` must be in-bounds.
    #[deprecated(since = "0.18.0", note = "use get_unchecked() instead")]
    pub unsafe fn get(self, index: usize) -> &'a T {
        // SAFETY: The caller guarantees that `index` is in-bounds.
        unsafe { self.get_unchecked(index) }
    }
}

impl<'a, T> Clone for ThinSlicePtr<'a, T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<'a, T> Copy for ThinSlicePtr<'a, T> {}

Review comment (bug):
non-blocking comment: I noticed you removed the `#[cfg(debug_assertions)]` line that was above this previously (in the deprecated function). With `debug_assert!`, as far as I can read from documentation, that line is actually not needed, right? It seems like there are other places in the codebase where this redundant line is placed above a `debug_assert!`

(new here and new to Rust!)

Code after:
/// # use core::mem::size_of;
/// # use bevy_ptr::ThinSlicePtr;
/// #
/// let slice: &[u32] = &[2, 4, 8];
/// let thin_slice = ThinSlicePtr::from(slice);
///
/// assert_eq!(*unsafe { thin_slice.get_unchecked(0) }, 2);
/// assert_eq!(*unsafe { thin_slice.get_unchecked(1) }, 4);
/// assert_eq!(*unsafe { thin_slice.get_unchecked(2) }, 8);
/// ```
pub struct ThinSlicePtr<'a, T> {
    ptr: NonNull<T>,
    #[cfg(debug_assertions)]
    len: usize,
    _marker: PhantomData<&'a [T]>,
}

impl<'a, T> ThinSlicePtr<'a, T> {
    /// Indexes the slice without performing bounds checks.
    ///
    /// # Safety
    ///
    /// `index` must be in-bounds.
    #[inline]
    pub unsafe fn get_unchecked(&self, index: usize) -> &'a T {
        // We cannot use `debug_assert!` here because `self.len` does not exist when not in debug
        // mode.
        #[cfg(debug_assertions)]
        assert!(index < self.len, "tried to index out-of-bounds of a slice");

        // SAFETY: The caller guarantees `index` is in-bounds so that the resulting pointer is
        // valid to dereference.
        unsafe { &*self.ptr.add(index).as_ptr() }
    }

    /// Indexes the slice without performing bounds checks.
    ///
    /// # Safety
    ///
    /// `index` must be in-bounds.
    #[deprecated(since = "0.18.0", note = "use get_unchecked() instead")]
    pub unsafe fn get(self, index: usize) -> &'a T {
        // SAFETY: The caller guarantees that `index` is in-bounds.
        unsafe { self.get_unchecked(index) }
    }
}

impl<'a, T> Clone for ThinSlicePtr<'a, T> {
    fn clone(&self) -> Self {
        *self
    }