Repository: jorgecarleitao/arrow2
Pull request #1020 (2022-05-28 06:14:16)

Remove `from_trusted_len_*` from `Buffer`
This PR proposes to remove some functions from `Buffer` since there are more idiomatically achieved with `Vec`'s API.

Specifically, the following are equivalents
* `Buffer::new_zeroed(length) <-> vec![0; length].into()`
* `Buffer::from_slice(slice) <-> slice.to_vec().into()`
* `Buffer::from_trusted_len_iter(iter) <-> iter.collect::<Vec<_>>().into()`
* `Buffer::try_from_trusted_len_iter(iter) <-> iter.collect::<Result<Vec<_>>>()?.into()`

This allows the API and documentation of `Buffer` to focus on its main semantics: 
* `O(1)` slicing
* copy on write


codecov[bot]: # [Codecov](https://codecov.io/gh/jorgecarleitao/arrow2/pull/1020?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao) Report
> Merging [#1020](https://codecov.io/gh/jorgecarleitao/arrow2/pull/1020?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao) (1527914) into [main](https://codecov.io/gh/jorgecarleitao/arrow2/commit/e976c1e8874082838460314dc304369cd4b29ee9?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao) (e976c1e) will **decrease** coverage by `0.00%`.
> The diff coverage is `85.71%`.

```diff
@@            Coverage Diff             @@
##             main    #1020      +/-   ##
==========================================
- Coverage   71.63%   71.63%   -0.01%     
==========================================
  Files         359      359              
  Lines       19904    19893      -11     
==========================================
- Hits        14259    14251       -8     
+ Misses       5645     5642       -3     
```


| [Impacted Files](https://codecov.io/gh/jorgecarleitao/arrow2/pull/1020?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao) | Coverage Δ | |
|---|---|---|
| [src/array/map/mod.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/1020/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-c3JjL2FycmF5L21hcC9tb2QucnM=) | `64.19% <0.00%> (ø)` | |
| [src/array/mod.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/1020/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-c3JjL2FycmF5L21vZC5ycw==) | `67.02% <ø> (ø)` | |
| [src/buffer/immutable.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/1020/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-c3JjL2J1ZmZlci9pbW11dGFibGUucnM=) | `97.77% <ø> (+4.92%)` | :arrow_up: |
| [src/array/binary/mod.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/1020/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-c3JjL2FycmF5L2JpbmFyeS9tb2QucnM=) | `84.46% <100.00%> (ø)` | |
| [src/array/fixed\_size\_binary/mod.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/1020/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-c3JjL2FycmF5L2ZpeGVkX3NpemVfYmluYXJ5L21vZC5ycw==) | `88.34% <100.00%> (ø)` | |
| [src/array/list/mod.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/1020/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-c3JjL2FycmF5L2xpc3QvbW9kLnJz) | `69.91% <100.00%> (ø)` | |
| [src/array/primitive/mod.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/1020/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-c3JjL2FycmF5L3ByaW1pdGl2ZS9tb2QucnM=) | `78.63% <100.00%> (ø)` | |
| [src/array/union/mod.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/1020/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-c3JjL2FycmF5L3VuaW9uL21vZC5ycw==) | `79.64% <100.00%> (ø)` | |
| [src/array/utf8/mod.rs](https://codecov.io/gh/jorgecarleitao/arrow2/pull/1020/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao#diff-c3JjL2FycmF5L3V0ZjgvbW9kLnJz) | `73.40% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/jorgecarleitao/arrow2/pull/1020?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/jorgecarleitao/arrow2/pull/1020?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao). Last update [e976c1e...1527914](https://codecov.io/gh/jorgecarleitao/arrow2/pull/1020?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jorge+Leitao).


Patch:
diff --git a/guide/src/high_level.md b/guide/src/high_level.md
index 80652038b0..8c5b93c55f 100644
--- a/guide/src/high_level.md
+++ b/guide/src/high_level.md
@@ -180,12 +180,7 @@ What this means is that certain operations can be performed irrespectively of wh
 is "null" or not (e.g. `PrimitiveArray<i32> + i32` can be applied to _all_ values
 via SIMD and only copy the validity bitmap independently).
 
-When an operation benefits from such arrangement, it is advantageous to use
-
-* `Buffer::from_iter`
-* `Buffer::from_trusted_len_iter`
-* `Buffer::try_from_trusted_len_iter`
-
+When an operation benefits from such arrangement, it is advantageous to use `Vec` and `Into<Buffer>`
 If not, then use the `MutableArray` API, such as
 `MutablePrimitiveArray<T>`, `MutableUtf8Array<O>` or `MutableListArray`.
 
@@ -254,12 +249,11 @@ where
     O: NativeType,
     F: Fn(I) -> O,
 {
-    // create the iterator over _all_ values
-    let values = array.values().iter().map(|v| op(*v));
-    let values = Buffer::from_trusted_len_iter(values);
+    // apply F over _all_ values
+    let values = array.values().iter().map(|v| op(*v)).collect::<Vec<_>>();
 
     // create the new array, cloning its validity
-    PrimitiveArray::<O>::from_data(data_type.clone(), values, array.validity().cloned())
+    PrimitiveArray::<O>::from_data(data_type.clone(), values.into(), array.validity().cloned())
 }
 ```
 
diff --git a/src/array/binary/mod.rs b/src/array/binary/mod.rs
index ba7f635de9..1805e5e33f 100644
--- a/src/array/binary/mod.rs
+++ b/src/array/binary/mod.rs
@@ -116,7 +116,7 @@ impl<O: Offset> BinaryArray<O> {
     pub fn new_null(data_type: DataType, length: usize) -> Self {
         Self::new(
             data_type,
-            Buffer::new_zeroed(length + 1),
+            vec![O::default(); 1 + length].into(),
             Buffer::new(),
             Some(Bitmap::new_zeroed(length)),
         )
diff --git a/src/array/fixed_size_binary/mod.rs b/src/array/fixed_size_binary/mod.rs
index 2a843842dc..4209998ec1 100644
--- a/src/array/fixed_size_binary/mod.rs
+++ b/src/array/fixed_size_binary/mod.rs
@@ -89,7 +89,7 @@ impl FixedSizeBinaryArray {
         let size = Self::maybe_get_size(&data_type).unwrap();
         Self::new(
             data_type,
-            Buffer::new_zeroed(length * size),
+            vec![0u8; length * size].into(),
             Some(Bitmap::new_zeroed(length)),
         )
     }
diff --git a/src/array/list/mod.rs b/src/array/list/mod.rs
index 1a3de45a34..c5ab495559 100644
--- a/src/array/list/mod.rs
+++ b/src/array/list/mod.rs
@@ -116,7 +116,7 @@ impl<O: Offset> ListArray<O> {
         let child = Self::get_child_type(&data_type).clone();
         Self::new(
             data_type,
-            Buffer::new_zeroed(length + 1),
+            vec![O::default(); 1 + length].into(),
             new_empty_array(child).into(),
             Some(Bitmap::new_zeroed(length)),
         )
diff --git a/src/array/map/mod.rs b/src/array/map/mod.rs
index b69ac7e2ac..2c667b2958 100644
--- a/src/array/map/mod.rs
+++ b/src/array/map/mod.rs
@@ -106,7 +106,7 @@ impl MapArray {
         let field = new_empty_array(Self::get_field(&data_type).data_type().clone()).into();
         Self::new(
             data_type,
-            Buffer::new_zeroed(length + 1),
+            vec![0i32; 1 + length].into(),
             field,
             Some(Bitmap::new_zeroed(length)),
         )
diff --git a/src/array/mod.rs b/src/array/mod.rs
index 5a0b84b224..0be802df2f 100644
--- a/src/array/mod.rs
+++ b/src/array/mod.rs
@@ -52,7 +52,7 @@ pub trait Array: Send + Sync {
 
     /// The number of null slots on this [`Array`].
     /// # Implementation
-    /// This is `O(1)`.
+    /// This is `O(1)` since the number of null elements is pre-computed.
     #[inline]
     fn null_count(&self) -> usize {
         if self.data_type() == &DataType::Null {
diff --git a/src/array/primitive/mod.rs b/src/array/primitive/mod.rs
index ae753549f7..7ba44345ce 100644
--- a/src/array/primitive/mod.rs
+++ b/src/array/primitive/mod.rs
@@ -305,7 +305,7 @@ impl<T: NativeType> PrimitiveArray<T> {
     pub fn new_null(data_type: DataType, length: usize) -> Self {
         Self::new(
             data_type,
-            Buffer::new_zeroed(length),
+            vec![T::default(); length].into(),
             Some(Bitmap::new_zeroed(length)),
         )
     }
diff --git a/src/array/union/mod.rs b/src/array/union/mod.rs
index 925a156c11..dfd87ef6af 100644
--- a/src/array/union/mod.rs
+++ b/src/array/union/mod.rs
@@ -142,7 +142,7 @@ impl UnionArray {
             };
 
             // all from the same field
-            let types = Buffer::new_zeroed(length);
+            let types = vec![0i8; length].into();
 
             Self::new(data_type, types, fields, offsets)
         } else {
diff --git a/src/array/utf8/mod.rs b/src/array/utf8/mod.rs
index c216ae0ab5..8c8813c14b 100644
--- a/src/array/utf8/mod.rs
+++ b/src/array/utf8/mod.rs
@@ -344,7 +344,7 @@ impl<O: Offset> Utf8Array<O> {
     pub fn new_null(data_type: DataType, length: usize) -> Self {
         Self::new(
             data_type,
-            Buffer::new_zeroed(length + 1),
+            vec![O::default(); 1 + length].into(),
             Buffer::new(),
             Some(Bitmap::new_zeroed(length)),
         )
diff --git a/src/bitmap/immutable.rs b/src/bitmap/immutable.rs
index c51a7335e5..bee9e62c63 100644
--- a/src/bitmap/immutable.rs
+++ b/src/bitmap/immutable.rs
@@ -2,26 +2,51 @@ use either::Either;
 use std::iter::FromIterator;
 use std::sync::Arc;
 
-use crate::{buffer::bytes::Bytes, trusted_len::TrustedLen};
+use crate::{buffer::bytes::Bytes, error::Error, trusted_len::TrustedLen};
 
 use super::{
     utils::{count_zeros, fmt, get_bit, get_bit_unchecked, BitChunk, BitChunks, BitmapIter},
     MutableBitmap,
 };
 
-/// An immutable container whose API is optimized to handle bitmaps. All quantities on this
-/// container's API are measured in bits.
-/// # Implementation
-/// * memory on this container is sharable across thread boundaries
-/// * Cloning [`Bitmap`] is `O(1)`
-/// * Slicing [`Bitmap`] is `O(1)`
+/// An immutable container semantically equivalent to `Arc<Vec<bool>>` but represented as `Arc<Vec<u8>>` where
+/// each boolean is represented as a single bit.
+///
+/// # Examples
+/// ```
+/// use arrow2::bitmap::{Bitmap, MutableBitmap};
+///
+/// let bitmap = Bitmap::from([true, false, true]);
+/// assert_eq!(bitmap.iter().collect::<Vec<_>>(), vec![true, false, true]);
+///
+/// // creation directly from bytes
+/// let bitmap = Bitmap::try_new(vec![0b00001101], 5).unwrap();
+/// // note: the first bit is the left-most of the first byte
+/// assert_eq!(bitmap.iter().collect::<Vec<_>>(), vec![true, false, true, true, false]);
+/// // we can also get the slice:
+/// assert_eq!(bitmap.as_slice(), ([0b00001101u8].as_ref(), 0, 5));
+/// // debug helps :)
+/// assert_eq!(format!("{:?}", bitmap), "[0b___01101]".to_string());
+///
+/// // it supports copy-on-write semantics (to a `MutableBitmap`)
+/// let bitmap: MutableBitmap = bitmap.into_mut().right().unwrap();
+/// assert_eq!(bitmap, MutableBitmap::from([true, false, true, true, false]));
+///
+/// // slicing is 'O(1)' (data is shared)
+/// let bitmap = Bitmap::try_new(vec![0b00001101], 5).unwrap();
+/// let sliced = bitmap.slice(1, 4);
+/// assert_eq!(sliced.as_slice(), ([0b00001101u8].as_ref(), 1, 4)); // 1 here is the offset:
+/// assert_eq!(format!("{:?}", sliced), "[0b___0110_]".to_string());
+/// // when sliced (or cloned), it is no longer possible to `into_mut`.
+/// let same: Bitmap = sliced.into_mut().left().unwrap();
+/// ```
 #[derive(Clone)]
 pub struct Bitmap {
     bytes: Arc<Bytes<u8>>,
     // both are measured in bits. They are used to bound the bitmap to a region of Bytes.
     offset: usize,
     length: usize,
-    // this is a cache: it must be computed on initialization
+    // this is a cache: it is computed on initialization
     null_count: usize,
 }
 
@@ -45,10 +70,25 @@ impl Bitmap {
         Self::default()
     }
 
-    /// Initializes an new [`Bitmap`] filled with unset values.
+    /// Initializes a new [`Bitmap`] from vector of bytes and a length.
+    /// # Errors
+    /// This function errors iff `length > bytes.len() * 8`
     #[inline]
-    pub fn new_zeroed(length: usize) -> Self {
-        MutableBitmap::from_len_zeroed(length).into()
+    pub fn try_new(bytes: Vec<u8>, length: usize) -> Result<Self, Error> {
+        if length > bytes.len().saturating_mul(8) {
+            return Err(Error::InvalidArgumentError(format!(
+                "The length of the bitmap ({}) must be `<=` to the number of bytes times 8 ({})",
+                length,
+                bytes.len().saturating_mul(8)
+            )));
+        }
+        let null_count = count_zeros(&bytes, 0, length);
+        Ok(Self {
+            length,
+            offset: 0,
+            bytes: Arc::new(bytes.into()),
+            null_count,
+        })
     }
 
     /// Returns the length of the [`Bitmap`].
@@ -63,6 +103,18 @@ impl Bitmap {
         self.len() == 0
     }
 
+    /// Returns a new iterator of `bool` over this bitmap
+    pub fn iter(&self) -> BitmapIter {
+        BitmapIter::new(&self.bytes, self.offset, self.length)
+    }
+
+    /// Returns an iterator over bits in bit chunks [`BitChunk`].
+    ///
+    /// This iterator is useful to operate over multiple bits via e.g. bitwise.
+    pub fn chunks<T: BitChunk>(&self) -> BitChunks<T> {
+        BitChunks::new(&self.bytes, self.offset, self.length)
+    }
+
     /// Creates a new [`Bitmap`] from [`Bytes`] and a length.
     /// # Panic
     /// Panics iff `length <= bytes.len() * 8`
@@ -78,28 +130,22 @@ impl Bitmap {
         }
     }
 
-    /// Creates a new [`Bitmap`] from [`Vec`] and a length.
-    /// This function is `O(1)`
-    /// # Panic
-    /// Panics iff `length <= buffer.len() * 8`
-    #[inline]
-    pub fn from_u8_vec(vec: Vec<u8>, length: usize) -> Self {
-        Bitmap::from_bytes(vec.into(), length)
-    }
-
-    /// Creates a new [`Bitmap`] from a slice and length.
-    /// # Panic
-    /// Panics iff `length <= bytes.len() * 8`
-    #[inline]
-    pub fn from_u8_slice<T: AsRef<[u8]>>(buffer: T, length: usize) -> Self {
-        let buffer = Vec::<u8>::from(buffer.as_ref());
-        Bitmap::from_u8_vec(buffer, length)
-    }
-
-    /// Counts the nulls (unset bits) starting from `offset` bits and for `length` bits.
+    /// Returns the byte slice of this [`Bitmap`].
+    ///
+    /// The returned tuple contains:
+    /// * `.1`: The byte slice, truncated to the start of the first bit. So the start of the slice
+    ///       is within the first 8 bits.
+    /// * `.2`: The start offset in bits on a range `0 <= offsets < 8`.
+    /// * `.3`: The length in number of bits.
     #[inline]
-    pub fn null_count_range(&self, offset: usize, length: usize) -> usize {
-        count_zeros(&self.bytes, self.offset + offset, length)
+    pub fn as_slice(&self) -> (&[u8], usize, usize) {
+        let start = self.offset / 8;
+        let len = (self.offset % 8 + self.length).saturating_add(7) / 8;
+        (
+            &self.bytes[start..start + len],
+            self.offset % 8,
+            self.length,
+        )
     }
 
     /// Returns the number of unset bits on this [`Bitmap`].
@@ -110,9 +156,10 @@ impl Bitmap {
 
     /// Slices `self`, offsetting by `offset` and truncating up to `length` bits.
     /// # Panic
-    /// Panics iff `self.offset + offset + length >= self.bytes.len() * 8`, i.e. if the offset and `length`
+    /// Panics iff `offset + length > self.length`, i.e. if the offset and `length`
     /// exceeds the allocated capacity of `self`.
     #[inline]
+    #[must_use]
     pub fn slice(self, offset: usize, length: usize) -> Self {
         assert!(offset + length <= self.length);
         unsafe { self.slice_unchecked(offset, length) }
@@ -147,16 +194,6 @@ impl Bitmap {
         get_bit(&self.bytes, self.offset + i)
     }
 
-    /// Returns whether the bit at position `i` is set.
-    #[inline]
-    pub fn get(&self, i: usize) -> Option<bool> {
-        if i < self.len() {
-            Some(unsafe { self.get_bit_unchecked(i) })
-        } else {
-            None
-        }
-    }
-
     /// Unsafely returns whether the bit at position `i` is set.
     /// # Safety
     /// Unsound iff `i >= self.len()`.
@@ -196,6 +233,45 @@ impl Bitmap {
             _ => Either::Left(self),
         }
     }
+
+    /// Initializes an new [`Bitmap`] filled with unset values.
+    #[inline]
+    pub fn new_zeroed(length: usize) -> Self {
+        MutableBitmap::from_len_zeroed(length).into()
+    }
+
+    /// Counts the nulls (unset bits) starting from `offset` bits and for `length` bits.
+    #[inline]
+    pub fn null_count_range(&self, offset: usize, length: usize) -> usize {
+        count_zeros(&self.bytes, self.offset + offset, length)
+    }
+
+    /// Creates a new [`Bitmap`] from a slice and length.
+    /// # Panic
+    /// Panics iff `length <= bytes.len() * 8`
+    #[inline]
+    pub fn from_u8_slice<T: AsRef<[u8]>>(slice: T, length: usize) -> Self {
+        Bitmap::try_new(slice.as_ref().to_vec(), length).unwrap()
+    }
+
+    /// Alias for `Bitmap::try_new().unwrap()`
+    /// This function is `O(1)`
+    /// # Panic
+    /// This function panics iff `length <= bytes.len() * 8`
+    #[inline]
+    pub fn from_u8_vec(vec: Vec<u8>, length: usize) -> Self {
+        Bitmap::try_new(vec, length).unwrap()
+    }
+
+    /// Returns whether the bit at position `i` is set.
+    #[inline]
+    pub fn get(&self, i: usize) -> Option<bool> {
+        if i < self.len() {
+            Some(unsafe { self.get_bit_unchecked(i) })
+        } else {
+            None
+        }
+    }
 }
 
 impl<P: AsRef<[bool]>> From<P> for Bitmap {
@@ -213,14 +289,6 @@ impl FromIterator<bool> for Bitmap {
     }
 }
 
-impl Bitmap {
-    /// Returns an iterator over bits in chunks of `T`, which is useful for
-    /// bit operations.
-    pub fn chunks<T: BitChunk>(&self) -> BitChunks<T> {
-        BitChunks::new(&self.bytes, self.offset, self.length)
-    }
-}
-
 impl Bitmap {
     /// Creates a new [`Bitmap`] from an iterator of booleans.
     /// # Safety
@@ -258,26 +326,6 @@ impl Bitmap {
     }
 }
 
-impl Bitmap {
-    /// Returns the byte slice of this Bitmap.
-    ///
-    /// The returned tuple contains:
-    /// .1 -> The byte slice, truncated to the start of the first bit. So the start of the slice
-    ///       is within the first 8 bits.
-    /// .2 -> The start offset in bits, given what described above `0 <= offsets < 8`.
-    /// .3 -> The length in bits.
-    #[inline]
-    pub fn as_slice(&self) -> (&[u8], usize, usize) {
-        let start = self.offset / 8;
-        let len = (self.offset % 8 + self.length).saturating_add(7) / 8;
-        (
-            &self.bytes[start..start + len],
-            self.offset % 8,
-            self.length,
-        )
-    }
-}
-
 impl<'a> IntoIterator for &'a Bitmap {
     type Item = bool;
     type IntoIter = BitmapIter<'a>;
@@ -286,10 +334,3 @@ impl<'a> IntoIterator for &'a Bitmap {
         BitmapIter::<'a>::new(&self.bytes, self.offset, self.length)
     }
 }
-
-impl<'a> Bitmap {
-    /// constructs a new iterator
-    pub fn iter(&'a self) -> BitmapIter<'a> {
-        BitmapIter::<'a>::new(&self.bytes, self.offset, self.length)
-    }
-}
diff --git a/src/bitmap/mutable.rs b/src/bitmap/mutable.rs
index c8e5e04649..4cc76a897c 100644
--- a/src/bitmap/mutable.rs
+++ b/src/bitmap/mutable.rs
@@ -264,7 +264,7 @@ impl MutableBitmap {
 impl From<MutableBitmap> for Bitmap {
     #[inline]
     fn from(buffer: MutableBitmap) -> Self {
-        Bitmap::from_bytes(buffer.buffer.into(), buffer.length)
+        Bitmap::try_new(buffer.buffer, buffer.length).unwrap()
     }
 }
 
@@ -272,7 +272,7 @@ impl From<MutableBitmap> for Option<Bitmap> {
     #[inline]
     fn from(buffer: MutableBitmap) -> Self {
         if buffer.null_count() > 0 {
-            Some(Bitmap::from_bytes(buffer.buffer.into(), buffer.length))
+            Some(Bitmap::try_new(buffer.buffer, buffer.length).unwrap())
         } else {
             None
         }
diff --git a/src/bitmap/utils/mod.rs b/src/bitmap/utils/mod.rs
index aba94cca00..55ca42ba14 100644
--- a/src/bitmap/utils/mod.rs
+++ b/src/bitmap/utils/mod.rs
@@ -59,10 +59,12 @@ pub unsafe fn set_bit_unchecked(data: &mut [u8], i: usize, value: bool) {
     *byte = set(*byte, i % 8, value);
 }
 
-/// Returns whether bit at position `i` in `data` is set or not
+/// Returns whether bit at position `i` in `data` is set
+/// # Panic
+/// This function panics iff `i / 8 >= bytes.len()`
 #[inline]
-pub fn get_bit(data: &[u8], i: usize) -> bool {
-    is_set(data[i / 8], i % 8)
+pub fn get_bit(bytes: &[u8], i: usize) -> bool {
+    is_set(bytes[i / 8], i % 8)
 }
 
 /// Returns whether bit at position `i` in `data` is set or not.
diff --git a/src/buffer/immutable.rs b/src/buffer/immutable.rs
index 1f6774f0a8..758de74f29 100644
--- a/src/buffer/immutable.rs
+++ b/src/buffer/immutable.rs
@@ -1,18 +1,38 @@
 use either::Either;
 use std::{iter::FromIterator, sync::Arc, usize};
 
-use crate::{trusted_len::TrustedLen, types::NativeType};
+use crate::types::NativeType;
 
 use super::bytes::Bytes;
 
-/// [`Buffer`] is a contiguous memory region that can
-/// be shared across thread boundaries.
+/// [`Buffer`] is a contiguous memory region that can be shared across thread boundaries.
+///
 /// The easiest way to think about `Buffer<T>` is being equivalent to
-/// an immutable `Vec<T>`, with the following differences:
+/// a `Arc<Vec<T>>`, with the following differences:
 /// * `T` must be [`NativeType`]
-/// * clone is `O(1)`
-/// * memory is sharable across thread boundaries (it is under an `Arc`)
-/// * it supports external allocated memory (FFI)
+/// * slicing the buffer is `O(1)`
+/// * it supports external allocated memory (via FFI)
+///
+/// The easiest way to create one is to use its implementation of `From` a `Vec`.
+///
+/// # Examples
+/// ```
+/// use arrow2::buffer::Buffer;
+///
+/// let buffer: Buffer<u32> = vec![1, 2, 3].into();
+/// assert_eq!(buffer.as_ref(), [1, 2, 3].as_ref());
+///
+/// // it supports copy-on-write semantics (i.e. back to a `Vec`)
+/// let vec: Vec<u32> = buffer.into_mut().right().unwrap();
+/// assert_eq!(vec, vec![1, 2, 3]);
+///
+/// // cloning and slicing is `O(1)` (data is shared)
+/// let buffer: Buffer<u32> = vec![1, 2, 3].into();
+/// let slice = buffer.clone().slice(1, 1);
+/// assert_eq!(slice.as_ref(), [2].as_ref());
+/// // no longer possible to get a vec since `slice` and `buffer` share data
+/// let same: Buffer<u32> = buffer.into_mut().left().unwrap();
+/// ```
 #[derive(Clone, PartialEq)]
 pub struct Buffer<T: NativeType> {
     /// the internal byte buffer.
@@ -46,20 +66,6 @@ impl<T: NativeType> Buffer<T> {
         Self::default()
     }
 
-    /// Creates a new [`Buffer`] filled with zeros.
-    #[inline]
-    pub fn new_zeroed(length: usize) -> Self {
-        vec![T::default(); length].into()
-    }
-
-    /// Takes ownership of [`Vec`].
-    /// # Implementation
-    /// This function is `O(1)`
-    #[inline]
-    pub fn from_slice<R: AsRef<[T]>>(data: R) -> Self {
-        data.as_ref().to_vec().into()
-    }
-
     /// Auxiliary method to create a new Buffer
     pub(crate) fn from_bytes(bytes: Bytes<T>) -> Self {
         let length = bytes.len();
@@ -153,53 +159,6 @@ impl<T: NativeType> Buffer<T> {
     }
 }
 
-impl<T: NativeType> Buffer<T> {
-    /// Creates a [`Buffer`] from an [`Iterator`] with a trusted length.
-    /// Prefer this to `collect` whenever possible, as it often enables auto-vectorization.
-    /// # Example
-    /// ```
-    /// # use arrow2::buffer::Buffer;
-    /// let v = vec![1u32];
-    /// let iter = v.iter().map(|x| x * 2);
-    /// let buffer = unsafe { Buffer::from_trusted_len_iter(iter) };
-    /// assert_eq!(buffer.len(), 1)
-    /// ```
-    #[inline]
-    pub fn from_trusted_len_iter<I: TrustedLen<Item = T>>(iterator: I) -> Self {
-        iterator.collect::<Vec<_>>().into()
-    }
-
-    /// Creates a [`Buffer`] from an fallible [`Iterator`] with a trusted length.
-    #[inline]
-    pub fn try_from_trusted_len_iter<E, I: TrustedLen<Item = std::result::Result<T, E>>>(
-        iterator: I,
-    ) -> std::result::Result<Self, E> {
-        Ok(iterator.collect::<std::result::Result<Vec<_>, E>>()?.into())
-    }
-
-    /// Creates a [`Buffer`] from an [`Iterator`] with a trusted (upper) length.
-    /// # Safety
-    /// This method assumes that the iterator's size is correct and is undefined behavior
-    /// to use it on an iterator that reports an incorrect length.
-    #[inline]
-    pub unsafe fn from_trusted_len_iter_unchecked<I: Iterator<Item = T>>(iterator: I) -> Self {
-        iterator.collect::<Vec<_>>().into()
-    }
-
-    /// # Safety
-    /// This method assumes that the iterator's size is correct and is undefined behavior
-    /// to use it on an iterator that reports an incorrect length.
-    #[inline]
-    pub unsafe fn try_from_trusted_len_iter_unchecked<
-        E,
-        I: Iterator<Item = std::result::Result<T, E>>,
-    >(
-        iterator: I,
-    ) -> std::result::Result<Self, E> {
-        Ok(iterator.collect::<std::result::Result<Vec<_>, E>>()?.into())
-    }
-}
-
 impl<T: NativeType> From<Vec<T>> for Buffer<T> {
     #[inline]
     fn from(p: Vec<T>) -> Self {
diff --git a/src/compute/sort/lex_sort.rs b/src/compute/sort/lex_sort.rs
index 114093f49a..c598cfd3ab 100644
--- a/src/compute/sort/lex_sort.rs
+++ b/src/compute/sort/lex_sort.rs
@@ -198,7 +198,12 @@ pub fn lexsort_to_indices_impl<I: Index>(
 
     if let Some(limit) = limit {
         let limit = limit.min(row_count);
-        let (before, _, _) = values.select_nth_unstable_by(limit, lex_comparator);
+        let before = if limit < row_count {
+            let (before, _, _) = values.select_nth_unstable_by(limit, lex_comparator);
+            before
+        } else {
+            &mut values[..]
+        };
         before.sort_unstable_by(lex_comparator);
         values.truncate(limit);
         values.shrink_to_fit();
diff --git a/src/compute/sort/mod.rs b/src/compute/sort/mod.rs
index 125e4da526..6f10f4bdb4 100644
--- a/src/compute/sort/mod.rs
+++ b/src/compute/sort/mod.rs
@@ -364,7 +364,7 @@ where
 /// Compare two `Array`s based on the ordering defined in [ord](crate::array::ord).
 fn cmp_array(a: &dyn Array, b: &dyn Array) -> Ordering {
     let cmp_op = ord::build_compare(a, b).unwrap();
-    let length = a.len().max(b.len());
+    let length = a.len().min(b.len());
 
     for i in 0..length {
         let result = cmp_op(i, i);
@@ -372,5 +372,5 @@ fn cmp_array(a: &dyn Array, b: &dyn Array) -> Ordering {
             return result;
         }
     }
-    Ordering::Equal
+    a.len().cmp(&b.len())
 }
diff --git a/src/io/ipc/read/read_basic.rs b/src/io/ipc/read/read_basic.rs
index 5f449917ad..893d62fc4d 100644
--- a/src/io/ipc/read/read_basic.rs
+++ b/src/io/ipc/read/read_basic.rs
@@ -209,7 +209,7 @@ pub fn read_bitmap<R: Read + Seek>(
         read_uncompressed_bitmap(length, bytes, reader)
     }?;
 
-    Ok(Bitmap::from_bytes(buffer.into(), length))
+    Bitmap::try_new(buffer, length)
 }
 
 pub fn read_validity<R: Read + Seek>(
diff --git a/src/types/bit_chunk.rs b/src/types/bit_chunk.rs
index 6466d7cbbb..5bc4e398fd 100644
--- a/src/types/bit_chunk.rs
+++ b/src/types/bit_chunk.rs
@@ -8,7 +8,7 @@ use num_traits::PrimInt;
 use super::NativeType;
 
 /// A chunk of bits. This is used to create masks of a given length
-/// whose width is `1` bit. In `simd_packed` notation, this corresponds to `m1xY`.
+/// whose width is `1` bit. In `portable_simd` notation, this corresponds to `m1xY`.
 pub trait BitChunk:
     super::private::Sealed
     + PrimInt


Test patch:
diff --git a/tests/it/array/binary/mod.rs b/tests/it/array/binary/mod.rs
index 2942188e6a..fb1bb9dcbe 100644
--- a/tests/it/array/binary/mod.rs
+++ b/tests/it/array/binary/mod.rs
@@ -86,24 +86,24 @@ fn with_validity() {
 #[test]
 #[should_panic]
 fn wrong_offsets() {
-    let offsets = Buffer::from_slice([0, 5, 4]); // invalid offsets
-    let values = Buffer::from_slice(b"abbbbb");
+    let offsets = Buffer::from(vec![0, 5, 4]); // invalid offsets
+    let values = Buffer::from(b"abbbbb".to_vec());
     BinaryArray::<i32>::from_data(DataType::Binary, offsets, values, None);
 }
 
 #[test]
 #[should_panic]
 fn wrong_data_type() {
-    let offsets = Buffer::from_slice([0, 4]);
-    let values = Buffer::from_slice(b"abbb");
+    let offsets = Buffer::from(vec![0, 4]);
+    let values = Buffer::from(b"abbb".to_vec());
     BinaryArray::<i32>::from_data(DataType::Int8, offsets, values, None);
 }
 
 #[test]
 #[should_panic]
 fn value_with_wrong_offsets_panics() {
-    let offsets = Buffer::from_slice([0, 10, 11, 4]);
-    let values = Buffer::from_slice(b"abbb");
+    let offsets = Buffer::from(vec![0, 10, 11, 4]);
+    let values = Buffer::from(b"abbb".to_vec());
     // the 10-11 is not checked
     let array = BinaryArray::<i32>::from_data(DataType::Binary, offsets, values, None);
 
@@ -115,8 +115,8 @@ fn value_with_wrong_offsets_panics() {
 #[test]
 #[should_panic]
 fn index_out_of_bounds_panics() {
-    let offsets = Buffer::from_slice([0, 1, 2, 4]);
-    let values = Buffer::from_slice(b"abbb");
+    let offsets = Buffer::from(vec![0, 1, 2, 4]);
+    let values = Buffer::from(b"abbb".to_vec());
     let array = BinaryArray::<i32>::from_data(DataType::Utf8, offsets, values, None);
 
     array.value(3);
@@ -125,8 +125,8 @@ fn index_out_of_bounds_panics() {
 #[test]
 #[should_panic]
 fn value_unchecked_with_wrong_offsets_panics() {
-    let offsets = Buffer::from_slice([0, 10, 11, 4]);
-    let values = Buffer::from_slice(b"abbb");
+    let offsets = Buffer::from(vec![0, 10, 11, 4]);
+    let values = Buffer::from(b"abbb".to_vec());
     // the 10-11 is not checked
     let array = BinaryArray::<i32>::from_data(DataType::Binary, offsets, values, None);
 
diff --git a/tests/it/array/equal/list.rs b/tests/it/array/equal/list.rs
index 8c0ff89b6e..c57a1888fc 100644
--- a/tests/it/array/equal/list.rs
+++ b/tests/it/array/equal/list.rs
@@ -69,7 +69,7 @@ fn test_list_offsets() {
 
 #[test]
 fn test_bla() {
-    let offsets = Buffer::from_slice([0, 3, 3, 6]);
+    let offsets = Buffer::from(vec![0, 3, 3, 6]);
     let data_type = ListArray::<i32>::default_datatype(DataType::Int32);
     let values = Arc::new(Int32Array::from([
         Some(1),
@@ -83,7 +83,7 @@ fn test_bla() {
     let lhs = ListArray::<i32>::from_data(data_type, offsets, values, Some(validity));
     let lhs = lhs.slice(1, 2);
 
-    let offsets = Buffer::from_slice([0, 0, 3]);
+    let offsets = Buffer::from(vec![0, 0, 3]);
     let data_type = ListArray::<i32>::default_datatype(DataType::Int32);
     let values = Arc::new(Int32Array::from([Some(4), None, Some(6)]));
     let validity = Bitmap::from([false, true]);
diff --git a/tests/it/array/fixed_size_binary/mod.rs b/tests/it/array/fixed_size_binary/mod.rs
index f1edb8bf76..3163f66973 100644
--- a/tests/it/array/fixed_size_binary/mod.rs
+++ b/tests/it/array/fixed_size_binary/mod.rs
@@ -6,7 +6,7 @@ mod mutable;
 fn basics() {
     let array = FixedSizeBinaryArray::from_data(
         DataType::FixedSizeBinary(2),
-        Buffer::from_slice([1, 2, 3, 4, 5, 6]),
+        Buffer::from(vec![1, 2, 3, 4, 5, 6]),
         Some(Bitmap::from([true, false, true])),
     );
     assert_eq!(array.size(), 2);
@@ -23,7 +23,7 @@ fn basics() {
 
 #[test]
 fn with_validity() {
-    let values = Buffer::from_slice([1, 2, 3, 4, 5, 6]);
+    let values = Buffer::from(vec![1, 2, 3, 4, 5, 6]);
     let a = FixedSizeBinaryArray::new(DataType::FixedSizeBinary(2), values, None);
     let a = a.with_validity(Some(Bitmap::from([true, false, true])));
     assert!(a.validity().is_some());
@@ -31,7 +31,7 @@ fn with_validity() {
 
 #[test]
 fn debug() {
-    let values = Buffer::from_slice([1, 2, 3, 4, 5, 6]);
+    let values = Buffer::from(vec![1, 2, 3, 4, 5, 6]);
     let a = FixedSizeBinaryArray::from_data(
         DataType::FixedSizeBinary(2),
         values,
@@ -66,26 +66,26 @@ fn from_iter() {
 
 #[test]
 fn wrong_size() {
-    let values = Buffer::from_slice(b"abb");
+    let values = Buffer::from(b"abb".to_vec());
     assert!(FixedSizeBinaryArray::try_new(DataType::FixedSizeBinary(2), values, None).is_err());
 }
 
 #[test]
 fn wrong_len() {
-    let values = Buffer::from_slice(b"abba");
+    let values = Buffer::from(b"abba".to_vec());
     let validity = Some([true, false, false].into()); // it should be 2
     assert!(FixedSizeBinaryArray::try_new(DataType::FixedSizeBinary(2), values, validity).is_err());
 }
 
 #[test]
 fn wrong_data_type() {
-    let values = Buffer::from_slice(b"abba");
+    let values = Buffer::from(b"abba".to_vec());
     assert!(FixedSizeBinaryArray::try_new(DataType::Binary, values, None).is_err());
 }
 
 #[test]
 fn to() {
-    let values = Buffer::from_slice(b"abba");
+    let values = Buffer::from(b"abba".to_vec());
     let a = FixedSizeBinaryArray::new(DataType::FixedSizeBinary(2), values, None);
 
     let extension = DataType::Extension(
diff --git a/tests/it/array/list/mod.rs b/tests/it/array/list/mod.rs
index 2a4e1feca4..c0e8ff19b1 100644
--- a/tests/it/array/list/mod.rs
+++ b/tests/it/array/list/mod.rs
@@ -8,13 +8,13 @@ mod mutable;
 
 #[test]
 fn debug() {
-    let values = Buffer::from_slice([1, 2, 3, 4, 5]);
+    let values = Buffer::from(vec![1, 2, 3, 4, 5]);
     let values = PrimitiveArray::<i32>::from_data(DataType::Int32, values, None);
 
     let data_type = ListArray::<i32>::default_datatype(DataType::Int32);
     let array = ListArray::<i32>::from_data(
         data_type,
-        Buffer::from_slice([0, 2, 2, 3, 5]),
+        Buffer::from(vec![0, 2, 2, 3, 5]),
         Arc::new(values),
         None,
     );
@@ -25,13 +25,13 @@ fn debug() {
 #[test]
 #[should_panic]
 fn test_nested_panic() {
-    let values = Buffer::from_slice([1, 2, 3, 4, 5]);
+    let values = Buffer::from(vec![1, 2, 3, 4, 5]);
     let values = PrimitiveArray::<i32>::from_data(DataType::Int32, values, None);
 
     let data_type = ListArray::<i32>::default_datatype(DataType::Int32);
     let array = ListArray::<i32>::from_data(
         data_type.clone(),
-        Buffer::from_slice([0, 2, 2, 3, 5]),
+        Buffer::from(vec![0, 2, 2, 3, 5]),
         Arc::new(values),
         None,
     );
@@ -40,7 +40,7 @@ fn test_nested_panic() {
     // the nested structure of the child data
     let _ = ListArray::<i32>::from_data(
         data_type,
-        Buffer::from_slice([0, 2, 4]),
+        Buffer::from(vec![0, 2, 4]),
         Arc::new(array),
         None,
     );
@@ -48,13 +48,13 @@ fn test_nested_panic() {
 
 #[test]
 fn test_nested_display() {
-    let values = Buffer::from_slice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
+    let values = Buffer::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
     let values = PrimitiveArray::<i32>::from_data(DataType::Int32, values, None);
 
     let data_type = ListArray::<i32>::default_datatype(DataType::Int32);
     let array = ListArray::<i32>::from_data(
         data_type,
-        Buffer::from_slice([0, 2, 4, 7, 7, 8, 10]),
+        Buffer::from(vec![0, 2, 4, 7, 7, 8, 10]),
         Arc::new(values),
         None,
     );
@@ -62,7 +62,7 @@ fn test_nested_display() {
     let data_type = ListArray::<i32>::default_datatype(array.data_type().clone());
     let nested = ListArray::<i32>::from_data(
         data_type,
-        Buffer::from_slice([0, 2, 5, 6]),
+        Buffer::from(vec![0, 2, 5, 6]),
         Arc::new(array),
         None,
     );
diff --git a/tests/it/array/list/mutable.rs b/tests/it/array/list/mutable.rs
index e87d5354f2..f7d21bdc51 100644
--- a/tests/it/array/list/mutable.rs
+++ b/tests/it/array/list/mutable.rs
@@ -16,14 +16,14 @@ fn basics() {
 
     let values = PrimitiveArray::<i32>::from_data(
         DataType::Int32,
-        Buffer::from_slice([1, 2, 3, 4, 0, 6]),
+        Buffer::from(vec![1, 2, 3, 4, 0, 6]),
         Some(Bitmap::from([true, true, true, true, false, true])),
     );
 
     let data_type = ListArray::<i32>::default_datatype(DataType::Int32);
     let expected = ListArray::<i32>::from_data(
         data_type,
-        Buffer::from_slice([0, 3, 3, 6]),
+        Buffer::from(vec![0, 3, 3, 6]),
         Arc::new(values),
         Some(Bitmap::from([true, false, true])),
     );
diff --git a/tests/it/array/primitive/mod.rs b/tests/it/array/primitive/mod.rs
index 7599d8ef2c..058d503f47 100644
--- a/tests/it/array/primitive/mod.rs
+++ b/tests/it/array/primitive/mod.rs
@@ -87,20 +87,20 @@ fn months_days_ns() {
 
 #[test]
 fn wrong_data_type() {
-    let values = Buffer::from_slice(b"abbb");
+    let values = Buffer::from(b"abbb".to_vec());
     assert!(PrimitiveArray::try_new(DataType::Utf8, values, None).is_err());
 }
 
 #[test]
 fn wrong_len() {
-    let values = Buffer::from_slice(b"abbb");
+    let values = Buffer::from(b"abbb".to_vec());
     let validity = Some([true, false].into());
     assert!(PrimitiveArray::try_new(DataType::Utf8, values, validity).is_err());
 }
 
 #[test]
 fn into_mut_1() {
-    let values = Buffer::<i32>::from_slice([0, 1]);
+    let values = Buffer::<i32>::from(vec![0, 1]);
     let a = values.clone(); // cloned values
     assert_eq!(a, values);
     let array = PrimitiveArray::new(DataType::Int32, values, None);
@@ -109,7 +109,7 @@ fn into_mut_1() {
 
 #[test]
 fn into_mut_2() {
-    let values = Buffer::<i32>::from_slice([0, 1]);
+    let values = Buffer::<i32>::from(vec![0, 1]);
     let validity = Some([true, false].into());
     let a = validity.clone(); // cloned values
     assert_eq!(a, validity);
@@ -119,7 +119,7 @@ fn into_mut_2() {
 
 #[test]
 fn into_mut_3() {
-    let values = Buffer::<i32>::from_slice([0, 1]);
+    let values = Buffer::<i32>::from(vec![0, 1]);
     let validity = Some([true, false].into());
     let array = PrimitiveArray::new(DataType::Int32, values, validity);
     assert!(array.into_mut().is_right());
diff --git a/tests/it/array/union.rs b/tests/it/array/union.rs
index b52b5ff4fd..6d8a1f54f7 100644
--- a/tests/it/array/union.rs
+++ b/tests/it/array/union.rs
@@ -69,7 +69,7 @@ fn slice() -> Result<()> {
         Field::new("b", DataType::Utf8, true),
     ];
     let data_type = DataType::Union(fields, None, UnionMode::Sparse);
-    let types = Buffer::from_slice([0, 0, 1]);
+    let types = Buffer::from(vec![0, 0, 1]);
     let fields = vec![
         Arc::new(Int32Array::from(&[Some(1), None, Some(2)])) as Arc<dyn Array>,
         Arc::new(Utf8Array::<i32>::from(&[Some("a"), Some("b"), Some("c")])) as Arc<dyn Array>,
@@ -79,7 +79,7 @@ fn slice() -> Result<()> {
 
     let result = array.slice(1, 2);
 
-    let sliced_types = Buffer::from_slice([0, 1]);
+    let sliced_types = Buffer::from(vec![0, 1]);
     let sliced_fields = vec![
         Arc::new(Int32Array::from(&[None, Some(2)])) as Arc<dyn Array>,
         Arc::new(Utf8Array::<i32>::from(&[Some("b"), Some("c")])) as Arc<dyn Array>,
@@ -97,7 +97,7 @@ fn iter_sparse() -> Result<()> {
         Field::new("b", DataType::Utf8, true),
     ];
     let data_type = DataType::Union(fields, None, UnionMode::Sparse);
-    let types = Buffer::from_slice([0, 0, 1]);
+    let types = Buffer::from(vec![0, 0, 1]);
     let fields = vec![
         Arc::new(Int32Array::from(&[Some(1), None, Some(2)])) as Arc<dyn Array>,
         Arc::new(Utf8Array::<i32>::from(&[Some("a"), Some("b"), Some("c")])) as Arc<dyn Array>,
@@ -130,8 +130,8 @@ fn iter_dense() -> Result<()> {
         Field::new("b", DataType::Utf8, true),
     ];
     let data_type = DataType::Union(fields, None, UnionMode::Dense);
-    let types = Buffer::from_slice([0, 0, 1]);
-    let offsets = Buffer::<i32>::from_slice([0, 1, 0]);
+    let types = Buffer::from(vec![0, 0, 1]);
+    let offsets = Buffer::<i32>::from(vec![0, 1, 0]);
     let fields = vec![
         Arc::new(Int32Array::from(&[Some(1), None])) as Arc<dyn Array>,
         Arc::new(Utf8Array::<i32>::from(&[Some("c")])) as Arc<dyn Array>,
@@ -164,7 +164,7 @@ fn iter_sparse_slice() -> Result<()> {
         Field::new("b", DataType::Utf8, true),
     ];
     let data_type = DataType::Union(fields, None, UnionMode::Sparse);
-    let types = Buffer::from_slice([0, 0, 1]);
+    let types = Buffer::from(vec![0, 0, 1]);
     let fields = vec![
         Arc::new(Int32Array::from(&[Some(1), Some(3), Some(2)])) as Arc<dyn Array>,
         Arc::new(Utf8Array::<i32>::from(&[Some("a"), Some("b"), Some("c")])) as Arc<dyn Array>,
@@ -190,8 +190,8 @@ fn iter_dense_slice() -> Result<()> {
         Field::new("b", DataType::Utf8, true),
     ];
     let data_type = DataType::Union(fields, None, UnionMode::Dense);
-    let types = Buffer::from_slice([0, 0, 1]);
-    let offsets = Buffer::<i32>::from_slice([0, 1, 0]);
+    let types = Buffer::from(vec![0, 0, 1]);
+    let offsets = Buffer::<i32>::from(vec![0, 1, 0]);
     let fields = vec![
         Arc::new(Int32Array::from(&[Some(1), Some(3)])) as Arc<dyn Array>,
         Arc::new(Utf8Array::<i32>::from(&[Some("c")])) as Arc<dyn Array>,
@@ -217,8 +217,8 @@ fn scalar() -> Result<()> {
         Field::new("b", DataType::Utf8, true),
     ];
     let data_type = DataType::Union(fields, None, UnionMode::Dense);
-    let types = Buffer::from_slice([0, 0, 1]);
-    let offsets = Buffer::<i32>::from_slice([0, 1, 0]);
+    let types = Buffer::from(vec![0, 0, 1]);
+    let offsets = Buffer::<i32>::from(vec![0, 1, 0]);
     let fields = vec![
         Arc::new(Int32Array::from(&[Some(1), None])) as Arc<dyn Array>,
         Arc::new(Utf8Array::<i32>::from(&[Some("c")])) as Arc<dyn Array>,
diff --git a/tests/it/array/utf8/mod.rs b/tests/it/array/utf8/mod.rs
index 90e71440b5..fc55ebccde 100644
--- a/tests/it/array/utf8/mod.rs
+++ b/tests/it/array/utf8/mod.rs
@@ -59,8 +59,8 @@ fn from() {
 fn from_slice() {
     let b = Utf8Array::<i32>::from_slice(["a", "b", "cc"]);
 
-    let offsets = Buffer::from_slice([0, 1, 2, 4]);
-    let values = Buffer::from_slice("abcc".as_bytes());
+    let offsets = Buffer::from(vec![0, 1, 2, 4]);
+    let values = Buffer::from(b"abcc".to_vec());
     assert_eq!(
         b,
         Utf8Array::<i32>::from_data(DataType::Utf8, offsets, values, None)
@@ -71,8 +71,8 @@ fn from_slice() {
 fn from_iter_values() {
     let b = Utf8Array::<i32>::from_iter_values(["a", "b", "cc"].iter());
 
-    let offsets = Buffer::from_slice([0, 1, 2, 4]);
-    let values = Buffer::from_slice(b"abcc");
+    let offsets = Buffer::from(vec![0, 1, 2, 4]);
+    let values = Buffer::from(b"abcc".to_vec());
     assert_eq!(
         b,
         Utf8Array::<i32>::from_data(DataType::Utf8, offsets, values, None)
@@ -84,8 +84,8 @@ fn from_trusted_len_iter() {
     let b =
         Utf8Array::<i32>::from_trusted_len_iter(vec![Some("a"), Some("b"), Some("cc")].into_iter());
 
-    let offsets = Buffer::from_slice([0, 1, 2, 4]);
-    let values = Buffer::from_slice(b"abcc");
+    let offsets = Buffer::from(vec![0, 1, 2, 4]);
+    let values = Buffer::from(b"abcc".to_vec());
     assert_eq!(
         b,
         Utf8Array::<i32>::from_data(DataType::Utf8, offsets, values, None)
@@ -101,8 +101,8 @@ fn try_from_trusted_len_iter() {
     )
     .unwrap();
 
-    let offsets = Buffer::from_slice([0, 1, 2, 4]);
-    let values = Buffer::from_slice("abcc".as_bytes());
+    let offsets = Buffer::from(vec![0, 1, 2, 4]);
+    let values = Buffer::from(b"abcc".to_vec());
     assert_eq!(
         b,
         Utf8Array::<i32>::from_data(DataType::Utf8, offsets, values, None)
@@ -111,59 +111,59 @@ fn try_from_trusted_len_iter() {
 
 #[test]
 fn not_utf8() {
-    let offsets = Buffer::from_slice([0, 4]);
-    let values = Buffer::from_slice([0, 159, 146, 150]); // invalid utf8
+    let offsets = Buffer::from(vec![0, 4]);
+    let values = Buffer::from(vec![0, 159, 146, 150]); // invalid utf8
     assert!(Utf8Array::<i32>::try_new(DataType::Utf8, offsets, values, None).is_err());
 }
 
 #[test]
 fn not_utf8_individually() {
-    let offsets = Buffer::from_slice([0, 1, 2]);
-    let values = Buffer::from_slice([207, 128]); // each is invalid utf8, but together is valid
+    let offsets = Buffer::from(vec![0, 1, 2]);
+    let values = Buffer::from(vec![207, 128]); // each is invalid utf8, but together is valid
     assert!(Utf8Array::<i32>::try_new(DataType::Utf8, offsets, values, None).is_err());
 }
 
 #[test]
 fn wrong_offsets() {
-    let offsets = Buffer::from_slice([0, 5, 4]); // invalid offsets
-    let values = Buffer::from_slice(b"abbbbb");
+    let offsets = Buffer::from(vec![0, 5, 4]); // invalid offsets
+    let values = Buffer::from(b"abbbbb".to_vec());
     assert!(Utf8Array::<i32>::try_new(DataType::Utf8, offsets, values, None).is_err());
 }
 
 #[test]
 fn wrong_data_type() {
-    let offsets = Buffer::from_slice([0, 4]);
-    let values = Buffer::from_slice(b"abbb");
+    let offsets = Buffer::from(vec![0, 4]);
+    let values = Buffer::from(b"abbb".to_vec());
     assert!(Utf8Array::<i32>::try_new(DataType::Int32, offsets, values, None).is_err());
 }
 
 #[test]
 fn out_of_bounds_offsets_panics() {
     // the 10 is out of bounds
-    let offsets = Buffer::from_slice([0, 10, 11]);
-    let values = Buffer::from_slice(b"abbb");
+    let offsets = Buffer::from(vec![0, 10, 11]);
+    let values = Buffer::from(b"abbb".to_vec());
     assert!(Utf8Array::<i32>::try_new(DataType::Utf8, offsets, values, None).is_err());
 }
 
 #[test]
 fn decreasing_offset_and_ascii_panics() {
-    let offsets = Buffer::from_slice([0, 2, 1]);
-    let values = Buffer::from_slice(b"abbb");
+    let offsets = Buffer::from(vec![0, 2, 1]);
+    let values = Buffer::from(b"abbb".to_vec());
     assert!(Utf8Array::<i32>::try_new(DataType::Utf8, offsets, values, None).is_err());
 }
 
 #[test]
 fn decreasing_offset_and_utf8_panics() {
-    let offsets = Buffer::from_slice([0, 2, 4, 2]); // not increasing
-    let values = Buffer::from_slice([207, 128, 207, 128, 207, 128]); // valid utf8
+    let offsets = Buffer::from(vec![0, 2, 4, 2]); // not increasing
+    let values = Buffer::from(vec![207, 128, 207, 128, 207, 128]); // valid utf8
     assert!(Utf8Array::<i32>::try_new(DataType::Utf8, offsets, values, None).is_err());
 }
 
 #[test]
 #[should_panic]
 fn index_out_of_bounds_panics() {
-    let offsets = Buffer::from_slice([0, 1, 2, 4]);
-    let values = Buffer::from_slice(b"abbb");
+    let offsets = Buffer::from(vec![0, 1, 2, 4]);
+    let values = Buffer::from(b"abbb".to_vec());
     let array = Utf8Array::<i32>::from_data(DataType::Utf8, offsets, values, None);
 
     array.value(3);
@@ -178,8 +178,8 @@ fn debug() {
 
 #[test]
 fn into_mut_1() {
-    let offsets = Buffer::from_slice([0, 1]);
-    let values = Buffer::from_slice(b"a");
+    let offsets = Buffer::from(vec![0, 1]);
+    let values = Buffer::from(b"a".to_vec());
     let a = values.clone(); // cloned values
     assert_eq!(a, values);
     let array = Utf8Array::<i32>::from_data(DataType::Utf8, offsets, values, None);
@@ -188,8 +188,8 @@ fn into_mut_1() {
 
 #[test]
 fn into_mut_2() {
-    let offsets = Buffer::from_slice([0, 1]);
-    let values = Buffer::from_slice(b"a");
+    let offsets = Buffer::from(vec![0, 1]);
+    let values = Buffer::from(b"a".to_vec());
     let a = offsets.clone(); // cloned offsets
     assert_eq!(a, offsets);
     let array = Utf8Array::<i32>::from_data(DataType::Utf8, offsets, values, None);
@@ -198,8 +198,8 @@ fn into_mut_2() {
 
 #[test]
 fn into_mut_3() {
-    let offsets = Buffer::from_slice([0, 1]);
-    let values = Buffer::from_slice(b"a");
+    let offsets = Buffer::from(vec![0, 1]);
+    let values = Buffer::from(b"a".to_vec());
     let validity = Some([true].into());
     let a = validity.clone(); // cloned validity
     assert_eq!(a, validity);
@@ -209,8 +209,8 @@ fn into_mut_3() {
 
 #[test]
 fn into_mut_4() {
-    let offsets = Buffer::from_slice([0, 1]);
-    let values = Buffer::from_slice(b"a");
+    let offsets = Buffer::from(vec![0, 1]);
+    let values = Buffer::from(b"a".to_vec());
     let validity = Some([true].into());
     let array = Utf8Array::<i32>::new(DataType::Utf8, offsets, values, validity);
     assert!(array.into_mut().is_right());
diff --git a/tests/it/buffer/immutable.rs b/tests/it/buffer/immutable.rs
index 48c4e24cb6..9398759e95 100644
--- a/tests/it/buffer/immutable.rs
+++ b/tests/it/buffer/immutable.rs
@@ -7,24 +7,16 @@ fn new() {
     assert!(buffer.is_empty());
 }
 
-#[test]
-fn new_zeroed() {
-    let buffer = Buffer::<i32>::new_zeroed(2);
-    assert_eq!(buffer.len(), 2);
-    assert!(!buffer.is_empty());
-    assert_eq!(buffer.as_slice(), &[0, 0]);
-}
-
 #[test]
 fn from_slice() {
-    let buffer = Buffer::<i32>::from_slice([0, 1, 2]);
+    let buffer = Buffer::<i32>::from(vec![0, 1, 2]);
     assert_eq!(buffer.len(), 3);
     assert_eq!(buffer.as_slice(), &[0, 1, 2]);
 }
 
 #[test]
 fn slice() {
-    let buffer = Buffer::<i32>::from_slice([0, 1, 2, 3]);
+    let buffer = Buffer::<i32>::from(vec![0, 1, 2, 3]);
     let buffer = buffer.slice(1, 2);
     assert_eq!(buffer.len(), 2);
     assert_eq!(buffer.as_slice(), &[1, 2]);
@@ -37,32 +29,9 @@ fn from_iter() {
     assert_eq!(buffer.as_slice(), &[0, 1, 2]);
 }
 
-#[test]
-fn from_trusted_len_iter() {
-    let buffer = unsafe { Buffer::<i32>::from_trusted_len_iter_unchecked(0..3) };
-    assert_eq!(buffer.len(), 3);
-    assert_eq!(buffer.as_slice(), &[0, 1, 2]);
-}
-
-#[test]
-fn try_from_trusted_len_iter() {
-    let iter = (0..3).map(Result::<_, String>::Ok);
-    let buffer = unsafe { Buffer::<i32>::try_from_trusted_len_iter_unchecked(iter) }.unwrap();
-    assert_eq!(buffer.len(), 3);
-    assert_eq!(buffer.as_slice(), &[0, 1, 2]);
-}
-
-#[test]
-fn as_ptr() {
-    let buffer = Buffer::<i32>::from_slice([0, 1, 2, 3]);
-    let buffer = buffer.slice(1, 2);
-    let ptr = buffer.as_ptr();
-    assert_eq!(unsafe { *ptr }, 1);
-}
-
 #[test]
 fn debug() {
-    let buffer = Buffer::<i32>::from_slice([0, 1, 2, 3]);
+    let buffer = Buffer::<i32>::from(vec![0, 1, 2, 3]);
     let buffer = buffer.slice(1, 2);
     let a = format!("{:?}", buffer);
     assert_eq!(a, "[1, 2]")
diff --git a/tests/it/compute/sort/lex_sort.rs b/tests/it/compute/sort/lex_sort.rs
index bf0b146a8f..a24f116dc8 100644
--- a/tests/it/compute/sort/lex_sort.rs
+++ b/tests/it/compute/sort/lex_sort.rs
@@ -5,6 +5,13 @@ fn test_lex_sort_arrays(input: Vec<SortColumn>, expected: Vec<Box<dyn Array>>) {
     let sorted = lexsort::<i32>(&input, None).unwrap();
     assert_eq!(sorted, expected);
 
+    let sorted = lexsort::<i32>(&input, Some(4)).unwrap();
+    let expected = expected
+        .into_iter()
+        .map(|x| x.slice(0, 4))
+        .collect::<Vec<_>>();
+    assert_eq!(sorted, expected);
+
     let sorted = lexsort::<i32>(&input, Some(2)).unwrap();
     let expected = expected
         .into_iter()
diff --git a/tests/it/compute/take.rs b/tests/it/compute/take.rs
index 676152b727..f2f5c1bd39 100644
--- a/tests/it/compute/take.rs
+++ b/tests/it/compute/take.rs
@@ -178,13 +178,13 @@ fn unsigned_take() {
 
 #[test]
 fn list_with_no_none() {
-    let values = Buffer::from_slice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
+    let values = Buffer::from(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
     let values = PrimitiveArray::<i32>::from_data(DataType::Int32, values, None);
 
     let data_type = ListArray::<i32>::default_datatype(DataType::Int32);
     let array = ListArray::<i32>::from_data(
         data_type,
-        Buffer::from_slice([0, 2, 2, 6, 9, 10]),
+        Buffer::from(vec![0, 2, 2, 6, 9, 10]),
         Arc::new(values),
         None,
     );
@@ -192,12 +192,12 @@ fn list_with_no_none() {
     let indices = PrimitiveArray::from([Some(4i32), Some(1), Some(3)]);
     let result = take(&array, &indices).unwrap();
 
-    let expected_values = Buffer::from_slice([9, 6, 7, 8]);
+    let expected_values = Buffer::from(vec![9, 6, 7, 8]);
     let expected_values = PrimitiveArray::<i32>::from_data(DataType::Int32, expected_values, None);
     let expected_type = ListArray::<i32>::default_datatype(DataType::Int32);
     let expected = ListArray::<i32>::from_data(
         expected_type,
-        Buffer::from_slice([0, 1, 1, 4]),
+        Buffer::from(vec![0, 1, 1, 4]),
         Arc::new(expected_values),
         None,
     );
@@ -207,7 +207,7 @@ fn list_with_no_none() {
 
 #[test]
 fn list_with_none() {
-    let values = Buffer::from_slice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
+    let values = Buffer::from(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
     let values = PrimitiveArray::<i32>::from_data(DataType::Int32, values, None);
 
     let validity_values = vec![true, false, true, true, true];
@@ -216,7 +216,7 @@ fn list_with_none() {
     let data_type = ListArray::<i32>::default_datatype(DataType::Int32);
     let array = ListArray::<i32>::from_data(
         data_type,
-        Buffer::from_slice([0, 2, 2, 6, 9, 10]),
+        Buffer::from(vec![0, 2, 2, 6, 9, 10]),
         Arc::new(values),
         Some(validity),
     );
@@ -269,13 +269,13 @@ fn list_both_validity() {
 
 #[test]
 fn test_nested() {
-    let values = Buffer::from_slice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
+    let values = Buffer::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
     let values = PrimitiveArray::<i32>::from_data(DataType::Int32, values, None);
 
     let data_type = ListArray::<i32>::default_datatype(DataType::Int32);
     let array = ListArray::<i32>::from_data(
         data_type,
-        Buffer::from_slice([0, 2, 4, 7, 7, 8, 10]),
+        Buffer::from(vec![0, 2, 4, 7, 7, 8, 10]),
         Arc::new(values),
         None,
     );
@@ -283,7 +283,7 @@ fn test_nested() {
     let data_type = ListArray::<i32>::default_datatype(array.data_type().clone());
     let nested = ListArray::<i32>::from_data(
         data_type,
-        Buffer::from_slice([0, 2, 5, 6]),
+        Buffer::from(vec![0, 2, 5, 6]),
         Arc::new(array),
         None,
     );
@@ -292,13 +292,13 @@ fn test_nested() {
     let result = take(&nested, &indices).unwrap();
 
     // expected data
-    let expected_values = Buffer::from_slice([1, 2, 3, 4, 5, 6, 7, 8]);
+    let expected_values = Buffer::from(vec![1, 2, 3, 4, 5, 6, 7, 8]);
     let expected_values = PrimitiveArray::<i32>::from_data(DataType::Int32, expected_values, None);
 
     let expected_data_type = ListArray::<i32>::default_datatype(DataType::Int32);
     let expected_array = ListArray::<i32>::from_data(
         expected_data_type,
-        Buffer::from_slice([0, 2, 4, 7, 7, 8]),
+        Buffer::from(vec![0, 2, 4, 7, 7, 8]),
         Arc::new(expected_values),
         None,
     );
@@ -306,7 +306,7 @@ fn test_nested() {
     let expected_data_type = ListArray::<i32>::default_datatype(expected_array.data_type().clone());
     let expected = ListArray::<i32>::from_data(
         expected_data_type,
-        Buffer::from_slice([0, 2, 5]),
+        Buffer::from(vec![0, 2, 5]),
         Arc::new(expected_array),
         None,
     );
diff --git a/tests/it/io/json/write.rs b/tests/it/io/json/write.rs
index d79a5310f0..dc7cd98746 100644
--- a/tests/it/io/json/write.rs
+++ b/tests/it/io/json/write.rs
@@ -249,7 +249,7 @@ fn list_of_struct() -> Result<()> {
     // [{"c11": 5, "c12": {"c121": "g"}}]
     let c1 = ListArray::<i32>::from_data(
         c1_datatype,
-        Buffer::from_slice([0, 2, 2, 3]),
+        Buffer::from(vec![0, 2, 2, 3]),
         Arc::new(s),
         Some(Bitmap::from_u8_slice([0b00000101], 3)),
     );
diff --git a/tests/it/io/ndjson/mod.rs b/tests/it/io/ndjson/mod.rs
index 1e25de7387..163abc0280 100644
--- a/tests/it/io/ndjson/mod.rs
+++ b/tests/it/io/ndjson/mod.rs
@@ -297,7 +297,7 @@ fn case_nested_list() -> (String, Arc<dyn Array>) {
     );
     let expected = ListArray::from_data(
         a_list_data_type,
-        Buffer::from_slice([0i32, 2, 3, 6, 6, 6]),
+        Buffer::from(vec![0i32, 2, 3, 6, 6, 6]),
         Arc::new(a_struct) as Arc<dyn Array>,
         Some(Bitmap::from_u8_slice([0b00010111], 5)),
     );
diff --git a/tests/it/io/parquet/mod.rs b/tests/it/io/parquet/mod.rs
index 2da19374ae..68a0ea43e0 100644
--- a/tests/it/io/parquet/mod.rs
+++ b/tests/it/io/parquet/mod.rs
@@ -68,7 +68,7 @@ pub fn pyarrow_nested_edge(column: &str) -> Box<dyn Array> {
 }
 
 pub fn pyarrow_nested_nullable(column: &str) -> Box<dyn Array> {
-    let offsets = Buffer::from_slice([0, 2, 2, 5, 8, 8, 11, 11, 12]);
+    let offsets = Buffer::from(vec![0, 2, 2, 5, 8, 8, 11, 11, 12]);
 
     let values = match column {
         "list_int64" => {
diff --git a/tests/it/io/print.rs b/tests/it/io/print.rs
index 68eccdcc32..5b16931a70 100644
--- a/tests/it/io/print.rs
+++ b/tests/it/io/print.rs
@@ -359,7 +359,7 @@ fn write_union() -> Result<()> {
         Field::new("b", DataType::Utf8, true),
     ];
     let data_type = DataType::Union(fields, None, UnionMode::Sparse);
-    let types = Buffer::from_slice([0, 0, 1]);
+    let types = Buffer::from(vec![0, 0, 1]);
     let fields = vec![
         Arc::new(Int32Array::from(&[Some(1), None, Some(2)])) as Arc<dyn Array>,
         Arc::new(Utf8Array::<i32>::from(&[Some("a"), Some("b"), Some("c")])) as Arc<dyn Array>,