diff --git a/src/array/utf8/mutable.rs b/src/array/utf8/mutable.rs index b80c73e212c..12a81d5c34a 100644 --- a/src/array/utf8/mutable.rs +++ b/src/array/utf8/mutable.rs @@ -66,10 +66,9 @@ impl Default for MutableUtf8Array { impl MutableUtf8Array { /// Initializes a new empty [`MutableUtf8Array`]. pub fn new() -> Self { - let offsets = vec![O::default()]; Self { data_type: Self::default_data_type(), - offsets, + offsets: vec![O::default()], values: Vec::::new(), validity: None, } diff --git a/src/buffer/bytes.rs b/src/buffer/bytes.rs index 3e33fc7ce45..de9b6b73b5a 100644 --- a/src/buffer/bytes.rs +++ b/src/buffer/bytes.rs @@ -31,10 +31,8 @@ impl Debug for Deallocation { /// A continuous, fixed-size, immutable memory region that knows how to de-allocate itself. /// -/// In the most common case, this buffer is allocated using [`allocate_aligned`](alloc::allocate_aligned) -/// and deallocated accordingly [`free_aligned`](alloc::free_aligned). -/// When the region is allocated by a foreign allocator, [Deallocation::Foreign], this calls the -/// foreign deallocator to deallocate the region when it is no longer needed. +/// In the most common case, this buffer is allocated using Rust's native allocator. +/// However, it may also be allocated by a foreign allocator, [Deallocation::Foreign]. pub struct Bytes { /// inner data data: MaybeForeign, diff --git a/src/buffer/immutable.rs b/src/buffer/immutable.rs index c4e73ce33d9..8bbe0115d2e 100644 --- a/src/buffer/immutable.rs +++ b/src/buffer/immutable.rs @@ -1,37 +1,37 @@ -use either::Either; use std::{iter::FromIterator, sync::Arc, usize}; 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 of plain old data types +/// that can be shared across thread boundaries. /// -/// The easiest way to think about `Buffer` is being equivalent to +/// The easiest way to think about [`Buffer`] is being equivalent to /// a `Arc>`, with the following differences: /// * `T` must be [`NativeType`] -/// * slicing the buffer is `O(1)` +/// * slicing and cloning 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`. +/// The easiest way to create one is to use its implementation of `From>`. /// /// # Examples /// ``` /// use arrow2::buffer::Buffer; /// -/// let buffer: Buffer = vec![1, 2, 3].into(); +/// let mut buffer: Buffer = 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 = buffer.into_mut().right().unwrap(); -/// assert_eq!(vec, vec![1, 2, 3]); +/// let vec: &mut [u32] = buffer.get_mut().unwrap(); +/// assert_eq!(vec, &mut [1, 2, 3]); /// /// // cloning and slicing is `O(1)` (data is shared) -/// let buffer: Buffer = vec![1, 2, 3].into(); +/// let mut buffer: Buffer = 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 = buffer.into_mut().left().unwrap(); +/// // but cloning forbids getting mut since `slice` and `buffer` now share data +/// assert_eq!(buffer.get_mut(), None); /// ``` #[derive(Clone, PartialEq)] pub struct Buffer { @@ -100,7 +100,7 @@ impl Buffer { } } - /// Returns a new [Buffer] that is a slice of this buffer starting at `offset`. + /// Returns a new [`Buffer`] that is a slice of this buffer starting at `offset`. /// Doing so allows the same memory region to be shared between buffers. /// # Panics /// Panics iff `offset` is larger than `len`. @@ -114,7 +114,7 @@ impl Buffer { unsafe { self.slice_unchecked(offset, length) } } - /// Returns a new [Buffer] that is a slice of this buffer starting at `offset`. + /// Returns a new [`Buffer`] that is a slice of this buffer starting at `offset`. /// Doing so allows the same memory region to be shared between buffers. /// # Safety /// The caller must ensure `offset + length <= self.len()` @@ -137,10 +137,10 @@ impl Buffer { self.offset } - /// Gets a mutable reference to its underlying [`Vec`], if it not being shared. + /// Returns a mutable reference to its underlying [`Vec`], if possible. /// - /// This operation returns a [`Vec`] iff this [`Buffer`]: - /// * is not an offsetted slice of another [`Buffer`] + /// This operation returns [`Some`] iff this [`Buffer`]: + /// * has not been sliced with an offset /// * has not been cloned (i.e. [`Arc`]`::get_mut` yields [`Some`]) /// * has not been imported from the c data interface (FFI) pub fn get_mut(&mut self) -> Option<&mut Vec> { @@ -151,32 +151,6 @@ impl Buffer { } } - /// Converts this [`Buffer`] to either a [`Buffer`] or a [`Vec`], returning itself if the conversion - /// is not possible - /// - /// This operation returns a [`Vec`] iff this [`Buffer`]: - /// * is not an offsetted slice of another [`Buffer`] - /// * has not been cloned (i.e. [`Arc`]`::get_mut` yields [`Some`]) - /// * has not been imported from the c data interface (FFI) - pub fn into_mut(mut self) -> Either> { - if let Some(vec) = self.get_mut() { - Either::Right(std::mem::take(vec)) - } else { - Either::Left(self) - } - } - - /// Converts this [`Buffer`] to a [`Vec`], cloning the data if needed, also - /// known as clone-on-write semantics. - /// - /// This function is O(1) under the same conditions that [`Self::into_mut`] returns `Vec`. - pub fn make_mut(self) -> Vec { - match self.into_mut() { - Either::Left(data) => data.as_ref().to_vec(), - Either::Right(data) => data, - } - } - /// Get the strong count of underlying `Arc` data buffer. pub fn shared_count_strong(&self) -> usize { Arc::strong_count(&self.data) diff --git a/src/compute/arity_assign.rs b/src/compute/arity_assign.rs index 79d8f579b30..b7fbdeab3d6 100644 --- a/src/compute/arity_assign.rs +++ b/src/compute/arity_assign.rs @@ -66,11 +66,9 @@ where // alloc new region &immutable & rhs } - Either::Right(mut mutable) => { + Either::Right(mutable) => { // mutate in place - let mut mutable_ref = &mut mutable; - mutable_ref &= rhs; - mutable.into() + (mutable & rhs).into() } } });