planus/
lib.rs

1#![deny(missing_docs)]
2#![doc = include_str!("../README.md")]
3#![cfg_attr(not(feature = "std"), no_std)]
4mod backvec;
5mod builder;
6mod impls;
7mod slice_helpers;
8mod traits;
9
10/// Error types for serialization/deserialization
11pub mod errors;
12/// Types for interacting with vectors in serialized data
13pub mod vectors;
14
15#[doc(hidden)]
16pub extern crate alloc;
17#[doc(hidden)]
18pub mod table_reader;
19#[doc(hidden)]
20pub mod table_writer;
21
22pub use crate::{
23    builder::Builder,
24    errors::Error,
25    slice_helpers::{ArrayWithStartOffset, SliceWithStartOffset},
26    traits::*,
27    vectors::Vector,
28};
29
30#[doc(hidden)]
31pub const fn check_version_compatibility(s: &str) {
32    match s.as_bytes() {
33        b"planus-0.3.1" => (),
34        _ => panic!(
35            "Your generated code is out of date, please regenerate using planus version 0.3.1"
36        ),
37    }
38}
39
40/// A type alias for [`Result`] with a Planus error
41///
42/// It is recommended to handle reading of serialized data in functions
43/// returning this result type to avoid boilerplate error handling using
44/// the ? operator.
45///
46/// [`Result`]: std::result::Result
47pub type Result<T> = core::result::Result<T, Error>;
48#[doc(hidden)]
49pub type Cursor<'a, const N: usize> = array_init_cursor::Cursor<'a, u8, N>;
50
51#[doc(hidden)]
52pub enum Void {}
53
54impl From<Void> for crate::Error {
55    fn from(v: Void) -> Self {
56        match v {}
57    }
58}
59
60/// An offset to a serialized value of type T inside a buffer currently being built.
61pub struct Offset<T: ?Sized> {
62    offset: u32,
63    phantom: core::marker::PhantomData<T>,
64}
65impl<T: ?Sized> Copy for Offset<T> {}
66impl<T: ?Sized> Clone for Offset<T> {
67    #[inline]
68    fn clone(&self) -> Self {
69        *self
70    }
71}
72
73/// An offset to a serialized union value of type T inside a buffer currently being built.
74pub struct UnionOffset<T: ?Sized> {
75    tag: u8,
76    offset: Offset<()>,
77    phantom: core::marker::PhantomData<T>,
78}
79impl<T: ?Sized> Copy for UnionOffset<T> {}
80impl<T: ?Sized> Clone for UnionOffset<T> {
81    #[inline]
82    fn clone(&self) -> Self {
83        *self
84    }
85}
86
87impl<T: ?Sized> Offset<T> {
88    #[doc(hidden)]
89    pub fn downcast(&self) -> Offset<()> {
90        Offset {
91            offset: self.offset,
92            phantom: core::marker::PhantomData,
93        }
94    }
95}
96
97impl<T: ?Sized> UnionOffset<T> {
98    #[doc(hidden)]
99    #[inline]
100    pub fn new(tag: u8, offset: Offset<()>) -> UnionOffset<T> {
101        Self {
102            tag,
103            offset,
104            phantom: core::marker::PhantomData,
105        }
106    }
107
108    #[doc(hidden)]
109    #[inline]
110    pub fn tag(&self) -> u8 {
111        self.tag
112    }
113
114    #[doc(hidden)]
115    #[inline]
116    pub fn offset(&self) -> Offset<()> {
117        self.offset
118    }
119}