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
//! Wrapper types which ensures that a given field is encoded or decoded as a
//! certain kind of value.
use crate::de::{Decode, DecodeBytes, DecodePacked, Decoder};
use crate::en::{Encode, EncodeBytes, EncodePacked, Encoder};
use crate::hint::SequenceHint;
use crate::mode::{Binary, Text};
/// Ensures that the given value `T` is encoded as a sequence.
///
/// This exists as a simple shim for certain types, to ensure they're encoded as
/// a sequence, such as `Sequence<()>`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct Sequence<T>(pub T);
impl<T> Sequence<T> {
/// Construct a new sequence wrapper.
pub const fn new(value: T) -> Self {
Self(value)
}
}
impl<M> Encode<M> for Sequence<()> {
#[inline]
fn encode<E>(&self, _: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
where
E: Encoder<Mode = M>,
{
static HINT: SequenceHint = SequenceHint::with_size(0);
encoder.encode_sequence_fn(&HINT, |_| Ok(()))
}
}
impl<'de, M> Decode<'de, M> for Sequence<()> {
fn decode<D>(_: &D::Cx, decoder: D) -> Result<Self, D::Error>
where
D: Decoder<'de>,
{
decoder.decode_sequence(|_| Ok(Self(())))
}
}
/// Ensures that the given value `T` is encoded as bytes.
///
/// This is useful for values which have a generic implementation to be encoded
/// as a sequence, such as [`Vec`] and [`VecDeque`].
///
/// [`Vec`]: alloc::vec::Vec
/// [`VecDeque`]: alloc::collections::VecDeque
///
/// # Examples
///
/// ```
/// use musli::{Decode, Decoder};
/// use musli::compat::Bytes;
///
/// struct Struct {
/// field: Vec<u8>,
/// }
///
/// impl<'de, M> Decode<'de, M> for Struct
/// where
/// Bytes<Vec<u8>>: Decode<'de, M>
/// {
/// fn decode<D>(cx: &D::Cx, decoder: D) -> Result<Self, D::Error>
/// where
/// D: Decoder<'de, Mode = M>,
/// {
/// let Bytes(field) = Decode::decode(cx, decoder)?;
///
/// Ok(Struct {
/// field,
/// })
/// }
/// }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, Decode)]
#[musli(crate, transparent)]
#[musli(mode = Binary, bound = {T: EncodeBytes<Binary>}, decode_bound = {T: DecodeBytes<'de, Binary>})]
#[musli(mode = Text, bound = {T: EncodeBytes<Text>}, decode_bound = {T: DecodeBytes<'de, Text>})]
#[repr(transparent)]
pub struct Bytes<T>(#[musli(bytes)] pub T);
impl<T> AsRef<[u8]> for Bytes<T>
where
T: AsRef<[u8]>,
{
#[inline]
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl<T> AsMut<[u8]> for Bytes<T>
where
T: AsMut<[u8]>,
{
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
self.0.as_mut()
}
}
/// Treat `T` as if its packed.
///
/// This is for example implemented for tuples.
///
/// # Examples
///
/// ```
/// use musli::{Decode, Decoder};
/// use musli::compat::Packed;
///
/// struct Struct {
/// field: u8,
/// field2: u32,
/// }
///
/// impl<'de, M> Decode<'de, M> for Struct
/// where
/// Packed<(u8, u32)>: Decode<'de, M>
/// {
/// fn decode<D>(cx: &D::Cx, decoder: D) -> Result<Self, D::Error>
/// where
/// D: Decoder<'de, Mode = M>,
/// {
/// let Packed((field, field2)) = Decode::decode(cx, decoder)?;
///
/// Ok(Struct {
/// field,
/// field2,
/// })
/// }
/// }
/// ```
#[derive(Encode, Decode)]
#[musli(crate, transparent)]
#[musli(mode = Binary, bound = {T: EncodePacked<Binary>}, decode_bound = {T: DecodePacked<'de, Binary>})]
#[musli(mode = Text, bound = {T: EncodePacked<Text>}, decode_bound = {T: DecodePacked<'de, Text>})]
#[repr(transparent)]
pub struct Packed<T>(#[musli(packed)] pub T);