dbutils/lib.rs
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 146 147 148 149 150 151 152 153 154 155 156
//! Utils for developing database
#![cfg_attr(not(any(feature = "std", test)), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, allow(unused_attributes))]
#![deny(missing_docs)]
#[cfg(any(feature = "std", test))]
extern crate std;
#[cfg(all(not(feature = "std"), feature = "alloc"))]
extern crate alloc as std;
/// Traits and structs for checksuming.
pub mod checksum;
/// LEB128 encoding and decoding
pub mod leb128;
/// A vacant buffer that can be filled with bytes.
pub mod buffer;
/// Common error types.
pub mod error;
pub use cheap_clone::CheapClone;
/// Re-export of [`equivalent`](::equivalent) crate.
pub mod equivalent;
/// Types and traits for encoding and decoding.
pub mod types;
#[doc(hidden)]
pub mod __private {
pub use paste;
}
/// A macro to generate builder types.
///
/// A builder type is typically has a size and a closure that can fill a buffer with the given size.
///
/// ## Example
///
/// ```rust
/// use dbutils::{builder, buffer::VacantBuffer};
///
/// // Generates a builder type named `ValueBuilder` with a maximum size of `u32`.
/// builder!(
/// /// A builder for a value
/// ValueBuilder;
/// );
///
/// let builder = ValueBuilder::new(16, |mut buf: VacantBuffer<'_>| {
/// buf.fill(1);
/// Ok::<_, core::convert::Infallible>(buf.len())
/// });
///
/// assert_eq!(builder.size(), 16);
/// ```
#[macro_export]
macro_rules! builder {
($(
$(#[$meta:meta])*
$wrapper_vis:vis $name:ident
); +$(;)?
) => {
$(
$crate::__private::paste::paste! {
$(#[$meta])*
#[derive(::core::marker::Copy, ::core::clone::Clone, ::core::fmt::Debug)]
$wrapper_vis struct $name <F> {
/// The required size of the builder.
$wrapper_vis size: usize,
/// The builder closure.
$wrapper_vis f: F,
}
impl<F> ::core::convert::From<(usize, F)> for $name<F> {
#[inline]
fn from((size, f): (usize, F)) -> Self {
Self { size, f }
}
}
impl<F> ::core::convert::From<$name<F>> for (usize, F) {
#[inline]
fn from(builder: $name<F>) -> Self {
(builder.size, builder.f)
}
}
impl<F> $name<F> {
#[doc = "Creates a new `" $name "` with the given size and builder closure."]
#[inline]
pub const fn new(size: usize, f: F) -> Self
{
Self { size, f }
}
#[doc = "Returns the required `" $name "` size."]
#[inline]
pub const fn size(&self) -> usize {
self.size
}
#[doc = "Returns the `" $name "` builder closure."]
#[inline]
pub const fn builder(&self) -> &F {
&self.f
}
/// Deconstructs the value builder into the size and the builder closure.
#[inline]
pub fn into_components(self) -> (usize, F) {
(self.size, self.f)
}
}
impl<W, E> $crate::buffer::BufWriter for $name<W>
where
W: ::core::ops::Fn(&mut $crate::buffer::VacantBuffer<'_>) -> ::core::result::Result<usize, E>,
{
type Error = E;
#[inline]
fn encoded_len(&self) -> usize {
self.size()
}
#[inline]
fn write(&self, buf: &mut $crate::buffer::VacantBuffer<'_>) -> ::core::result::Result<usize, Self::Error> {
self.builder()(buf)
}
}
impl<W, E> $crate::buffer::BufWriterOnce for $name<W>
where
W: ::core::ops::FnOnce(&mut $crate::buffer::VacantBuffer<'_>) -> ::core::result::Result<usize, E>,
{
type Error = E;
#[inline]
fn encoded_len(&self) -> usize {
self.size()
}
#[inline]
fn write_once(self, buf: &mut $crate::buffer::VacantBuffer<'_>) -> ::core::result::Result<usize, Self::Error> {
self.into_components().1(buf)
}
}
}
)*
};
}