Repository: pola-rs/polars
File: crates/polars-buffer/src/buffer.rs (Rust)

Code before:
    /// # Safety
    /// The caller must ensure the range is in-bounds.
    #[inline]
    pub unsafe fn slice_in_place_unchecked<R: RangeBounds<usize>>(&mut self, range: R) {
        unsafe {
            let Range { start, end } = crate::decode_range_unchecked(range, ..self.len());
            self.ptr = self.ptr.add(start);
            self.length = end - start;
        }
    }

    /// Divides one buffer into two at an index.
    ///
    /// The first will contain all indices from `[0, mid)` (excluding
    /// the index `mid` itself) and the second will contain all
    /// indices from `[mid, len)` (excluding the index `len` itself).
    ///
    /// # Panics
    /// Panics if `mid > len`.
    #[must_use]
    pub fn split_at(self, mid: usize) -> (Self, Self) {
        (self.clone().sliced(..mid), self.sliced(mid..))
    }

    /// Splits the buffer into two at the given index.
    /// Afterwards self contains elements [at, len), and the returned Buffer contains elements [0, at).
    /// This is an O(1) operation that just increases the reference count and sets a few indices.
    #[must_use]
    pub fn split_off(&mut self, at: usize) -> Self {
        let out = self.clone().sliced(..at);
        self.slice_in_place(at..);
        out
    }

    /// Returns a pointer to the start of the storage underlying this buffer.
    #[inline]
    pub fn storage_ptr(&self) -> *const T {
        self.storage.as_ptr()
    }

    /// Returns the start offset of this buffer within the underlying storage.
    #[inline]
    pub fn offset(&self) -> usize {
        unsafe {
            let ret = self.ptr.offset_from(self.storage.as_ptr()) as usize;
            debug_assert!(ret <= self.storage.len());
            ret
        }
    }

    /// # Safety

Review comment (suggestion):
This should be the other way around for consistency with `Vec::split_off`.

Code after:
    /// # Safety
    /// The caller must ensure the range is in-bounds.
    #[inline]
    pub unsafe fn slice_in_place_unchecked<R: RangeBounds<usize>>(&mut self, range: R) {
        unsafe {
            let Range { start, end } = crate::decode_range_unchecked(range, ..self.len());
            self.ptr = self.ptr.add(start);
            self.length = end - start;
        }
    }

    /// Divides one buffer into two at an index.
    ///
    /// The first will contain all indices from `[0, mid)` (excluding
    /// the index `mid` itself) and the second will contain all
    /// indices from `[mid, len)` (excluding the index `len` itself).
    ///
    /// # Panics
    /// Panics if `mid > len`.
    #[must_use]
    pub fn split_at(self, mid: usize) -> (Self, Self) {
        (self.clone().sliced(..mid), self.sliced(mid..))
    }

    /// Splits the buffer into two at the given index.
    ///
    /// Returns a buffer containing the elements in the range
    /// `[at, len)`. After the call, self will be left containing
    /// the elements `[0, at)`.
    ///
    /// # Panics
    /// Panics if `at > len`.
    #[must_use]
    pub fn split_off(&mut self, at: usize) -> Self {
        let out = self.clone().sliced(at..);
        self.slice_in_place(..at);
        out
    }

    /// Returns a pointer to the start of the storage underlying this buffer.
    #[inline]
    pub fn storage_ptr(&self) -> *const T {
        self.storage.as_ptr()
    }

    /// Returns the start offset of this buffer within the underlying storage.
    #[inline]
    pub fn offset(&self) -> usize {
        unsafe {
            let ret = self.ptr.offset_from(self.storage.as_ptr()) as usize;
            debug_assert!(ret <= self.storage.len());