musli/storage/encoding.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
//! Module that defines [`Encoding`] whith allows for customization of the
//! encoding format, and the [`DEFAULT`] encoding configuration.
use core::marker;
use crate::mode::Binary;
use crate::options;
use crate::{IntoReader, Options};
use super::de::StorageDecoder;
use super::en::StorageEncoder;
use super::error::Error;
/// Default options to use with [`Encoding`].
pub const OPTIONS: Options = options::new().build();
/// The default configuration.
///
/// Uses variable-encoded numerical fields and variable-encoded prefix lengths.
///
/// The variable length encoding uses [`zigzag`] with [`variable length`]
/// encoding for numbers.
///
/// [`zigzag`]: https://en.wikipedia.org/wiki/Variable-length_quantity#Zigzag_encoding
/// [`variable length`]: https://en.wikipedia.org/wiki/Variable-length_quantity
pub const DEFAULT: Encoding = Encoding::new();
crate::macros::bare_encoding!(Binary, DEFAULT, storage, IntoReader);
/// Setting up encoding with parameters.
pub struct Encoding<const OPT: Options = OPTIONS, M = Binary>
where
M: 'static,
{
_marker: marker::PhantomData<M>,
}
impl Default for Encoding<OPTIONS, Binary> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl Encoding<OPTIONS, Binary> {
/// Construct a new [`Encoding`] instance which uses [`OPTIONS`].
///
/// You can modify this behavior by using a custom [`Options`] instance:
///
/// ```
/// use musli::{Encode, Decode};
/// use musli::options::{self, Options, Integer};
/// use musli::storage::Encoding;
/// # use musli::storage::Error;
///
/// const OPTIONS: Options = options::new().with_integer(Integer::Fixed).build();
/// const CONFIG: Encoding<OPTIONS> = Encoding::new().with_options();
///
/// #[derive(Debug, PartialEq, Encode, Decode)]
/// struct Person<'a> {
/// name: &'a str,
/// age: u32,
/// }
///
/// let mut out = Vec::new();
///
/// let expected = Person {
/// name: "Aristotle",
/// age: 61,
/// };
///
/// CONFIG.encode(&mut out, &expected)?;
/// let actual = CONFIG.decode(&out[..])?;
///
/// assert_eq!(expected, actual);
/// # Ok::<_, Error>(())
/// ```
pub const fn new() -> Self {
Encoding {
_marker: marker::PhantomData,
}
}
}
impl<const OPT: Options, M> Encoding<OPT, M>
where
M: 'static,
{
/// Change the mode of the encoding.
///
/// # Examples
///
/// ```rust
/// use musli::storage::{OPTIONS, Encoding};
///
/// enum Custom {}
///
/// const CONFIG: Encoding<OPTIONS, Custom> = Encoding::new().with_mode();
/// ```
pub const fn with_mode<T>(self) -> Encoding<OPT, T> {
Encoding {
_marker: marker::PhantomData,
}
}
/// Change the options of the encoding.
///
/// # Examples
///
/// ```
/// use musli::options::{self, Options, Integer};
/// use musli::storage::Encoding;
///
/// const OPTIONS: Options = options::new().with_integer(Integer::Fixed).build();
/// const CONFIG: Encoding<OPTIONS> = Encoding::new().with_options();
/// ```
pub const fn with_options<const U: Options>(self) -> Encoding<U, M> {
Encoding {
_marker: marker::PhantomData,
}
}
crate::macros::encoding_impls!(
M,
storage,
StorageEncoder::<_, OPT, _>::new,
StorageDecoder::<_, OPT, _>::new,
IntoReader::into_reader,
);
}
impl<const OPT: Options, M> Clone for Encoding<OPT, M> {
#[inline]
fn clone(&self) -> Self {
*self
}
}
impl<const OPT: Options, M> Copy for Encoding<OPT, M> {}