dbutils/
lib.rs

1//! Utils for developing database
2#![cfg_attr(not(any(feature = "std", test)), no_std)]
3#![cfg_attr(docsrs, feature(doc_cfg))]
4#![cfg_attr(docsrs, allow(unused_attributes))]
5#![deny(missing_docs)]
6
7#[cfg(any(feature = "std", test))]
8extern crate std;
9
10#[cfg(all(not(feature = "std"), feature = "alloc"))]
11extern crate alloc as std;
12
13/// Traits and structs for checksuming.
14pub mod checksum;
15
16/// LEB128 encoding and decoding
17pub mod leb128;
18
19/// A vacant buffer that can be filled with bytes.
20pub mod buffer;
21
22/// Common error types.
23pub mod error;
24
25pub use cheap_clone::CheapClone;
26
27/// Traits for key comparison.
28pub mod equivalent;
29
30/// Similar to [`equivalent`], but for bytes.
31pub mod equivalentor;
32
33/// Types and traits for encoding and decoding.
34pub mod types;
35
36/// State for entries
37pub mod state;
38
39#[doc(hidden)]
40pub mod __private {
41  pub use paste;
42}
43
44/// A macro to generate builder types.
45///
46/// A builder type is typically has a size and a closure that can fill a buffer with the given size.
47///
48/// ## Example
49///
50/// ```rust
51/// use dbutils::{builder, buffer::VacantBuffer};
52///
53/// // Generates a builder type named `ValueBuilder` with a maximum size of `u32`.
54/// builder!(
55///   /// A builder for a value
56///   ValueBuilder;
57/// );
58///
59/// let builder = ValueBuilder::new(16, |mut buf: VacantBuffer<'_>| {
60///   buf.fill(1);
61///   Ok::<_, core::convert::Infallible>(buf.len())
62/// });
63///
64/// assert_eq!(builder.size(), 16);
65/// ```
66#[macro_export]
67macro_rules! builder {
68  ($(
69    $(#[$meta:meta])*
70    $wrapper_vis:vis $name:ident
71   ); +$(;)?
72  ) => {
73    $(
74      $crate::__private::paste::paste! {
75        $(#[$meta])*
76        #[derive(::core::marker::Copy, ::core::clone::Clone, ::core::fmt::Debug)]
77        $wrapper_vis struct $name <F> {
78          /// The required size of the builder.
79          $wrapper_vis size: usize,
80
81          /// The builder closure.
82          $wrapper_vis f: F,
83        }
84
85        impl<F> ::core::convert::From<(usize, F)> for $name<F> {
86          #[inline]
87          fn from((size, f): (usize, F)) -> Self {
88            Self { size, f }
89          }
90        }
91
92        impl<F> ::core::convert::From<$name<F>> for (usize, F) {
93          #[inline]
94          fn from(builder: $name<F>) -> Self {
95            (builder.size, builder.f)
96          }
97        }
98
99        impl<F> $name<F> {
100          #[doc = "Creates a new `" $name "` with the given size and builder closure."]
101          #[inline]
102          pub const fn new(size: usize, f: F) -> Self
103          {
104            Self { size, f }
105          }
106
107          #[doc = "Returns the required `" $name "` size."]
108          #[inline]
109          pub const fn size(&self) -> usize {
110            self.size
111          }
112
113          #[doc = "Returns the `" $name "` builder closure."]
114          #[inline]
115          pub const fn builder(&self) -> &F {
116            &self.f
117          }
118
119          /// Deconstructs the value builder into the size and the builder closure.
120          #[inline]
121          pub fn into_components(self) -> (usize, F) {
122            (self.size, self.f)
123          }
124        }
125
126        impl<W, E> $crate::buffer::BufWriter for $name<W>
127        where
128          W: ::core::ops::Fn(&mut $crate::buffer::VacantBuffer<'_>) -> ::core::result::Result<usize, E>,
129        {
130          type Error = E;
131
132          #[inline]
133          fn encoded_len(&self) -> usize {
134            self.size()
135          }
136
137          #[inline]
138          fn write(&self, buf: &mut $crate::buffer::VacantBuffer<'_>) -> ::core::result::Result<usize, Self::Error> {
139            self.builder()(buf)
140          }
141        }
142
143        impl<W, E> $crate::buffer::BufWriterOnce for $name<W>
144        where
145          W: ::core::ops::FnOnce(&mut $crate::buffer::VacantBuffer<'_>) -> ::core::result::Result<usize, E>,
146        {
147          type Error = E;
148
149          #[inline]
150          fn encoded_len(&self) -> usize {
151            self.size()
152          }
153
154          #[inline]
155          fn write_once(self, buf: &mut $crate::buffer::VacantBuffer<'_>) -> ::core::result::Result<usize, Self::Error> {
156            self.into_components().1(buf)
157          }
158        }
159      }
160    )*
161  };
162}
163
164/// Abort the process.
165#[inline(never)]
166#[cold]
167pub fn abort() -> ! {
168  #[cfg(feature = "std")]
169  {
170    std::process::abort()
171  }
172
173  #[cfg(not(feature = "std"))]
174  {
175    struct Abort;
176    impl Drop for Abort {
177      fn drop(&mut self) {
178        panic!();
179      }
180    }
181    let _a = Abort;
182    panic!("abort");
183  }
184}