1use crate::ptr::{PtrConst, PtrMut, PtrUninit};
2
3use super::Shape;
4
5#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
7#[repr(C)]
8#[non_exhaustive]
9pub struct ListDef {
10 pub vtable: &'static ListVTable,
12 pub t: fn() -> &'static Shape,
14}
15
16impl ListDef {
17 pub const fn builder() -> ListDefBuilder {
19 ListDefBuilder::new()
20 }
21
22 pub fn t(&self) -> &'static Shape {
24 (self.t)()
25 }
26}
27
28pub struct ListDefBuilder {
30 vtable: Option<&'static ListVTable>,
31 t: Option<fn() -> &'static Shape>,
32}
33
34impl ListDefBuilder {
35 #[allow(clippy::new_without_default)]
37 pub const fn new() -> Self {
38 Self {
39 vtable: None,
40 t: None,
41 }
42 }
43
44 pub const fn vtable(mut self, vtable: &'static ListVTable) -> Self {
46 self.vtable = Some(vtable);
47 self
48 }
49
50 pub const fn t(mut self, t: fn() -> &'static Shape) -> Self {
52 self.t = Some(t);
53 self
54 }
55
56 pub const fn build(self) -> ListDef {
58 ListDef {
59 vtable: self.vtable.unwrap(),
60 t: self.t.unwrap(),
61 }
62 }
63}
64
65pub type ListInitInPlaceWithCapacityFn =
72 for<'mem> unsafe fn(list: PtrUninit<'mem>, capacity: usize) -> PtrMut<'mem>;
73
74pub type ListPushFn = unsafe fn(list: PtrMut, item: PtrMut);
82pub type ListLenFn = unsafe fn(list: PtrConst) -> usize;
90
91pub type ListGetItemPtrFn = unsafe fn(list: PtrConst, index: usize) -> PtrConst;
97
98#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
101#[repr(C)]
102#[non_exhaustive]
103pub struct ListVTable {
104 pub init_in_place_with_capacity: Option<ListInitInPlaceWithCapacityFn>,
107
108 pub push: ListPushFn,
110
111 pub len: ListLenFn,
113
114 pub get_item_ptr: ListGetItemPtrFn,
116}
117
118impl ListVTable {
119 pub const fn builder() -> ListVTableBuilder {
121 ListVTableBuilder::new()
122 }
123}
124
125pub struct ListVTableBuilder {
127 init_in_place_with_capacity: Option<ListInitInPlaceWithCapacityFn>,
128 push: Option<ListPushFn>,
129 len: Option<ListLenFn>,
130 get_item_ptr: Option<ListGetItemPtrFn>,
131}
132
133impl ListVTableBuilder {
134 #[allow(clippy::new_without_default)]
136 pub const fn new() -> Self {
137 Self {
138 init_in_place_with_capacity: None,
139 push: None,
140 len: None,
141 get_item_ptr: None,
142 }
143 }
144
145 pub const fn init_in_place_with_capacity(mut self, f: ListInitInPlaceWithCapacityFn) -> Self {
147 self.init_in_place_with_capacity = Some(f);
148 self
149 }
150
151 pub const fn push(mut self, f: ListPushFn) -> Self {
153 self.push = Some(f);
154 self
155 }
156
157 pub const fn len(mut self, f: ListLenFn) -> Self {
159 self.len = Some(f);
160 self
161 }
162
163 pub const fn get_item_ptr(mut self, f: ListGetItemPtrFn) -> Self {
165 self.get_item_ptr = Some(f);
166 self
167 }
168
169 pub const fn build(self) -> ListVTable {
175 ListVTable {
176 init_in_place_with_capacity: self.init_in_place_with_capacity,
177 push: self.push.unwrap(),
178 len: self.len.unwrap(),
179 get_item_ptr: self.get_item_ptr.unwrap(),
180 }
181 }
182}