reifydb_core/value/column/data/
slice.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4use reifydb_type::{Date, DateTime, Decimal, Duration, Int, Time, Uint, Value};
5
6use crate::value::column::ColumnData;
7
8pub trait AsSlice<T> {
9	fn as_slice(&self) -> &[T];
10}
11
12impl ColumnData {
13	pub fn as_slice<T>(&self) -> &[T]
14	where
15		Self: AsSlice<T>,
16	{
17		<Self as AsSlice<T>>::as_slice(self)
18	}
19}
20
21impl AsSlice<bool> for ColumnData {
22	fn as_slice(&self) -> &[bool] {
23		match self {
24			ColumnData::Bool(_) => {
25				panic!("as_slice() is not supported for BitVec. Use to_vec() instead.")
26			}
27			other => {
28				panic!("called `as_slice::<bool>()` on ColumnData::{:?}", other.get_type())
29			}
30		}
31	}
32}
33
34impl AsSlice<f32> for ColumnData {
35	fn as_slice(&self) -> &[f32] {
36		match self {
37			ColumnData::Float4(container) => container.data().as_slice(),
38			other => {
39				panic!("called `as_slice::<f32>()` on ColumnData::{:?}", other.get_type())
40			}
41		}
42	}
43}
44
45impl AsSlice<f64> for ColumnData {
46	fn as_slice(&self) -> &[f64] {
47		match self {
48			ColumnData::Float8(container) => container.data().as_slice(),
49			other => {
50				panic!("called `as_slice::<f64>()` on ColumnData::{:?}", other.get_type())
51			}
52		}
53	}
54}
55
56impl AsSlice<i8> for ColumnData {
57	fn as_slice(&self) -> &[i8] {
58		match self {
59			ColumnData::Int1(container) => container.data().as_slice(),
60			other => {
61				panic!("called `as_slice::<i8>()` on ColumnData::{:?}", other.get_type())
62			}
63		}
64	}
65}
66
67impl AsSlice<i16> for ColumnData {
68	fn as_slice(&self) -> &[i16] {
69		match self {
70			ColumnData::Int2(container) => container.data().as_slice(),
71			other => {
72				panic!("called `as_slice::<i16>()` on ColumnData::{:?}", other.get_type())
73			}
74		}
75	}
76}
77
78impl AsSlice<i32> for ColumnData {
79	fn as_slice(&self) -> &[i32] {
80		match self {
81			ColumnData::Int4(container) => container.data().as_slice(),
82			other => {
83				panic!("called `as_slice::<i32>()` on ColumnData::{:?}", other.get_type())
84			}
85		}
86	}
87}
88
89impl AsSlice<i64> for ColumnData {
90	fn as_slice(&self) -> &[i64] {
91		match self {
92			ColumnData::Int8(container) => container.data().as_slice(),
93			other => {
94				panic!("called `as_slice::<i64>()` on ColumnData::{:?}", other.get_type())
95			}
96		}
97	}
98}
99
100impl AsSlice<i128> for ColumnData {
101	fn as_slice(&self) -> &[i128] {
102		match self {
103			ColumnData::Int16(container) => container.data().as_slice(),
104			other => {
105				panic!("called `as_slice::<i128>()` on ColumnData::{:?}", other.get_type())
106			}
107		}
108	}
109}
110
111impl AsSlice<u8> for ColumnData {
112	fn as_slice(&self) -> &[u8] {
113		match self {
114			ColumnData::Uint1(container) => container.data().as_slice(),
115			other => {
116				panic!("called `as_slice::<u8>()` on ColumnData::{:?}", other.get_type())
117			}
118		}
119	}
120}
121
122impl AsSlice<u16> for ColumnData {
123	fn as_slice(&self) -> &[u16] {
124		match self {
125			ColumnData::Uint2(container) => container.data().as_slice(),
126			other => {
127				panic!("called `as_slice::<u16>()` on ColumnData::{:?}", other.get_type())
128			}
129		}
130	}
131}
132
133impl AsSlice<u32> for ColumnData {
134	fn as_slice(&self) -> &[u32] {
135		match self {
136			ColumnData::Uint4(container) => container.data().as_slice(),
137			other => {
138				panic!("called `as_slice::<u32>()` on ColumnData::{:?}", other.get_type())
139			}
140		}
141	}
142}
143
144impl AsSlice<u64> for ColumnData {
145	fn as_slice(&self) -> &[u64] {
146		match self {
147			ColumnData::Uint8(container) => container.data().as_slice(),
148			other => {
149				panic!("called `as_slice::<u64>()` on ColumnData::{:?}", other.get_type())
150			}
151		}
152	}
153}
154
155impl AsSlice<u128> for ColumnData {
156	fn as_slice(&self) -> &[u128] {
157		match self {
158			ColumnData::Uint16(container) => container.data().as_slice(),
159			other => {
160				panic!("called `as_slice::<u128>()` on ColumnData::{:?}", other.get_type())
161			}
162		}
163	}
164}
165
166impl AsSlice<String> for ColumnData {
167	fn as_slice(&self) -> &[String] {
168		match self {
169			ColumnData::Utf8 {
170				container,
171				..
172			} => container.data().as_slice(),
173			other => {
174				panic!("called `as_slice::<String>()` on ColumnData::{:?}", other.get_type())
175			}
176		}
177	}
178}
179
180impl AsSlice<Date> for ColumnData {
181	fn as_slice(&self) -> &[Date] {
182		match self {
183			ColumnData::Date(container) => container.data().as_slice(),
184			other => {
185				panic!("called `as_slice::<Date>()` on ColumnData::{:?}", other.get_type())
186			}
187		}
188	}
189}
190
191impl AsSlice<DateTime> for ColumnData {
192	fn as_slice(&self) -> &[DateTime] {
193		match self {
194			ColumnData::DateTime(container) => container.data().as_slice(),
195			other => {
196				panic!("called `as_slice::<DateTime>()` on ColumnData::{:?}", other.get_type())
197			}
198		}
199	}
200}
201
202impl AsSlice<Time> for ColumnData {
203	fn as_slice(&self) -> &[Time] {
204		match self {
205			ColumnData::Time(container) => container.data().as_slice(),
206			other => {
207				panic!("called `as_slice::<Time>()` on ColumnData::{:?}", other.get_type())
208			}
209		}
210	}
211}
212
213impl AsSlice<Duration> for ColumnData {
214	fn as_slice(&self) -> &[Duration] {
215		match self {
216			ColumnData::Duration(container) => container.data().as_slice(),
217			other => {
218				panic!("called `as_slice::<Duration>()` on ColumnData::{:?}", other.get_type())
219			}
220		}
221	}
222}
223
224impl AsSlice<Int> for ColumnData {
225	fn as_slice(&self) -> &[Int] {
226		match self {
227			ColumnData::Int {
228				container,
229				..
230			} => container.data().as_slice(),
231			other => {
232				panic!("called `as_slice::<Int>()` on ColumnData::{:?}", other.get_type())
233			}
234		}
235	}
236}
237
238impl AsSlice<Uint> for ColumnData {
239	fn as_slice(&self) -> &[Uint] {
240		match self {
241			ColumnData::Uint {
242				container,
243				..
244			} => container.data().as_slice(),
245			other => {
246				panic!("called `as_slice::<Uint>()` on ColumnData::{:?}", other.get_type())
247			}
248		}
249	}
250}
251
252impl AsSlice<Decimal> for ColumnData {
253	fn as_slice(&self) -> &[Decimal] {
254		match self {
255			ColumnData::Decimal {
256				container,
257				..
258			} => container.data().as_slice(),
259			other => {
260				panic!("called `as_slice::<Decimal>()` on ColumnData::{:?}", other.get_type())
261			}
262		}
263	}
264}
265
266impl AsSlice<Box<Value>> for ColumnData {
267	fn as_slice(&self) -> &[Box<Value>] {
268		match self {
269			ColumnData::Any(container) => container.data().as_slice(),
270			other => {
271				panic!("called `as_slice::<Box<Value>>()` on ColumnData::{:?}", other.get_type())
272			}
273		}
274	}
275}