From 74ed32db6ef5ac7e9d28b975daf6e789b0e14e09 Mon Sep 17 00:00:00 2001 From: Nicholas Gates Date: Wed, 3 Apr 2024 15:29:38 +0100 Subject: [PATCH] Accessor lifetime (#186) --- vortex-array/src/accessor.rs | 4 +- vortex-array/src/array/chunked/mod.rs | 2 +- vortex-array/src/array/mod.rs | 71 ----------------------- vortex-array/src/array/primitive/mod.rs | 4 +- vortex-array/src/array/validity/mod.rs | 2 +- vortex-array/src/array/varbin/accessor.rs | 6 +- vortex-array/src/array/varbin/mod.rs | 2 +- vortex-array/src/iterator.rs | 14 ++--- 8 files changed, 17 insertions(+), 88 deletions(-) diff --git a/vortex-array/src/accessor.rs b/vortex-array/src/accessor.rs index ec36393bce..46a51a46d3 100644 --- a/vortex-array/src/accessor.rs +++ b/vortex-array/src/accessor.rs @@ -1,5 +1,5 @@ use crate::array::Array; -pub trait ArrayAccessor: Array { - fn value(&self, index: usize) -> Option; +pub trait ArrayAccessor<'a, T>: Array { + fn value(&'a self, index: usize) -> Option; } diff --git a/vortex-array/src/array/chunked/mod.rs b/vortex-array/src/array/chunked/mod.rs index 1921137ebb..513912f908 100644 --- a/vortex-array/src/array/chunked/mod.rs +++ b/vortex-array/src/array/chunked/mod.rs @@ -159,7 +159,7 @@ impl Array for ChunkedArray { fn walk(&self, walker: &mut dyn ArrayWalker) -> VortexResult<()> { for chunk in self.chunks() { - walker.visit_child(&chunk)?; + walker.visit_child(chunk)?; } Ok(()) } diff --git a/vortex-array/src/array/mod.rs b/vortex-array/src/array/mod.rs index 1f45d74e58..dedbed59ba 100644 --- a/vortex-array/src/array/mod.rs +++ b/vortex-array/src/array/mod.rs @@ -243,77 +243,6 @@ impl ArrayDisplay for ArrayRef { } } -impl<'a, T: Array + Clone> Array for &'a T { - fn as_any(&self) -> &dyn Any { - T::as_any(self) - } - - fn into_any(self: Arc) -> Arc { - T::into_any(Arc::new((*self).clone())) - } - - fn to_array(&self) -> ArrayRef { - T::to_array(self) - } - - fn into_array(self) -> ArrayRef { - self.to_array() - } - - fn len(&self) -> usize { - T::len(self) - } - - fn is_empty(&self) -> bool { - T::is_empty(self) - } - - fn dtype(&self) -> &DType { - T::dtype(self) - } - - fn stats(&self) -> Stats { - T::stats(self) - } - - fn validity(&self) -> Option { - T::validity(self) - } - - fn slice(&self, start: usize, stop: usize) -> VortexResult { - T::slice(self, start, stop) - } - - fn encoding(&self) -> EncodingRef { - T::encoding(self) - } - - fn nbytes(&self) -> usize { - T::nbytes(self) - } - - fn serde(&self) -> Option<&dyn ArraySerde> { - T::serde(self) - } - - fn with_compute_mut( - &self, - f: &mut dyn FnMut(&dyn ArrayCompute) -> VortexResult<()>, - ) -> VortexResult<()> { - T::with_compute_mut(self, f) - } - - fn walk(&self, walker: &mut dyn ArrayWalker) -> VortexResult<()> { - T::walk(self, walker) - } -} - -impl<'a, T: ArrayDisplay> ArrayDisplay for &'a T { - fn fmt(&self, fmt: &'_ mut ArrayFormatter) -> std::fmt::Result { - ArrayDisplay::fmt(*self, fmt) - } -} - pub fn check_slice_bounds(array: &dyn Array, start: usize, stop: usize) -> VortexResult<()> { if start > array.len() { vortex_bail!(OutOfBounds: start, 0, array.len()); diff --git a/vortex-array/src/array/primitive/mod.rs b/vortex-array/src/array/primitive/mod.rs index fcc44d18f1..3bfb085c41 100644 --- a/vortex-array/src/array/primitive/mod.rs +++ b/vortex-array/src/array/primitive/mod.rs @@ -217,7 +217,7 @@ impl Array for PrimitiveArray { } } -impl ArrayAccessor for PrimitiveArray { +impl ArrayAccessor<'_, T> for PrimitiveArray { fn value(&self, index: usize) -> Option { if self.is_valid(index) { Some(self.typed_data::()[index]) @@ -229,7 +229,7 @@ impl ArrayAccessor for PrimitiveArray { impl PrimitiveArray { pub fn iter(&self) -> ArrayIter { - ArrayIter::new(self.clone()) + ArrayIter::new(self) } } diff --git a/vortex-array/src/array/validity/mod.rs b/vortex-array/src/array/validity/mod.rs index ce57e55b07..3ca18a8860 100644 --- a/vortex-array/src/array/validity/mod.rs +++ b/vortex-array/src/array/validity/mod.rs @@ -104,7 +104,7 @@ impl Validity { match self { Validity::Valid(_) => true, Validity::Invalid(_) => false, - Validity::Array(a) => scalar_at(&a, idx).and_then(|s| s.try_into()).unwrap(), + Validity::Array(a) => scalar_at(a, idx).and_then(|s| s.try_into()).unwrap(), } } diff --git a/vortex-array/src/array/varbin/accessor.rs b/vortex-array/src/array/varbin/accessor.rs index bf2ef5faaf..131b228b88 100644 --- a/vortex-array/src/array/varbin/accessor.rs +++ b/vortex-array/src/array/varbin/accessor.rs @@ -19,8 +19,8 @@ fn offset_at(array: &dyn Array, index: usize) -> usize { } } -impl<'a> ArrayAccessor<&'a [u8]> for &'a VarBinArray { - fn value(&self, index: usize) -> Option<&'a [u8]> { +impl<'a> ArrayAccessor<'a, &'a [u8]> for VarBinArray { + fn value(&'a self, index: usize) -> Option<&'a [u8]> { if self.is_valid(index) { let start = offset_at(self.offsets(), index); let end = offset_at(self.offsets(), index + 1); @@ -31,7 +31,7 @@ impl<'a> ArrayAccessor<&'a [u8]> for &'a VarBinArray { } } -impl<'a> ArrayAccessor> for &'a VarBinArray { +impl ArrayAccessor<'_, Vec> for VarBinArray { fn value(&self, index: usize) -> Option> { if self.is_valid(index) { let start = offset_at(self.offsets(), index); diff --git a/vortex-array/src/array/varbin/mod.rs b/vortex-array/src/array/varbin/mod.rs index caf35a3ca8..20a7640167 100644 --- a/vortex-array/src/array/varbin/mod.rs +++ b/vortex-array/src/array/varbin/mod.rs @@ -193,7 +193,7 @@ impl VarBinArray { } } -pub type VarBinIter<'a, T> = ArrayIter<&'a VarBinArray, T>; +pub type VarBinIter<'a, T> = ArrayIter<'a, VarBinArray, T>; impl Array for VarBinArray { impl_array!(); diff --git a/vortex-array/src/iterator.rs b/vortex-array/src/iterator.rs index 11d888ba26..40a85e1f03 100644 --- a/vortex-array/src/iterator.rs +++ b/vortex-array/src/iterator.rs @@ -2,15 +2,15 @@ use std::marker::PhantomData; use crate::accessor::ArrayAccessor; -pub struct ArrayIter, T> { - array: A, +pub struct ArrayIter<'a, A: ArrayAccessor<'a, T>, T> { + array: &'a A, current: usize, end: usize, phantom: PhantomData, } -impl, T> ArrayIter { - pub fn new(array: A) -> Self { +impl<'a, A: ArrayAccessor<'a, T>, T> ArrayIter<'a, A, T> { + pub fn new(array: &'a A) -> Self { let len = array.len(); ArrayIter { array, @@ -21,7 +21,7 @@ impl, T> ArrayIter { } } -impl, T> Iterator for ArrayIter { +impl<'a, A: ArrayAccessor<'a, T>, T> Iterator for ArrayIter<'a, A, T> { type Item = Option; #[inline] @@ -43,7 +43,7 @@ impl, T> Iterator for ArrayIter { } } -impl, T> DoubleEndedIterator for ArrayIter { +impl<'a, A: ArrayAccessor<'a, T>, T> DoubleEndedIterator for ArrayIter<'a, A, T> { fn next_back(&mut self) -> Option { if self.end == self.current { None @@ -54,4 +54,4 @@ impl, T> DoubleEndedIterator for ArrayIter { } } -impl, T> ExactSizeIterator for ArrayIter {} +impl<'a, A: ArrayAccessor<'a, T>, T> ExactSizeIterator for ArrayIter<'a, A, T> {}