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, Mode};
use musli::Context;
use crate::de::WireDecoder;
use crate::en::WireEncoder;
use crate::error::Error;
use crate::fixed_bytes::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,
Error: From<W::Error>,
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>,
Error: From<R::Error>,
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 F: Options = DEFAULT_OPTIONS> {
_marker: marker::PhantomData<M>,
}
impl Encoding<DefaultMode, DEFAULT_OPTIONS> {
pub const fn new() -> Self {
Encoding {
_marker: marker::PhantomData,
}
}
}
impl<M, const F: Options> Encoding<M, F>
where
M: Mode,
{
pub const fn with_mode<T>(self) -> Encoding<T, F>
where
T: Mode,
{
Encoding {
_marker: marker::PhantomData,
}
}
pub const fn with_options<const U: Options>(self) -> Encoding<M, U> {
Encoding {
_marker: marker::PhantomData,
}
}
#[deprecated = "This does nothing, use `with_options` instead"]
pub const fn with_variable_integers(self) -> Encoding<M, F> {
Encoding {
_marker: marker::PhantomData,
}
}
#[deprecated = "This does nothing, use `with_options` instead"]
pub const fn with_fixed_integers(self) -> Encoding<M, F> {
Encoding {
_marker: marker::PhantomData,
}
}
#[deprecated = "This does nothing, use `with_options` instead"]
pub const fn with_fixed_integers_le(self) -> Encoding<M, F> {
Encoding {
_marker: marker::PhantomData,
}
}
#[deprecated = "This does nothing, use `with_options` instead"]
pub const fn with_fixed_integers_be(self) -> Encoding<M, F> {
Encoding {
_marker: marker::PhantomData,
}
}
#[deprecated = "This does nothing, use `with_options` instead"]
pub const fn with_fixed_integers_ne(self) -> Encoding<M, F> {
Encoding {
_marker: marker::PhantomData,
}
}
#[deprecated = "This does nothing, use `with_options` instead"]
pub const fn with_fixed_integers_endian<E>(self) -> Encoding<M, F> {
Encoding {
_marker: marker::PhantomData,
}
}
#[deprecated = "This does nothing, use `with_options` instead"]
pub const fn with_variable_lengths(self) -> Encoding<M, F> {
Encoding {
_marker: marker::PhantomData,
}
}
#[deprecated = "This does nothing, use `with_options` instead"]
pub const fn with_fixed_lengths(self) -> Encoding<M, F> {
Encoding {
_marker: marker::PhantomData,
}
}
#[deprecated = "This does nothing, use `with_options` instead"]
pub const fn with_fixed_lengths64(self) -> Encoding<M, F> {
Encoding {
_marker: marker::PhantomData,
}
}
musli_common::encoding_impls!(WireEncoder::<_, F>::new, WireDecoder::<_, F>::new);
musli_common::encoding_from_slice_impls!(WireDecoder::<_, F>::new);
}
impl<M, const F: Options> Clone for Encoding<M, F>
where
M: Mode,
{
#[inline]
fn clone(&self) -> Self {
*self
}
}
impl<M, const F: Options> Copy for Encoding<M, F> where M: Mode {}