facet_core/types/def/
slice.rs1use crate::{PtrMut, ptr::PtrConst};
2
3use super::Shape;
4
5#[derive(Clone, Copy, Debug)]
7#[repr(C)]
8pub struct SliceDef<'shape> {
9 pub vtable: &'shape SliceVTable,
11 pub t: &'shape Shape<'shape>,
13}
14
15impl<'shape> SliceDef<'shape> {
16 pub const fn builder() -> SliceDefBuilder<'shape> {
18 SliceDefBuilder::new()
19 }
20
21 pub const fn t(&self) -> &'shape Shape<'shape> {
23 self.t
24 }
25}
26
27pub struct SliceDefBuilder<'shape> {
29 vtable: Option<&'shape SliceVTable>,
30 t: Option<&'shape Shape<'shape>>,
31}
32
33impl<'shape> SliceDefBuilder<'shape> {
34 #[allow(clippy::new_without_default)]
36 pub const fn new() -> Self {
37 Self {
38 vtable: None,
39 t: None,
40 }
41 }
42
43 pub const fn vtable(mut self, vtable: &'shape SliceVTable) -> Self {
45 self.vtable = Some(vtable);
46 self
47 }
48
49 pub const fn t(mut self, t: &'shape Shape<'shape>) -> Self {
51 self.t = Some(t);
52 self
53 }
54
55 pub const fn build(self) -> SliceDef<'shape> {
57 SliceDef {
58 vtable: self.vtable.unwrap(),
59 t: self.t.unwrap(),
60 }
61 }
62}
63
64pub type SliceLenFn = unsafe fn(slice: PtrConst) -> usize;
70
71pub type SliceAsPtrFn = unsafe fn(slice: PtrConst) -> PtrConst;
77
78pub type SliceAsMutPtrFn = unsafe fn(slice: PtrMut) -> PtrMut;
84
85#[derive(Clone, Copy, Debug)]
88#[repr(C)]
89pub struct SliceVTable {
90 pub len: SliceLenFn,
92 pub as_ptr: SliceAsPtrFn,
94 pub as_mut_ptr: SliceAsMutPtrFn,
96}
97
98impl SliceVTable {
99 pub const fn builder() -> SliceVTableBuilder {
101 SliceVTableBuilder::new()
102 }
103}
104
105pub struct SliceVTableBuilder {
107 as_ptr: Option<SliceAsPtrFn>,
108 as_mut_ptr: Option<SliceAsMutPtrFn>,
109 len: Option<SliceLenFn>,
110}
111
112impl SliceVTableBuilder {
113 #[allow(clippy::new_without_default)]
115 pub const fn new() -> Self {
116 Self {
117 len: None,
118 as_ptr: None,
119 as_mut_ptr: None,
120 }
121 }
122
123 pub const fn len(mut self, f: SliceLenFn) -> Self {
125 self.len = Some(f);
126 self
127 }
128
129 pub const fn as_ptr(mut self, f: SliceAsPtrFn) -> Self {
131 self.as_ptr = Some(f);
132 self
133 }
134
135 pub const fn as_mut_ptr(mut self, f: SliceAsMutPtrFn) -> Self {
137 self.as_mut_ptr = Some(f);
138 self
139 }
140
141 pub const fn build(self) -> SliceVTable {
147 SliceVTable {
148 len: self.len.unwrap(),
149 as_ptr: self.as_ptr.unwrap(),
150 as_mut_ptr: self.as_mut_ptr.unwrap(),
151 }
152 }
153}