use crate::Allocator;
use crate::de::{Decode, DecodeBytes, DecodePacked, Decoder};
use crate::en::{Encode, EncodeBytes, EncodePacked, Encoder};
use crate::mode::{Binary, Text};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct Sequence<T>(pub T);
impl<T> Sequence<T> {
#[inline]
pub const fn new(value: T) -> Self {
Self(value)
}
}
impl<M> Encode<M> for Sequence<()> {
type Encode = Self;
const IS_BITWISE_ENCODE: bool = true;
#[inline]
fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
where
E: Encoder<Mode = M>,
{
encoder.encode_sequence_fn(0, |_| Ok(()))
}
#[inline]
fn as_encode(&self) -> &Self::Encode {
self
}
}
impl<'de, M, A> Decode<'de, M, A> for Sequence<()>
where
A: Allocator,
{
const IS_BITWISE_DECODE: bool = true;
#[inline]
fn decode<D>(decoder: D) -> Result<Self, D::Error>
where
D: Decoder<'de, Allocator = A>,
{
decoder.decode_sequence(|_| Ok(Self(())))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, Decode)]
#[musli(crate, transparent)]
#[musli(Binary, bound = {T: EncodeBytes<Binary>}, decode_bound<'de, A> = {T: DecodeBytes<'de, Binary, A>})]
#[musli(Text, bound = {T: EncodeBytes<Text>}, decode_bound<'de, A> = {T: DecodeBytes<'de, Text, A>})]
#[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()
}
}
#[derive(Encode, Decode)]
#[musli(crate, transparent)]
#[musli(Binary, bound = {T: EncodePacked<Binary>}, decode_bound<'de, A> = {T: DecodePacked<'de, Binary, A>})]
#[musli(Text, bound = {T: EncodePacked<Text>}, decode_bound<'de, A> = {T: DecodePacked<'de, Text, A>})]
#[repr(transparent)]
pub struct Packed<T>(#[musli(packed)] pub T);