facet_core/types/def/
slice.rs1use crate::{PtrMut, ptr::PtrConst};
2
3use super::Shape;
4
5#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
7#[repr(C)]
8#[non_exhaustive]
9pub struct SliceDef<'shape> {
10 pub vtable: &'shape SliceVTable,
12 pub t: &'shape Shape<'shape>,
14}
15
16impl<'shape> SliceDef<'shape> {
17 pub const fn builder() -> SliceDefBuilder<'shape> {
19 SliceDefBuilder::new()
20 }
21
22 pub const fn t(&self) -> &'shape Shape<'shape> {
24 self.t
25 }
26}
27
28pub struct SliceDefBuilder<'shape> {
30 vtable: Option<&'shape SliceVTable>,
31 t: Option<&'shape Shape<'shape>>,
32}
33
34impl<'shape> SliceDefBuilder<'shape> {
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: &'shape SliceVTable) -> Self {
46 self.vtable = Some(vtable);
47 self
48 }
49
50 pub const fn t(mut self, t: &'shape Shape<'shape>) -> Self {
52 self.t = Some(t);
53 self
54 }
55
56 pub const fn build(self) -> SliceDef<'shape> {
58 SliceDef {
59 vtable: self.vtable.unwrap(),
60 t: self.t.unwrap(),
61 }
62 }
63}
64
65pub type SliceLenFn = unsafe fn(slice: PtrConst) -> usize;
71
72pub type SliceAsPtrFn = unsafe fn(slice: PtrConst) -> PtrConst;
78
79pub type SliceAsMutPtrFn = unsafe fn(slice: PtrMut) -> PtrMut;
85
86#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
89#[repr(C)]
90#[non_exhaustive]
91pub struct SliceVTable {
92 pub len: SliceLenFn,
94 pub as_ptr: SliceAsPtrFn,
96 pub as_mut_ptr: SliceAsMutPtrFn,
98}
99
100impl SliceVTable {
101 pub const fn builder() -> SliceVTableBuilder {
103 SliceVTableBuilder::new()
104 }
105}
106
107pub struct SliceVTableBuilder {
109 as_ptr: Option<SliceAsPtrFn>,
110 as_mut_ptr: Option<SliceAsMutPtrFn>,
111 len: Option<SliceLenFn>,
112}
113
114impl SliceVTableBuilder {
115 #[allow(clippy::new_without_default)]
117 pub const fn new() -> Self {
118 Self {
119 len: None,
120 as_ptr: None,
121 as_mut_ptr: None,
122 }
123 }
124
125 pub const fn len(mut self, f: SliceLenFn) -> Self {
127 self.len = Some(f);
128 self
129 }
130
131 pub const fn as_ptr(mut self, f: SliceAsPtrFn) -> Self {
133 self.as_ptr = Some(f);
134 self
135 }
136
137 pub const fn as_mut_ptr(mut self, f: SliceAsMutPtrFn) -> Self {
139 self.as_mut_ptr = Some(f);
140 self
141 }
142
143 pub const fn build(self) -> SliceVTable {
149 SliceVTable {
150 len: self.len.unwrap(),
151 as_ptr: self.as_ptr.unwrap(),
152 as_mut_ptr: self.as_mut_ptr.unwrap(),
153 }
154 }
155}