use core::marker;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[cfg(feature = "std")]
use std::io;
use musli::de::Decode;
use musli::en::Encode;
use musli::mode::Binary;
use musli::Context;
use musli_utils::options;
use musli_utils::{FixedBytes, Options, Reader, Writer};
use crate::de::WireDecoder;
use crate::en::WireEncoder;
use crate::error::Error;
pub const OPTIONS: options::Options = options::new().build();
pub const DEFAULT: Encoding = Encoding::new();
#[inline]
pub fn encode<W, T>(writer: W, value: &T) -> Result<(), Error>
where
W: Writer,
T: ?Sized + Encode<Binary>,
{
DEFAULT.encode(writer, value)
}
#[cfg(feature = "std")]
#[inline]
pub fn to_writer<W, T>(writer: W, value: &T) -> Result<(), Error>
where
W: io::Write,
T: ?Sized + Encode<Binary>,
{
DEFAULT.to_writer(writer, value)
}
#[cfg(feature = "alloc")]
#[inline]
pub fn to_vec<T>(value: &T) -> Result<Vec<u8>, Error>
where
T: ?Sized + Encode<Binary>,
{
DEFAULT.to_vec(value)
}
#[inline]
pub fn to_fixed_bytes<const N: usize, T>(value: &T) -> Result<FixedBytes<N>, Error>
where
T: ?Sized + Encode<Binary>,
{
DEFAULT.to_fixed_bytes::<N, _>(value)
}
#[inline]
pub fn decode<'de, R, T>(reader: R) -> Result<T, Error>
where
R: Reader<'de>,
T: Decode<'de, Binary>,
{
DEFAULT.decode(reader)
}
#[inline]
pub fn from_slice<'de, T>(bytes: &'de [u8]) -> Result<T, Error>
where
T: Decode<'de, Binary>,
{
DEFAULT.from_slice(bytes)
}
pub struct Encoding<const OPT: Options = OPTIONS, M = Binary> {
_marker: marker::PhantomData<M>,
}
impl Encoding<OPTIONS, Binary> {
pub const fn new() -> Self {
Encoding {
_marker: marker::PhantomData,
}
}
}
impl<const OPT: Options, M> Encoding<OPT, M> {
pub const fn with_mode<T>(self) -> Encoding<OPT, T> {
Encoding {
_marker: marker::PhantomData,
}
}
pub const fn with_options<const U: Options>(self) -> Encoding<U, M> {
Encoding {
_marker: marker::PhantomData,
}
}
musli_utils::encoding_impls!(
M,
WireEncoder::<_, OPT, _>::new,
WireDecoder::<_, OPT, _>::new
);
musli_utils::encoding_from_slice_impls!(M);
}
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> {}