This is an automated email from the ASF dual-hosted git repository.

tustvold pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/master by this push:
     new e4e6c67ee Add OffsetBuffer::new (#3910)
e4e6c67ee is described below

commit e4e6c67eedc69e4f402c66dacb5176880d5101bf
Author: Raphael Taylor-Davies <[email protected]>
AuthorDate: Thu Mar 23 21:04:48 2023 +0000

    Add OffsetBuffer::new (#3910)
---
 arrow-buffer/src/buffer/offset.rs | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/arrow-buffer/src/buffer/offset.rs 
b/arrow-buffer/src/buffer/offset.rs
index 808e43cbf..ada290f09 100644
--- a/arrow-buffer/src/buffer/offset.rs
+++ b/arrow-buffer/src/buffer/offset.rs
@@ -24,6 +24,22 @@ use std::ops::Deref;
 pub struct OffsetBuffer<O: ArrowNativeType>(ScalarBuffer<O>);
 
 impl<O: ArrowNativeType> OffsetBuffer<O> {
+    /// Create a new [`OffsetBuffer`] from the provided [`ScalarBuffer`]
+    ///
+    /// # Panics
+    ///
+    /// Panics if `buffer` is not a non-empty buffer containing
+    /// monotonically increasing values greater than zero
+    pub fn new(buffer: ScalarBuffer<O>) -> Self {
+        assert!(!buffer.is_empty(), "offsets cannot be empty");
+        assert!(buffer[0] > O::usize_as(0), "offsets must be greater than 0");
+        assert!(
+            buffer.windows(2).all(|w| w[0] <= w[1]),
+            "offsets must be monotonically increasing"
+        );
+        Self(buffer)
+    }
+
     /// Create a new [`OffsetBuffer`] from the provided [`ScalarBuffer`]
     ///
     /// # Safety
@@ -71,3 +87,26 @@ impl<T: ArrowNativeType> AsRef<[T]> for OffsetBuffer<T> {
         self
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    #[should_panic(expected = "offsets cannot be empty")]
+    fn empty_offsets() {
+        OffsetBuffer::new(Vec::<i32>::new().into());
+    }
+
+    #[test]
+    #[should_panic(expected = "offsets must be greater than 0")]
+    fn negative_offsets() {
+        OffsetBuffer::new(vec![-1, 0, 1].into());
+    }
+
+    #[test]
+    #[should_panic(expected = "offsets must be monotonically increasing")]
+    fn non_monotonic_offsets() {
+        OffsetBuffer::new(vec![1, 2, 0].into());
+    }
+}

Reply via email to