Skip to content

Commit

Permalink
Accessor lifetime (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
gatesn authored Apr 3, 2024
1 parent 1aed3c5 commit 74ed32d
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 88 deletions.
4 changes: 2 additions & 2 deletions vortex-array/src/accessor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::array::Array;

pub trait ArrayAccessor<T>: Array {
fn value(&self, index: usize) -> Option<T>;
pub trait ArrayAccessor<'a, T>: Array {
fn value(&'a self, index: usize) -> Option<T>;
}
2 changes: 1 addition & 1 deletion vortex-array/src/array/chunked/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
71 changes: 0 additions & 71 deletions vortex-array/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self>) -> Arc<dyn Any + Send + Sync> {
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<Validity> {
T::validity(self)
}

fn slice(&self, start: usize, stop: usize) -> VortexResult<ArrayRef> {
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());
Expand Down
4 changes: 2 additions & 2 deletions vortex-array/src/array/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl Array for PrimitiveArray {
}
}

impl<T: NativePType> ArrayAccessor<T> for PrimitiveArray {
impl<T: NativePType> ArrayAccessor<'_, T> for PrimitiveArray {
fn value(&self, index: usize) -> Option<T> {
if self.is_valid(index) {
Some(self.typed_data::<T>()[index])
Expand All @@ -229,7 +229,7 @@ impl<T: NativePType> ArrayAccessor<T> for PrimitiveArray {

impl PrimitiveArray {
pub fn iter<T: NativePType>(&self) -> ArrayIter<PrimitiveArray, T> {
ArrayIter::new(self.clone())
ArrayIter::new(self)
}
}

Expand Down
2 changes: 1 addition & 1 deletion vortex-array/src/array/validity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down
6 changes: 3 additions & 3 deletions vortex-array/src/array/varbin/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -31,7 +31,7 @@ impl<'a> ArrayAccessor<&'a [u8]> for &'a VarBinArray {
}
}

impl<'a> ArrayAccessor<Vec<u8>> for &'a VarBinArray {
impl ArrayAccessor<'_, Vec<u8>> for VarBinArray {
fn value(&self, index: usize) -> Option<Vec<u8>> {
if self.is_valid(index) {
let start = offset_at(self.offsets(), index);
Expand Down
2 changes: 1 addition & 1 deletion vortex-array/src/array/varbin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
Expand Down
14 changes: 7 additions & 7 deletions vortex-array/src/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use std::marker::PhantomData;

use crate::accessor::ArrayAccessor;

pub struct ArrayIter<A: ArrayAccessor<T>, T> {
array: A,
pub struct ArrayIter<'a, A: ArrayAccessor<'a, T>, T> {
array: &'a A,
current: usize,
end: usize,
phantom: PhantomData<T>,
}

impl<A: ArrayAccessor<T>, T> ArrayIter<A, T> {
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,
Expand All @@ -21,7 +21,7 @@ impl<A: ArrayAccessor<T>, T> ArrayIter<A, T> {
}
}

impl<A: ArrayAccessor<T>, T> Iterator for ArrayIter<A, T> {
impl<'a, A: ArrayAccessor<'a, T>, T> Iterator for ArrayIter<'a, A, T> {
type Item = Option<T>;

#[inline]
Expand All @@ -43,7 +43,7 @@ impl<A: ArrayAccessor<T>, T> Iterator for ArrayIter<A, T> {
}
}

impl<A: ArrayAccessor<T>, T> DoubleEndedIterator for ArrayIter<A, T> {
impl<'a, A: ArrayAccessor<'a, T>, T> DoubleEndedIterator for ArrayIter<'a, A, T> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.end == self.current {
None
Expand All @@ -54,4 +54,4 @@ impl<A: ArrayAccessor<T>, T> DoubleEndedIterator for ArrayIter<A, T> {
}
}

impl<A: ArrayAccessor<T>, T> ExactSizeIterator for ArrayIter<A, T> {}
impl<'a, A: ArrayAccessor<'a, T>, T> ExactSizeIterator for ArrayIter<'a, A, T> {}

0 comments on commit 74ed32d

Please sign in to comment.