musli/packed/encoding.rs
1use core::marker;
2
3#[cfg(feature = "alloc")]
4use crate::alloc::Global;
5use crate::mode::Binary;
6use crate::options;
7use crate::{Context, Decode, Encode, IntoReader, IntoWriter, Options};
8
9#[cfg(feature = "alloc")]
10use super::error::Error;
11use crate::storage::de::StorageDecoder;
12use crate::storage::en::StorageEncoder;
13
14/// The default options for the packed encoding.
15pub const OPTIONS: Options = options::new().fixed().native_byte_order().build();
16
17#[allow(unused)]
18const DEFAULT: Encoding = Encoding::new();
19
20crate::macros::bare_encoding!(Binary, DEFAULT, packed, IntoReader, IntoWriter);
21
22/// Setting up encoding with parameters.
23pub struct Encoding<const OPT: Options = OPTIONS, M = Binary>
24where
25 M: 'static,
26{
27 _marker: marker::PhantomData<M>,
28}
29
30impl Default for Encoding<OPTIONS, Binary> {
31 #[inline]
32 fn default() -> Self {
33 Self::new()
34 }
35}
36
37impl Encoding<OPTIONS, Binary> {
38 /// Construct a new [`Encoding`]. which uses [`OPTIONS`].
39 ///
40 /// You can modify this behavior by using a custom [`Options`] instance:
41 ///
42 /// ```
43 /// use musli::{Encode, Decode};
44 /// use musli::options::{self, Options, Integer};
45 /// use musli::packed::Encoding;
46 /// # use musli::packed::Error;
47 ///
48 /// const OPTIONS: Options = options::new().integer(Integer::Fixed).build();
49 /// const CONFIG: Encoding<OPTIONS> = Encoding::new().with_options();
50 ///
51 /// #[derive(Debug, PartialEq, Encode, Decode)]
52 /// struct Person<'a> {
53 /// name: &'a str,
54 /// age: u32,
55 /// }
56 ///
57 /// let mut out = Vec::new();
58 ///
59 /// let expected = Person {
60 /// name: "Aristotle",
61 /// age: 61,
62 /// };
63 ///
64 /// CONFIG.encode(&mut out, &expected)?;
65 /// let actual = CONFIG.decode(&out[..])?;
66 ///
67 /// assert_eq!(expected, actual);
68 /// # Ok::<_, Error>(())
69 /// ```
70 pub const fn new() -> Self {
71 Encoding {
72 _marker: marker::PhantomData,
73 }
74 }
75}
76
77impl<const OPT: Options, M> Encoding<OPT, M>
78where
79 M: 'static,
80{
81 /// Change the mode of the encoding.
82 ///
83 /// # Examples
84 ///
85 /// ```
86 /// use musli::packed::{OPTIONS, Encoding};
87 ///
88 /// enum Custom {}
89 ///
90 /// const CONFIG: Encoding<OPTIONS, Custom> = Encoding::new().with_mode();
91 /// ```
92 pub const fn with_mode<T>(self) -> Encoding<OPT, T> {
93 Encoding {
94 _marker: marker::PhantomData,
95 }
96 }
97
98 /// Change the options of the encoding.
99 ///
100 /// # Examples
101 ///
102 /// ```
103 /// use musli::options::{self, Options, Integer};
104 /// use musli::packed::Encoding;
105 ///
106 /// const OPTIONS: Options = options::new().build();
107 /// const CONFIG: Encoding<OPTIONS> = Encoding::new().with_options();
108 /// ```
109 pub const fn with_options<const U: Options>(self) -> Encoding<U, M> {
110 Encoding {
111 _marker: marker::PhantomData,
112 }
113 }
114
115 crate::macros::encoding_impls!(
116 M,
117 packed,
118 StorageEncoder::<OPT, true, _, _, M>::new,
119 StorageDecoder::<OPT, true, _, _, M>::new,
120 IntoReader::into_reader,
121 IntoWriter::into_writer,
122 );
123}
124
125impl<const OPT: Options, M> Clone for Encoding<OPT, M> {
126 #[inline]
127 fn clone(&self) -> Self {
128 *self
129 }
130}
131
132impl<const OPT: Options, M> Copy for Encoding<OPT, M> {}