shapely_core/
lib.rs

1use std::mem::MaybeUninit;
2
3mod hashmap_impl;
4mod scalar_impls;
5
6mod scalar_contents;
7pub use scalar_contents::ScalarContents;
8
9mod shape;
10pub use shape::*;
11
12mod slot;
13pub use slot::Slot;
14
15mod partial;
16pub use partial::*;
17
18mod helpers;
19pub use helpers::*;
20
21pub mod mini_typeid;
22
23#[doc(hidden)]
24pub mod log;
25pub use log::*;
26
27#[cfg(test)]
28mod tests;
29
30#[cfg(test)]
31mod scalar_contents_tests;
32
33/// A wrapper around Vec<u8> for binary data
34#[derive(Debug, Clone, PartialEq, Eq)]
35pub struct Bytes(pub Vec<u8>);
36
37impl Shapely for Bytes {
38    fn shape() -> Shape {
39        Shape {
40            name: |f| write!(f, "Bytes"),
41            typeid: mini_typeid::of::<Self>(),
42            layout: std::alloc::Layout::new::<Self>(),
43            innards: Innards::Scalar(Scalar::Bytes),
44            set_to_default: Some(|addr: *mut u8| unsafe {
45                *(addr as *mut Bytes) = Bytes(Vec::new());
46            }),
47            drop_in_place: Some(|addr: *mut u8| unsafe {
48                std::ptr::drop_in_place(addr as *mut Bytes);
49            }),
50        }
51    }
52}
53
54/// Allows querying the [Shape] of a type, which in turn lets us inspect any fields, build a value of
55/// this type progressively, etc.
56pub trait Shapely: Sized {
57    /// Returns the shape of this type
58    fn shape() -> Shape;
59
60    /// Returns a shape def (a function that can describe this shape)
61    fn shape_desc() -> ShapeDesc {
62        ShapeDesc(Self::shape)
63    }
64
65    /// Allocates this shape on the heap and return a partial that allows gradually initializing its fields.
66    fn partial() -> Partial<'static> {
67        Partial::alloc(Self::shape_desc())
68    }
69
70    /// Initializes a `Partial` from a borrowed `MaybeUninit<Self>`.
71    ///
72    /// Before calling assume_init, make sure to call Partial.build_in_place().
73    fn partial_from_uninit(dest: &mut MaybeUninit<Self>) -> Partial<'_> {
74        Partial::borrow(dest)
75    }
76
77    // TODO: partial_from_mut? where all the fields are already initialized?
78}