vortex_array/vtable/
serde.rs

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