shapely_core/
lib.rs

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