1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#![deny(missing_docs)]
#![doc = include_str!("../README.md")]
#![cfg_attr(not(feature = "std"), no_std)]
mod backvec;
mod builder;
mod impls;
mod slice_helpers;
mod traits;

/// Error types for serialization/deserialization
pub mod errors;
/// Types for interacting with vectors in serialized data
pub mod vectors;

#[cfg(any(
    feature = "vtable-cache",
    feature = "string-cache",
    feature = "bytes-cache"
))]
mod builder_cache;

#[doc(hidden)]
pub extern crate alloc;
#[doc(hidden)]
pub mod table_reader;
#[doc(hidden)]
pub mod table_writer;

pub use crate::{
    builder::Builder,
    errors::Error,
    slice_helpers::{ArrayWithStartOffset, SliceWithStartOffset},
    traits::*,
    vectors::Vector,
};

#[doc(hidden)]
pub const fn check_version_compatibility(s: &str) {
    match s.as_bytes() {
        b"planus-0.4.0" => (),
        _ => panic!(
            "Your generated code is out of date, please regenerate using planus version 0.4.0"
        ),
    }
}

/// A type alias for [`Result`] with a Planus error
///
/// It is recommended to handle reading of serialized data in functions
/// returning this result type to avoid boilerplate error handling using
/// the ? operator.
///
/// [`Result`]: core::result::Result
pub type Result<T> = core::result::Result<T, Error>;
#[doc(hidden)]
pub type Cursor<'a, const N: usize> = array_init_cursor::Cursor<'a, u8, N>;

#[doc(hidden)]
pub enum Void {}

#[doc(hidden)]
/// Used in the union-builders in generated code
pub struct Uninitialized;

#[doc(hidden)]
/// Used in the union-builders in generated code
pub struct Initialized<const N: u8, T>(pub T);

#[doc(hidden)]
/// Used in the tables-builders in generated code
pub struct DefaultValue;

impl<P: Primitive, D: ?Sized> WriteAsDefault<P, D> for DefaultValue {
    type Prepared = Void;
    fn prepare(&self, _builder: &mut Builder, _default: &D) -> Option<Self::Prepared> {
        None
    }
}

impl From<Void> for crate::Error {
    fn from(v: Void) -> Self {
        match v {}
    }
}

/// An offset to a serialized value of type T inside a buffer currently being built.
pub struct Offset<T: ?Sized> {
    offset: u32,
    phantom: core::marker::PhantomData<T>,
}
impl<T: ?Sized> Copy for Offset<T> {}
impl<T: ?Sized> Clone for Offset<T> {
    #[inline]
    fn clone(&self) -> Self {
        *self
    }
}

/// An offset to a serialized union value of type T inside a buffer currently being built.
pub struct UnionOffset<T: ?Sized> {
    tag: u8,
    offset: Offset<()>,
    phantom: core::marker::PhantomData<T>,
}
impl<T: ?Sized> Copy for UnionOffset<T> {}
impl<T: ?Sized> Clone for UnionOffset<T> {
    #[inline]
    fn clone(&self) -> Self {
        *self
    }
}

impl<T: ?Sized> Offset<T> {
    #[doc(hidden)]
    pub fn downcast(&self) -> Offset<()> {
        Offset {
            offset: self.offset,
            phantom: core::marker::PhantomData,
        }
    }
}

impl<T: ?Sized> UnionOffset<T> {
    #[doc(hidden)]
    #[inline]
    pub fn new(tag: u8, offset: Offset<()>) -> UnionOffset<T> {
        Self {
            tag,
            offset,
            phantom: core::marker::PhantomData,
        }
    }

    #[doc(hidden)]
    #[inline]
    pub fn tag(&self) -> u8 {
        self.tag
    }

    #[doc(hidden)]
    #[inline]
    pub fn offset(&self) -> Offset<()> {
        self.offset
    }
}