facet_types/list.rs
1use facet_opaque::{Opaque, OpaqueConst, OpaqueUninit};
2
3/// Initialize a list in place with a given capacity
4///
5/// # Safety
6///
7/// The `list` parameter must point to uninitialized memory of sufficient size.
8/// The function must properly initialize the memory.
9pub type ListInitInPlaceWithCapacityFn =
10 unsafe fn(list: OpaqueUninit, capacity: usize) -> Result<Opaque, ()>;
11
12/// Push an item to the list
13///
14/// # Safety
15///
16/// The `list` parameter must point to aligned, initialized memory of the correct type.
17/// `item` is moved out of (with [`std::ptr::read`]) — it should be deallocated
18/// afterwards but NOT dropped.
19pub type ListPushFn = unsafe fn(list: Opaque, item: Opaque);
20// FIXME: this forces allocating item separately, copying it, and then dropping it — it's not great.
21
22/// Get the number of items in the list
23///
24/// # Safety
25///
26/// The `list` parameter must point to aligned, initialized memory of the correct type.
27pub type ListLenFn = unsafe fn(list: OpaqueConst) -> usize;
28
29/// Get pointer to the item at the given index. Panics if out of bounds.
30///
31/// # Safety
32///
33/// The `list` parameter must point to aligned, initialized memory of the correct type.
34pub type ListGetItemPtrFn = unsafe fn(list: OpaqueConst, index: usize) -> OpaqueConst;
35
36/// Virtual table for a list-like type (like `Vec<T>`,
37/// but also `HashSet<T>`, etc.)
38#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
39pub struct ListVTable {
40 /// cf. [`ListInitInPlaceWithCapacityFn`]
41 pub init_in_place_with_capacity: ListInitInPlaceWithCapacityFn,
42
43 /// cf. [`ListPushFn`]
44 pub push: ListPushFn,
45
46 /// cf. [`ListLenFn`]
47 pub len: ListLenFn,
48
49 /// cf. [`ListGetItemPtrFn`]
50 pub get_item_ptr: ListGetItemPtrFn,
51}