vortex_array/vtable/
serde.rs

1use std::fmt::Debug;
2
3use vortex_buffer::ByteBuffer;
4use vortex_dtype::DType;
5use vortex_error::{VortexResult, vortex_bail};
6
7use crate::serde::ArrayChildren;
8use crate::vtable::{NotSupported, VTable};
9use crate::{DeserializeMetadata, EmptyMetadata, SerializeMetadata};
10
11/// VTable for assisting with the serialization and deserialiation of arrays.
12///
13/// It is required to implement this vtable in order to support:
14///  * Serialization to disk or over IPC.
15///  * Import/export over FFI.
16pub trait SerdeVTable<V: VTable> {
17    type Metadata: Debug + SerializeMetadata + DeserializeMetadata;
18
19    /// Exports the metadata for the array.
20    ///
21    /// All other parts of the array are exported using the [`crate::vtable::VisitorVTable`].
22    ///
23    /// * If the array does not require serialized metadata, it should return
24    ///   [`crate::metadata::EmptyMetadata`].
25    /// * If the array does not support serialization, it should return `None`.
26    fn metadata(array: &V::Array) -> VortexResult<Option<Self::Metadata>>;
27
28    /// Build an array from its given parts.
29    fn build(
30        encoding: &V::Encoding,
31        dtype: &DType,
32        len: usize,
33        metadata: &<Self::Metadata as DeserializeMetadata>::Output,
34        buffers: &[ByteBuffer],
35        children: &dyn ArrayChildren,
36    ) -> VortexResult<V::Array>;
37}
38
39impl<V: VTable> SerdeVTable<V> for NotSupported {
40    type Metadata = EmptyMetadata;
41
42    fn metadata(_array: &V::Array) -> VortexResult<Option<Self::Metadata>> {
43        Ok(None)
44    }
45
46    fn build(
47        encoding: &V::Encoding,
48        _dtype: &DType,
49        _len: usize,
50        _metadata: &Self::Metadata,
51        _buffers: &[ByteBuffer],
52        _children: &dyn ArrayChildren,
53    ) -> VortexResult<V::Array> {
54        vortex_bail!("Serde not supported by {} encoding", V::id(encoding));
55    }
56}