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::DefaultMode;
use musli::Context;
use crate::de::WireDecoder;
use crate::en::WireEncoder;
use crate::error::Error;
use crate::fixed::FixedBytes;
use crate::options::{self, Options};
use crate::reader::{Reader, SliceReader};
use crate::writer::Writer;
pub const DEFAULT_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<DefaultMode>,
{
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<DefaultMode>,
{
DEFAULT.to_writer(writer, value)
}
#[cfg(feature = "alloc")]
#[inline]
pub fn to_vec<T>(value: &T) -> Result<Vec<u8>, Error>
where
T: ?Sized + Encode<DefaultMode>,
{
DEFAULT.to_vec(value)
}
#[inline]
pub fn to_fixed_bytes<const N: usize, T>(value: &T) -> Result<FixedBytes<N>, Error>
where
T: ?Sized + Encode<DefaultMode>,
{
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, DefaultMode>,
{
DEFAULT.decode(reader)
}
#[inline]
pub fn from_slice<'de, T>(bytes: &'de [u8]) -> Result<T, Error>
where
T: Decode<'de, DefaultMode>,
{
DEFAULT.from_slice(bytes)
}
pub struct Encoding<M = DefaultMode, const OPT: Options = DEFAULT_OPTIONS> {
_marker: marker::PhantomData<M>,
}
impl Encoding<DefaultMode, DEFAULT_OPTIONS> {
pub const fn new() -> Self {
Encoding {
_marker: marker::PhantomData,
}
}
}
impl<M, const OPT: Options> Encoding<M, OPT> {
pub const fn with_mode<T>(self) -> Encoding<T, OPT> {
Encoding {
_marker: marker::PhantomData,
}
}
pub const fn with_options<const U: Options>(self) -> Encoding<M, U> {
Encoding {
_marker: marker::PhantomData,
}
}
musli_common::encoding_impls!(
M,
WireEncoder::<_, OPT, _>::new,
WireDecoder::<_, OPT, _>::new
);
musli_common::encoding_from_slice_impls!(M, WireDecoder::<_, F>::new);
}
impl<M, const OPT: Options> Clone for Encoding<M, OPT> {
#[inline]
fn clone(&self) -> Self {
*self
}
}
impl<M, const OPT: Options> Copy for Encoding<M, OPT> {}