facet_core/types/def/slice.rs
1use super::{PtrConst, PtrMut};
2
3use super::Shape;
4
5/// Fields for slice types
6#[derive(Clone, Copy, Debug)]
7#[repr(C)]
8pub struct SliceDef {
9 /// vtable for interacting with the slice
10 pub vtable: &'static SliceVTable,
11 /// shape of the items in the slice
12 pub t: &'static Shape,
13}
14
15impl SliceDef {
16 /// Construct a `SliceDef` from its vtable and element shape.
17 pub const fn new(vtable: &'static SliceVTable, t: &'static Shape) -> Self {
18 Self { vtable, t }
19 }
20
21 /// Returns the shape of the items in the slice
22 pub const fn t(&self) -> &'static Shape {
23 self.t
24 }
25}
26
27/// Get the number of items in the slice
28///
29/// # Safety
30///
31/// The `slice` parameter must point to aligned, initialized memory of the correct type.
32pub type SliceLenFn = unsafe fn(slice: PtrConst) -> usize;
33
34/// Get pointer to the data buffer of the slice
35///
36/// # Safety
37///
38/// The `slice` parameter must point to aligned, initialized memory of the correct type.
39pub type SliceAsPtrFn = unsafe fn(slice: PtrConst) -> PtrConst;
40
41/// Get mutable pointer to the data buffer of the slice
42///
43/// # Safety
44///
45/// The `slice` parameter must point to aligned, initialized memory of the correct type.
46pub type SliceAsMutPtrFn = unsafe fn(slice: PtrMut) -> PtrMut;
47
48/// Virtual table for a slice-like type (like `Vec<T>`,
49/// but also `HashSet<T>`, etc.)
50#[derive(Clone, Copy, Debug)]
51#[repr(C)]
52pub struct SliceVTable {
53 /// Number of items in the slice
54 pub len: SliceLenFn,
55 /// Get pointer to the data buffer of the slice.
56 pub as_ptr: SliceAsPtrFn,
57 /// Get mutable pointer to the data buffer of the slice.
58 pub as_mut_ptr: SliceAsMutPtrFn,
59}
60impl SliceVTable {
61 /// Const ctor for slice vtable.
62 pub const fn new(len: SliceLenFn, as_ptr: SliceAsPtrFn, as_mut_ptr: SliceAsMutPtrFn) -> Self {
63 Self {
64 len,
65 as_ptr,
66 as_mut_ptr,
67 }
68 }
69}