facet_core/types/def/
option.rs1use super::Shape;
2use crate::ptr::{PtrConst, PtrMut, PtrUninit};
3
4#[derive(Clone, Copy, Debug)]
7#[repr(C)]
8pub struct OptionDef {
9 pub vtable: &'static OptionVTable,
11
12 pub t: &'static Shape,
14}
15
16impl OptionDef {
17 pub const fn builder() -> OptionDefBuilder {
19 OptionDefBuilder::new()
20 }
21
22 pub const fn t(&self) -> &'static Shape {
24 self.t
25 }
26}
27
28pub struct OptionDefBuilder {
30 vtable: Option<&'static OptionVTable>,
31 t: Option<&'static Shape>,
32}
33
34impl OptionDefBuilder {
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 OptionVTable) -> Self {
46 self.vtable = Some(vtable);
47 self
48 }
49
50 pub const fn t(mut self, t: &'static Shape) -> Self {
52 self.t = Some(t);
53 self
54 }
55
56 pub const fn build(self) -> OptionDef {
58 OptionDef {
59 vtable: self.vtable.unwrap(),
60 t: self.t.unwrap(),
61 }
62 }
63}
64
65pub type OptionIsSomeFn = for<'option> unsafe fn(option: PtrConst<'option>) -> bool;
71
72pub type OptionGetValueFn =
78 for<'option> unsafe fn(option: PtrConst<'option>) -> Option<PtrConst<'option>>;
79
80pub type OptionInitSomeFn =
89 for<'option> unsafe fn(option: PtrUninit<'option>, value: PtrConst<'_>) -> PtrMut<'option>;
90
91pub type OptionInitNoneFn = unsafe fn(option: PtrUninit) -> PtrMut;
98
99pub type OptionReplaceWithFn =
108 for<'option> unsafe fn(option: PtrMut<'option>, value: Option<PtrConst<'_>>);
109
110#[derive(Clone, Copy, Debug)]
112#[repr(C)]
113pub struct OptionVTable {
114 pub is_some_fn: OptionIsSomeFn,
116
117 pub get_value_fn: OptionGetValueFn,
119
120 pub init_some_fn: OptionInitSomeFn,
122
123 pub init_none_fn: OptionInitNoneFn,
125
126 pub replace_with_fn: OptionReplaceWithFn,
128}
129
130impl OptionVTable {
131 pub const fn builder() -> OptionVTableBuilder {
133 OptionVTableBuilder::new()
134 }
135}
136
137pub struct OptionVTableBuilder {
139 is_some_fn: Option<OptionIsSomeFn>,
140 get_value_fn: Option<OptionGetValueFn>,
141 init_some_fn: Option<OptionInitSomeFn>,
142 init_none_fn: Option<OptionInitNoneFn>,
143 replace_with_fn: Option<OptionReplaceWithFn>,
144}
145
146impl OptionVTableBuilder {
147 #[allow(clippy::new_without_default)]
149 pub const fn new() -> Self {
150 Self {
151 is_some_fn: None,
152 get_value_fn: None,
153 init_some_fn: None,
154 init_none_fn: None,
155 replace_with_fn: None,
156 }
157 }
158
159 pub const fn is_some(mut self, f: OptionIsSomeFn) -> Self {
161 self.is_some_fn = Some(f);
162 self
163 }
164
165 pub const fn get_value(mut self, f: OptionGetValueFn) -> Self {
167 self.get_value_fn = Some(f);
168 self
169 }
170
171 pub const fn init_some(mut self, f: OptionInitSomeFn) -> Self {
173 self.init_some_fn = Some(f);
174 self
175 }
176
177 pub const fn init_none(mut self, f: OptionInitNoneFn) -> Self {
179 self.init_none_fn = Some(f);
180 self
181 }
182
183 pub const fn replace_with(mut self, f: OptionReplaceWithFn) -> Self {
185 self.replace_with_fn = Some(f);
186 self
187 }
188
189 pub const fn build(self) -> OptionVTable {
195 OptionVTable {
196 is_some_fn: self.is_some_fn.unwrap(),
197 get_value_fn: self.get_value_fn.unwrap(),
198 init_some_fn: self.init_some_fn.unwrap(),
199 init_none_fn: self.init_none_fn.unwrap(),
200 replace_with_fn: self.replace_with_fn.unwrap(),
201 }
202 }
203}