diff --git a/src/array/binary/mod.rs b/src/array/binary/mod.rs index 91b402b69d5..73860840bc5 100644 --- a/src/array/binary/mod.rs +++ b/src/array/binary/mod.rs @@ -202,35 +202,8 @@ impl BinaryArray { } impl_sliced!(); - - /// Boxes self into a [`Box`]. - pub fn boxed(self) -> Box { - Box::new(self) - } - - /// Boxes self into a [`std::sync::Arc`]. - pub fn arced(self) -> std::sync::Arc { - std::sync::Arc::new(self) - } - - /// Returns this [`BinaryArray`] with a new validity. - /// # Panic - /// Panics iff `validity.len() != self.len()`. - #[must_use] - pub fn with_validity(mut self, validity: Option) -> Self { - self.set_validity(validity); - self - } - - /// Sets the validity of this [`BinaryArray`]. - /// # Panics - /// This function panics iff `values.len() != self.len()`. - pub fn set_validity(&mut self, validity: Option) { - if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) { - panic!("validity must be equal to the array's length") - } - self.validity = validity; - } + impl_mut_validity!(); + impl_into_array!(); /// Try to convert this `BinaryArray` to a `MutableBinaryArray` pub fn into_mut(mut self) -> Either> { @@ -409,44 +382,16 @@ impl BinaryArray { } impl Array for BinaryArray { - #[inline] - fn as_any(&self) -> &dyn std::any::Any { - self - } - - #[inline] - fn as_any_mut(&mut self) -> &mut dyn std::any::Any { - self - } - - #[inline] - fn len(&self) -> usize { - self.len() - } - - #[inline] - fn data_type(&self) -> &DataType { - &self.data_type - } + impl_common_array!(); fn validity(&self) -> Option<&Bitmap> { self.validity.as_ref() } - fn slice(&mut self, offset: usize, length: usize) { - self.slice(offset, length) - } - - unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { - self.slice_unchecked(offset, length) - } - + #[inline] fn with_validity(&self, validity: Option) -> Box { Box::new(self.clone().with_validity(validity)) } - fn to_boxed(&self) -> Box { - Box::new(self.clone()) - } } unsafe impl GenericBinaryArray for BinaryArray { diff --git a/src/array/boolean/mod.rs b/src/array/boolean/mod.rs index be6cfe358a0..4e959481a28 100644 --- a/src/array/boolean/mod.rs +++ b/src/array/boolean/mod.rs @@ -172,25 +172,8 @@ impl BooleanArray { } impl_sliced!(); - - /// Returns this [`BooleanArray`] with a new validity. - /// # Panic - /// This function panics iff `validity.len() != self.len()`. - #[must_use] - pub fn with_validity(mut self, validity: Option) -> Self { - self.set_validity(validity); - self - } - - /// Sets the validity of this [`BooleanArray`]. - /// # Panics - /// This function panics iff `values.len() != self.len()`. - pub fn set_validity(&mut self, validity: Option) { - if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) { - panic!("validity must be equal to the array's length") - } - self.validity = validity; - } + impl_mut_validity!(); + impl_into_array!(); /// Returns a clone of this [`BooleanArray`] with new values. /// # Panics @@ -346,16 +329,6 @@ impl BooleanArray { Ok(MutableBooleanArray::try_from_trusted_len_iter(iterator)?.into()) } - /// Boxes self into a [`Box`]. - pub fn boxed(self) -> Box { - Box::new(self) - } - - /// Boxes self into a [`std::sync::Arc`]. - pub fn arced(self) -> std::sync::Arc { - std::sync::Arc::new(self) - } - /// Returns its internal representation #[must_use] pub fn into_inner(self) -> (DataType, Bitmap, Option) { @@ -369,45 +342,14 @@ impl BooleanArray { } impl Array for BooleanArray { - #[inline] - fn as_any(&self) -> &dyn std::any::Any { - self - } - - #[inline] - fn as_any_mut(&mut self) -> &mut dyn std::any::Any { - self - } - - #[inline] - fn len(&self) -> usize { - self.len() - } - - #[inline] - fn data_type(&self) -> &DataType { - &self.data_type - } + impl_common_array!(); - #[inline] fn validity(&self) -> Option<&Bitmap> { self.validity.as_ref() } #[inline] - fn slice(&mut self, offset: usize, length: usize) { - self.slice(offset, length) - } - - #[inline] - unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { - self.slice_unchecked(offset, length) - } - fn with_validity(&self, validity: Option) -> Box { Box::new(self.clone().with_validity(validity)) } - fn to_boxed(&self) -> Box { - Box::new(self.clone()) - } } diff --git a/src/array/dictionary/mod.rs b/src/array/dictionary/mod.rs index 1225edb1c3c..bf11567832a 100644 --- a/src/array/dictionary/mod.rs +++ b/src/array/dictionary/mod.rs @@ -323,6 +323,8 @@ impl DictionaryArray { self.keys.set_validity(validity); } + impl_into_array!(); + /// Returns the length of this array #[inline] pub fn len(&self) -> usize { @@ -384,16 +386,6 @@ impl DictionaryArray { new_scalar(self.values.as_ref(), index) } - /// Boxes self into a [`Box`]. - pub fn boxed(self) -> Box { - Box::new(self) - } - - /// Boxes self into a [`std::sync::Arc`]. - pub fn arced(self) -> std::sync::Arc { - std::sync::Arc::new(self) - } - pub(crate) fn try_get_child(data_type: &DataType) -> Result<&DataType, Error> { Ok(match data_type.to_logical_type() { DataType::Dictionary(_, values, _) => values.as_ref(), @@ -407,43 +399,14 @@ impl DictionaryArray { } impl Array for DictionaryArray { - #[inline] - fn as_any(&self) -> &dyn std::any::Any { - self - } - - #[inline] - fn as_any_mut(&mut self) -> &mut dyn std::any::Any { - self - } - - #[inline] - fn len(&self) -> usize { - self.len() - } - - #[inline] - fn data_type(&self) -> &DataType { - &self.data_type - } + impl_common_array!(); fn validity(&self) -> Option<&Bitmap> { self.keys.validity() } - fn slice(&mut self, offset: usize, length: usize) { - self.slice(offset, length) - } - - unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { - self.slice_unchecked(offset, length) - } - + #[inline] fn with_validity(&self, validity: Option) -> Box { Box::new(self.clone().with_validity(validity)) } - - fn to_boxed(&self) -> Box { - Box::new(self.clone()) - } } diff --git a/src/array/fixed_size_binary/mod.rs b/src/array/fixed_size_binary/mod.rs index 57fc4ddec34..5419f30fc54 100644 --- a/src/array/fixed_size_binary/mod.rs +++ b/src/array/fixed_size_binary/mod.rs @@ -83,16 +83,6 @@ impl FixedSizeBinaryArray { Some(Bitmap::new_zeroed(length)), ) } - - /// Boxes self into a [`Box`]. - pub fn boxed(self) -> Box { - Box::new(self) - } - - /// Boxes self into a [`std::sync::Arc`]. - pub fn arced(self) -> std::sync::Arc { - std::sync::Arc::new(self) - } } // must use @@ -125,25 +115,8 @@ impl FixedSizeBinaryArray { } impl_sliced!(); - - /// Returns this [`FixedSizeBinaryArray`] with a new validity. - /// # Panic - /// This function panics iff `validity.len() != self.len()`. - #[must_use] - pub fn with_validity(mut self, validity: Option) -> Self { - self.set_validity(validity); - self - } - - /// Sets the validity of this [`FixedSizeBinaryArray`]. - /// # Panics - /// This function panics iff `validity.len() != self.len()`. - pub fn set_validity(&mut self, validity: Option) { - if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) { - panic!("validity must be equal to the array's length") - } - self.validity = validity; - } + impl_mut_validity!(); + impl_into_array!(); } // accessors @@ -234,45 +207,16 @@ impl FixedSizeBinaryArray { } impl Array for FixedSizeBinaryArray { - #[inline] - fn as_any(&self) -> &dyn std::any::Any { - self - } - - #[inline] - fn as_any_mut(&mut self) -> &mut dyn std::any::Any { - self - } - - #[inline] - fn len(&self) -> usize { - self.len() - } - - #[inline] - fn data_type(&self) -> &DataType { - &self.data_type - } + impl_common_array!(); fn validity(&self) -> Option<&Bitmap> { self.validity.as_ref() } - fn slice(&mut self, offset: usize, length: usize) { - self.slice(offset, length) - } - - unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { - self.slice_unchecked(offset, length) - } - + #[inline] fn with_validity(&self, validity: Option) -> Box { Box::new(self.clone().with_validity(validity)) } - - fn to_boxed(&self) -> Box { - Box::new(self.clone()) - } } impl FixedSizeBinaryArray { diff --git a/src/array/fixed_size_list/mod.rs b/src/array/fixed_size_list/mod.rs index dce8c65179a..e8f6f167039 100644 --- a/src/array/fixed_size_list/mod.rs +++ b/src/array/fixed_size_list/mod.rs @@ -3,7 +3,6 @@ use crate::{ datatypes::{DataType, Field}, error::Error, }; -use std::sync::Arc; use super::{new_empty_array, new_null_array, Array}; @@ -97,16 +96,6 @@ impl FixedSizeListArray { let values = new_null_array(field.data_type().clone(), length * size); Self::new(data_type, values, Some(Bitmap::new_zeroed(length))) } - - /// Boxes self into a [`Box`]. - pub fn boxed(self) -> Box { - Box::new(self) - } - - /// Boxes self into a [`Arc`]. - pub fn arced(self) -> Arc { - Arc::new(self) - } } // must use @@ -139,25 +128,8 @@ impl FixedSizeListArray { } impl_sliced!(); - - /// Returns this [`FixedSizeListArray`] with a new validity. - /// # Panic - /// This function panics iff `validity.len() != self.len()`. - #[must_use] - pub fn with_validity(mut self, validity: Option) -> Self { - self.set_validity(validity); - self - } - - /// Sets the validity of this [`FixedSizeListArray`]. - /// # Panics - /// This function panics iff `validity.len() != self.len()`. - pub fn set_validity(&mut self, validity: Option) { - if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) { - panic!("validity must be equal to the array's length") - } - self.validity = validity; - } + impl_mut_validity!(); + impl_into_array!(); } // accessors @@ -223,42 +195,14 @@ impl FixedSizeListArray { } impl Array for FixedSizeListArray { - #[inline] - fn as_any(&self) -> &dyn std::any::Any { - self - } - - #[inline] - fn as_any_mut(&mut self) -> &mut dyn std::any::Any { - self - } - - #[inline] - fn len(&self) -> usize { - self.len() - } - - #[inline] - fn data_type(&self) -> &DataType { - &self.data_type - } + impl_common_array!(); fn validity(&self) -> Option<&Bitmap> { self.validity.as_ref() } - fn slice(&mut self, offset: usize, length: usize) { - self.slice(offset, length) - } - - unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { - self.slice_unchecked(offset, length) - } - + #[inline] fn with_validity(&self, validity: Option) -> Box { Box::new(self.clone().with_validity(validity)) } - fn to_boxed(&self) -> Box { - Box::new(self.clone()) - } } diff --git a/src/array/list/mod.rs b/src/array/list/mod.rs index 98361ef26d8..fa0ff32c01e 100644 --- a/src/array/list/mod.rs +++ b/src/array/list/mod.rs @@ -4,7 +4,6 @@ use crate::{ error::Error, offset::{Offset, Offsets, OffsetsBuffer}, }; -use std::sync::Arc; use super::{new_empty_array, specification::try_check_offsets_bounds, Array}; @@ -104,16 +103,6 @@ impl ListArray { Some(Bitmap::new_zeroed(length)), ) } - - /// Boxes self into a [`Box`]. - pub fn boxed(self) -> Box { - Box::new(self) - } - - /// Boxes self into a [`Arc`]. - pub fn arced(self) -> Arc { - Arc::new(self) - } } impl ListArray { @@ -140,25 +129,8 @@ impl ListArray { } impl_sliced!(); - - /// Returns this [`ListArray`] with a new validity. - /// # Panic - /// This function panics iff `validity.len() != self.len()`. - #[must_use] - pub fn with_validity(mut self, validity: Option) -> Self { - self.set_validity(validity); - self - } - - /// Sets the validity of this [`ListArray`]. - /// # Panics - /// This function panics iff `validity.len() != self.len()`. - pub fn set_validity(&mut self, validity: Option) { - if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) { - panic!("validity must be equal to the array's length") - } - self.validity = validity; - } + impl_mut_validity!(); + impl_into_array!(); } // Accessors @@ -255,44 +227,14 @@ impl ListArray { } impl Array for ListArray { - #[inline] - fn as_any(&self) -> &dyn std::any::Any { - self - } - - #[inline] - fn as_any_mut(&mut self) -> &mut dyn std::any::Any { - self - } - - #[inline] - fn len(&self) -> usize { - self.len() - } + impl_common_array!(); - #[inline] - fn data_type(&self) -> &DataType { - &self.data_type - } - - #[inline] fn validity(&self) -> Option<&Bitmap> { self.validity.as_ref() } - fn slice(&mut self, offset: usize, length: usize) { - self.slice(offset, length) - } - - unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { - self.slice_unchecked(offset, length) - } - + #[inline] fn with_validity(&self, validity: Option) -> Box { Box::new(self.clone().with_validity(validity)) } - - fn to_boxed(&self) -> Box { - Box::new(self.clone()) - } } diff --git a/src/array/map/mod.rs b/src/array/map/mod.rs index 1176e54e965..434a14233c9 100644 --- a/src/array/map/mod.rs +++ b/src/array/map/mod.rs @@ -104,35 +104,6 @@ impl MapArray { let field = new_empty_array(Self::get_field(&data_type).data_type().clone()); Self::new(data_type, OffsetsBuffer::default(), field, None) } - - /// Returns this [`MapArray`] with a new validity. - /// # Panics - /// This function panics iff `validity.len() != self.len()`. - #[must_use] - pub fn with_validity(mut self, validity: Option) -> Self { - self.set_validity(validity); - self - } - - /// Sets the validity of this [`MapArray`]. - /// # Panics - /// This function panics iff `validity.len() != self.len()`. - pub fn set_validity(&mut self, validity: Option) { - if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) { - panic!("validity's length must be equal to the array's length") - } - self.validity = validity; - } - - /// Boxes self into a [`Box`]. - pub fn boxed(self) -> Box { - Box::new(self) - } - - /// Boxes self into a [`std::sync::Arc`]. - pub fn arced(self) -> std::sync::Arc { - std::sync::Arc::new(self) - } } impl MapArray { @@ -160,6 +131,8 @@ impl MapArray { } impl_sliced!(); + impl_mut_validity!(); + impl_into_array!(); pub(crate) fn try_get_field(data_type: &DataType) -> Result<&Field, Error> { if let DataType::Map(field, _) = data_type.to_logical_type() { @@ -218,44 +191,14 @@ impl MapArray { } impl Array for MapArray { - #[inline] - fn as_any(&self) -> &dyn std::any::Any { - self - } - - #[inline] - fn as_any_mut(&mut self) -> &mut dyn std::any::Any { - self - } - - #[inline] - fn len(&self) -> usize { - self.len() - } + impl_common_array!(); - #[inline] - fn data_type(&self) -> &DataType { - &self.data_type - } - - #[inline] fn validity(&self) -> Option<&Bitmap> { self.validity.as_ref() } - fn slice(&mut self, offset: usize, length: usize) { - self.slice(offset, length) - } - - unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { - self.slice_unchecked(offset, length) - } - + #[inline] fn with_validity(&self, validity: Option) -> Box { Box::new(self.clone().with_validity(validity)) } - - fn to_boxed(&self) -> Box { - Box::new(self.clone()) - } } diff --git a/src/array/mod.rs b/src/array/mod.rs index 4ce3b56e57f..e2eda0c5b97 100644 --- a/src/array/mod.rs +++ b/src/array/mod.rs @@ -396,6 +396,7 @@ macro_rules! clone_dyn { }}; } +// macro implementing `sliced` and `sliced_unchecked` macro_rules! impl_sliced { () => { /// Returns this array sliced. @@ -427,6 +428,87 @@ macro_rules! impl_sliced { }; } +// macro implementing `with_validity` and `set_validity` +macro_rules! impl_mut_validity { + () => { + /// Returns this array with a new validity. + /// # Panic + /// Panics iff `validity.len() != self.len()`. + #[must_use] + #[inline] + pub fn with_validity(mut self, validity: Option) -> Self { + self.set_validity(validity); + self + } + + /// Sets the validity of this array. + /// # Panics + /// This function panics iff `values.len() != self.len()`. + #[inline] + pub fn set_validity(&mut self, validity: Option) { + if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) { + panic!("validity must be equal to the array's length") + } + self.validity = validity; + } + } +} + +// macro implementing `boxed` and `arced` +macro_rules! impl_into_array { + () => { + /// Boxes this array into a [`Box`]. + pub fn boxed(self) -> Box { + Box::new(self) + } + + /// Arcs this array into a [`std::sync::Arc`]. + pub fn arced(self) -> std::sync::Arc { + std::sync::Arc::new(self) + } + }; +} + +// macro implementing common methods of trait `Array` +macro_rules! impl_common_array { + () => { + #[inline] + fn as_any(&self) -> &dyn std::any::Any { + self + } + + #[inline] + fn as_any_mut(&mut self) -> &mut dyn std::any::Any { + self + } + + #[inline] + fn len(&self) -> usize { + self.len() + } + + #[inline] + fn data_type(&self) -> &DataType { + &self.data_type + } + + #[inline] + fn slice(&mut self, offset: usize, length: usize) { + self.slice(offset, length); + } + + #[inline] + unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.slice_unchecked(offset, length); + } + + #[inline] + fn to_boxed(&self) -> Box { + Box::new(self.clone()) + } + }; +} + /// Clones a dynamic [`Array`]. /// # Implementation /// This operation is `O(1)` over `len`, as it amounts to increase two ref counts diff --git a/src/array/null.rs b/src/array/null.rs index 45d956073c5..0cf8e600e46 100644 --- a/src/array/null.rs +++ b/src/array/null.rs @@ -47,20 +47,26 @@ impl NullArray { Self::new(data_type, length) } - /// Boxes self into a [`Box`]. - pub fn boxed(self) -> Box { - Box::new(self) - } - - /// Boxes self into a [`std::sync::Arc`]. - pub fn arced(self) -> std::sync::Arc { - std::sync::Arc::new(self) - } + impl_sliced!(); + impl_into_array!(); } impl NullArray { /// Returns a slice of the [`NullArray`]. - pub fn slice(&mut self, _offset: usize, length: usize) { + /// # Panic + /// This function panics iff `offset + length > self.len()`. + pub fn slice(&mut self, offset: usize, length: usize) { + assert!( + offset + length <= self.len(), + "the offset of the new array cannot exceed the arrays' length" + ); + unsafe { self.slice_unchecked(offset, length) }; + } + + /// Returns a slice of the [`NullArray`]. + /// # Safety + /// The caller must ensure that `offset + length < self.len()`. + pub unsafe fn slice_unchecked(&mut self, _offset: usize, length: usize) { self.length = length; } @@ -71,45 +77,15 @@ impl NullArray { } impl Array for NullArray { - #[inline] - fn as_any(&self) -> &dyn std::any::Any { - self - } - - #[inline] - fn as_any_mut(&mut self) -> &mut dyn std::any::Any { - self - } - - #[inline] - fn len(&self) -> usize { - self.len() - } - - #[inline] - fn data_type(&self) -> &DataType { - &self.data_type - } + impl_common_array!(); fn validity(&self) -> Option<&Bitmap> { None } - fn slice(&mut self, offset: usize, length: usize) { - self.slice(offset, length) - } - - unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { - self.slice(offset, length) - } - fn with_validity(&self, _: Option) -> Box { panic!("cannot set validity of a null array") } - - fn to_boxed(&self) -> Box { - Box::new(self.clone()) - } } impl std::fmt::Debug for NullArray { diff --git a/src/array/primitive/mod.rs b/src/array/primitive/mod.rs index d855b5ef720..34229101df1 100644 --- a/src/array/primitive/mod.rs +++ b/src/array/primitive/mod.rs @@ -184,7 +184,7 @@ impl PrimitiveArray { /// This function panics iff `i >= self.len`. #[inline] pub fn value(&self, i: usize) -> T { - self.values()[i] + self.values[i] } /// Returns the value at index `i`. @@ -223,25 +223,8 @@ impl PrimitiveArray { } impl_sliced!(); - - /// Returns this [`PrimitiveArray`] with a new validity. - /// # Panics - /// This function panics iff `validity.len() != self.len()`. - #[must_use] - pub fn with_validity(mut self, validity: Option) -> Self { - self.set_validity(validity); - self - } - - /// Sets the validity of this [`PrimitiveArray`]. - /// # Panics - /// This function panics iff `validity.len() != self.len()`. - pub fn set_validity(&mut self, validity: Option) { - if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) { - panic!("validity's length must be equal to the array's length") - } - self.validity = validity; - } + impl_mut_validity!(); + impl_into_array!(); /// Returns this [`PrimitiveArray`] with new values. /// # Panics @@ -396,16 +379,6 @@ impl PrimitiveArray { MutablePrimitiveArray::::from_trusted_len_iter_unchecked(iter).into() } - /// Boxes self into a [`Box`]. - pub fn boxed(self) -> Box { - Box::new(self) - } - - /// Boxes self into a [`std::sync::Arc`]. - pub fn arced(self) -> std::sync::Arc { - std::sync::Arc::new(self) - } - /// Alias for `Self::try_new(..).unwrap()`. /// # Panics /// This function errors iff: @@ -417,44 +390,16 @@ impl PrimitiveArray { } impl Array for PrimitiveArray { - #[inline] - fn as_any(&self) -> &dyn std::any::Any { - self - } - - #[inline] - fn as_any_mut(&mut self) -> &mut dyn std::any::Any { - self - } - - #[inline] - fn len(&self) -> usize { - self.values.len() - } - - #[inline] - fn data_type(&self) -> &DataType { - self.data_type() - } + impl_common_array!(); fn validity(&self) -> Option<&Bitmap> { self.validity.as_ref() } - fn slice(&mut self, offset: usize, length: usize) { - self.slice(offset, length); - } - - unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { - self.slice_unchecked(offset, length); - } - + #[inline] fn with_validity(&self, validity: Option) -> Box { Box::new(self.clone().with_validity(validity)) } - fn to_boxed(&self) -> Box { - Box::new(self.clone()) - } } /// A type definition [`PrimitiveArray`] for `i8` diff --git a/src/array/struct_/mod.rs b/src/array/struct_/mod.rs index 1e2de673b32..04548504f2a 100644 --- a/src/array/struct_/mod.rs +++ b/src/array/struct_/mod.rs @@ -195,34 +195,9 @@ impl StructArray { impl_sliced!(); - /// Returns this [`StructArray`] with a new validity. - /// # Panics - /// This function panics iff `validity.len() != self.len()`. - #[must_use] - pub fn with_validity(mut self, validity: Option) -> Self { - self.set_validity(validity); - self - } - - /// Sets the validity of this [`StructArray`]. - /// # Panics - /// This function panics iff `validity.len() != self.len()`. - pub fn set_validity(&mut self, validity: Option) { - if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) { - panic!("validity's length must be equal to the array's length") - } - self.validity = validity; - } + impl_mut_validity!(); - /// Boxes self into a [`Box`]. - pub fn boxed(self) -> Box { - Box::new(self) - } - - /// Boxes self into a [`std::sync::Arc`]. - pub fn arced(self) -> std::sync::Arc { - std::sync::Arc::new(self) - } + impl_into_array!(); } // Accessors @@ -267,44 +242,14 @@ impl StructArray { } impl Array for StructArray { - #[inline] - fn as_any(&self) -> &dyn std::any::Any { - self - } - - #[inline] - fn as_any_mut(&mut self) -> &mut dyn std::any::Any { - self - } + impl_common_array!(); - #[inline] - fn len(&self) -> usize { - self.len() - } - - #[inline] - fn data_type(&self) -> &DataType { - &self.data_type - } - - #[inline] fn validity(&self) -> Option<&Bitmap> { self.validity.as_ref() } - fn slice(&mut self, offset: usize, length: usize) { - self.slice(offset, length) - } - - unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { - self.slice_unchecked(offset, length) - } - + #[inline] fn with_validity(&self, validity: Option) -> Box { Box::new(self.clone().with_validity(validity)) } - - fn to_boxed(&self) -> Box { - Box::new(self.clone()) - } } diff --git a/src/array/union/mod.rs b/src/array/union/mod.rs index 79ce6a3e3e2..c3bf98a3a91 100644 --- a/src/array/union/mod.rs +++ b/src/array/union/mod.rs @@ -219,16 +219,6 @@ impl UnionArray { panic!("Union struct must be created with the corresponding Union DataType") } } - - /// Boxes self into a [`Box`]. - pub fn boxed(self) -> Box { - Box::new(self) - } - - /// Boxes self into a [`std::sync::Arc`]. - pub fn arced(self) -> std::sync::Arc { - std::sync::Arc::new(self) - } } impl UnionArray { @@ -263,6 +253,7 @@ impl UnionArray { } impl_sliced!(); + impl_into_array!(); } impl UnionArray { @@ -344,41 +335,15 @@ impl UnionArray { } impl Array for UnionArray { - fn as_any(&self) -> &dyn std::any::Any { - self - } - - #[inline] - fn as_any_mut(&mut self) -> &mut dyn std::any::Any { - self - } - - fn len(&self) -> usize { - self.len() - } - - fn data_type(&self) -> &DataType { - &self.data_type - } + impl_common_array!(); fn validity(&self) -> Option<&Bitmap> { None } - fn slice(&mut self, offset: usize, length: usize) { - self.slice(offset, length) - } - - unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { - self.slice_unchecked(offset, length) - } - fn with_validity(&self, _: Option) -> Box { panic!("cannot set validity of a union array") } - fn to_boxed(&self) -> Box { - Box::new(self.clone()) - } } impl UnionArray { diff --git a/src/array/utf8/mod.rs b/src/array/utf8/mod.rs index 7e1afdf5a76..51055fe8dd1 100644 --- a/src/array/utf8/mod.rs +++ b/src/array/utf8/mod.rs @@ -222,35 +222,8 @@ impl Utf8Array { } impl_sliced!(); - - /// Boxes self into a [`Box`]. - pub fn boxed(self) -> Box { - Box::new(self) - } - - /// Boxes self into a [`std::sync::Arc`]. - pub fn arced(self) -> std::sync::Arc { - std::sync::Arc::new(self) - } - - /// Returns this [`Utf8Array`] with a new validity. - /// # Panics - /// This function panics iff `validity.len() != self.len()`. - #[must_use] - pub fn with_validity(mut self, validity: Option) -> Self { - self.set_validity(validity); - self - } - - /// Sets the validity of this [`Utf8Array`]. - /// # Panics - /// This function panics iff `validity.len() != self.len()`. - pub fn set_validity(&mut self, validity: Option) { - if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) { - panic!("validity's length must be equal to the array's length") - } - self.validity = validity; - } + impl_mut_validity!(); + impl_into_array!(); /// Try to convert this `Utf8Array` to a `MutableUtf8Array` pub fn into_mut(mut self) -> Either> { @@ -521,45 +494,16 @@ impl Utf8Array { } impl Array for Utf8Array { - #[inline] - fn as_any(&self) -> &dyn std::any::Any { - self - } - - #[inline] - fn as_any_mut(&mut self) -> &mut dyn std::any::Any { - self - } - - #[inline] - fn len(&self) -> usize { - self.len() - } - - #[inline] - fn data_type(&self) -> &DataType { - &self.data_type - } + impl_common_array!(); fn validity(&self) -> Option<&Bitmap> { self.validity.as_ref() } - fn slice(&mut self, offset: usize, length: usize) { - self.slice(offset, length) - } - - unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { - self.slice_unchecked(offset, length) - } - + #[inline] fn with_validity(&self, validity: Option) -> Box { Box::new(self.clone().with_validity(validity)) } - - fn to_boxed(&self) -> Box { - Box::new(self.clone()) - } } unsafe impl GenericBinaryArray for Utf8Array {