1use std::fmt::Debug;
5use std::ops::Deref;
6use std::sync::Arc;
7
8use vortex_array::{ArrayContext, DeserializeMetadata, SerializeMetadata};
9use vortex_dtype::DType;
10use vortex_error::VortexResult;
11
12use crate::children::LayoutChildren;
13use crate::segments::{SegmentId, SegmentSource};
14use crate::{
15 IntoLayout, Layout, LayoutChildType, LayoutEncoding, LayoutEncodingRef, LayoutId,
16 LayoutReaderRef, LayoutRef,
17};
18
19pub trait VTable: 'static + Sized + Send + Sync + Debug {
20 type Layout: 'static + Send + Sync + Clone + Debug + Deref<Target = dyn Layout> + IntoLayout;
21 type Encoding: 'static + Send + Sync + Deref<Target = dyn LayoutEncoding>;
22 type Metadata: SerializeMetadata + DeserializeMetadata + Debug;
23
24 fn id(encoding: &Self::Encoding) -> LayoutId;
26
27 fn encoding(layout: &Self::Layout) -> LayoutEncodingRef;
29
30 fn row_count(layout: &Self::Layout) -> u64;
32
33 fn dtype(layout: &Self::Layout) -> &DType;
35
36 fn metadata(layout: &Self::Layout) -> Self::Metadata;
38
39 fn segment_ids(layout: &Self::Layout) -> Vec<SegmentId>;
41
42 fn nchildren(layout: &Self::Layout) -> usize;
44
45 fn child(layout: &Self::Layout, idx: usize) -> VortexResult<LayoutRef>;
47
48 fn child_type(layout: &Self::Layout, idx: usize) -> LayoutChildType;
50
51 fn new_reader(
53 layout: &Self::Layout,
54 name: Arc<str>,
55 segment_source: Arc<dyn SegmentSource>,
56 ) -> VortexResult<LayoutReaderRef>;
57
58 fn build(
60 encoding: &Self::Encoding,
61 dtype: &DType,
62 row_count: u64,
63 metadata: &<Self::Metadata as DeserializeMetadata>::Output,
64 segment_ids: Vec<SegmentId>,
65 children: &dyn LayoutChildren,
66 ctx: ArrayContext,
67 ) -> VortexResult<Self::Layout>;
68}
69
70#[macro_export]
71macro_rules! vtable {
72 ($V:ident) => {
73 $crate::aliases::paste::paste! {
74 #[derive(Debug)]
75 pub struct [<$V VTable>];
76
77 impl AsRef<dyn $crate::Layout> for [<$V Layout>] {
78 fn as_ref(&self) -> &dyn $crate::Layout {
79 unsafe { &*(self as *const [<$V Layout>] as *const $crate::LayoutAdapter<[<$V VTable>]>) }
83 }
84 }
85
86 impl std::ops::Deref for [<$V Layout>] {
87 type Target = dyn $crate::Layout;
88
89 fn deref(&self) -> &Self::Target {
90 unsafe { &*(self as *const [<$V Layout>] as *const $crate::LayoutAdapter<[<$V VTable>]>) }
94 }
95 }
96
97 impl $crate::IntoLayout for [<$V Layout>] {
98 fn into_layout(self) -> $crate::LayoutRef {
99 std::sync::Arc::new(unsafe { std::mem::transmute::<[<$V Layout>], $crate::LayoutAdapter::<[<$V VTable>]>>(self) })
103 }
104 }
105
106 impl From<[<$V Layout>]> for $crate::LayoutRef {
107 fn from(value: [<$V Layout>]) -> $crate::LayoutRef {
108 use $crate::IntoLayout;
109 value.into_layout()
110 }
111 }
112
113 impl AsRef<dyn $crate::LayoutEncoding> for [<$V LayoutEncoding>] {
114 fn as_ref(&self) -> &dyn $crate::LayoutEncoding {
115 unsafe { &*(self as *const [<$V LayoutEncoding>] as *const $crate::LayoutEncodingAdapter<[<$V VTable>]>) }
119 }
120 }
121
122 impl std::ops::Deref for [<$V LayoutEncoding>] {
123 type Target = dyn $crate::LayoutEncoding;
124
125 fn deref(&self) -> &Self::Target {
126 unsafe { &*(self as *const [<$V LayoutEncoding>] as *const $crate::LayoutEncodingAdapter<[<$V VTable>]>) }
130 }
131 }
132 }
133 };
134}