vortex_array/array/
view.rs1use std::fmt::Debug;
5use std::fmt::Formatter;
6use std::ops::Deref;
7
8use vortex_error::VortexResult;
9
10use crate::ArrayRef;
11use crate::array::Array;
12use crate::array::ArrayId;
13use crate::array::VTable;
14use crate::dtype::DType;
15use crate::stats::StatsSetRef;
16use crate::validity::Validity;
17
18pub struct ArrayView<'a, V: VTable> {
24 array: &'a ArrayRef,
25 data: &'a V::TypedArrayData,
26}
27
28impl<V: VTable> Copy for ArrayView<'_, V> {}
29
30impl<V: VTable> Clone for ArrayView<'_, V> {
31 fn clone(&self) -> Self {
32 *self
33 }
34}
35
36impl<'a, V: VTable> ArrayView<'a, V> {
37 pub(crate) unsafe fn new_unchecked(array: &'a ArrayRef, data: &'a V::TypedArrayData) -> Self {
40 debug_assert!(array.is::<V>());
41 Self { array, data }
42 }
43
44 pub fn array(&self) -> &'a ArrayRef {
46 self.array
47 }
48
49 pub fn data(&self) -> &'a V::TypedArrayData {
51 self.data
52 }
53
54 #[allow(clippy::same_name_method)]
56 pub fn slots(&self) -> &'a [Option<ArrayRef>] {
57 self.array.slots()
58 }
59
60 #[allow(clippy::same_name_method)]
62 pub fn dtype(&self) -> &DType {
63 self.array.dtype()
64 }
65
66 #[allow(clippy::same_name_method)]
68 pub fn len(&self) -> usize {
69 self.array.len()
70 }
71
72 #[allow(clippy::same_name_method)]
74 pub fn is_empty(&self) -> bool {
75 self.array.len() == 0
76 }
77
78 pub fn encoding_id(&self) -> ArrayId {
80 self.array.encoding_id()
81 }
82
83 pub fn statistics(&self) -> StatsSetRef<'_> {
85 self.array.statistics()
86 }
87
88 pub fn validity(&self) -> VortexResult<Validity> {
90 self.array.validity()
91 }
92
93 pub fn into_owned(self) -> Array<V> {
95 unsafe { Array::<V>::from_array_ref_unchecked(self.array.clone()) }
97 }
98}
99
100impl<V: VTable> AsRef<ArrayRef> for ArrayView<'_, V> {
101 fn as_ref(&self) -> &ArrayRef {
102 self.array
103 }
104}
105
106impl<V: VTable> Deref for ArrayView<'_, V> {
107 type Target = V::TypedArrayData;
108
109 fn deref(&self) -> &V::TypedArrayData {
110 self.data
111 }
112}
113
114impl<V: VTable> Debug for ArrayView<'_, V> {
115 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
116 f.debug_struct("ArrayView")
117 .field("encoding", &self.array.encoding_id())
118 .field("dtype", self.array.dtype())
119 .field("len", &self.array.len())
120 .finish()
121 }
122}