vortex_array/vtable/
mod.rs1use std::fmt::{Debug, Display, Formatter};
4use std::hash::{Hash, Hasher};
5
6mod compute;
7
8pub use compute::*;
9use vortex_dtype::DType;
10use vortex_error::{VortexExpect, VortexResult, vortex_bail};
11
12use crate::arcref::ArcRef;
13use crate::encoding::EncodingId;
14use crate::serde::ArrayParts;
15use crate::{Array, ArrayContext, ArrayRef, Canonical};
16
17pub type VTableRef = ArcRef<dyn EncodingVTable>;
19
20pub trait EncodingVTable: 'static + Sync + Send + ComputeVTable {
27 fn id(&self) -> EncodingId;
29
30 fn decode(
31 &self,
32 parts: &ArrayParts,
33 ctx: &ArrayContext,
34 _dtype: DType,
35 _len: usize,
36 ) -> VortexResult<ArrayRef> {
37 vortex_bail!(
38 "Decoding not supported for encoding {}",
39 ctx.lookup_encoding(parts.encoding_id())
40 .vortex_expect("Encoding already validated")
41 .id()
42 )
43 }
44
45 fn encode(
49 &self,
50 input: &Canonical,
51 _like: Option<&dyn Array>,
52 ) -> VortexResult<Option<ArrayRef>> {
53 if self.id() == input.as_ref().encoding() {
54 return Ok(Some(input.as_ref().to_array()));
55 }
56
57 Ok(None)
58 }
59}
60
61impl PartialEq for dyn EncodingVTable + '_ {
62 fn eq(&self, other: &Self) -> bool {
63 self.id() == other.id()
64 }
65}
66
67impl Eq for dyn EncodingVTable + '_ {}
68
69impl Hash for dyn EncodingVTable + '_ {
70 fn hash<H: Hasher>(&self, state: &mut H) {
71 self.id().hash(state)
72 }
73}
74
75impl Debug for dyn EncodingVTable + '_ {
76 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
77 write!(f, "{}", self.id())
78 }
79}
80
81impl Display for dyn EncodingVTable + '_ {
82 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
83 write!(f, "{}", self.id())
84 }
85}