use crate::{Context, Decode, Encode, Encoder, EncodingResult};
use crate::io::{Read, Slice, SliceMut, Write};
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "unstable", doc(cfg(feature = "alloc")))]
#[inline]
pub fn encode_bytes<V: Encode<crate::io::VecStream>>(value: V) -> EncodingResult<alloc::vec::Vec<u8>> {
let stream = crate::io::VecStream::new(alloc::vec::Vec::new(), 0);
let mut encoder = Encoder::new(stream, Context::default());
value.encode(&mut encoder)?;
Ok(encoder.stream.into_inner())
}
#[inline]
pub fn decode_bytes<'a, R: AsRef<[u8]>, V: Decode<Slice<'a>>>(bytes: &'a R) -> EncodingResult<V> {
let mut decoder = Encoder::new(Slice::new(bytes.as_ref()), Context::default());
V::decode(&mut decoder)
}
#[inline]
pub fn decode_bytes_owned<R: AsRef<[u8]>, V: for<'a> Decode<Slice<'a>>>(bytes: &R) -> EncodingResult<V> {
let mut decoder = Encoder::new(Slice::new(bytes.as_ref()), Context::default());
V::decode(&mut decoder)
}
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "unstable", doc(cfg(feature = "alloc")))]
#[inline]
pub fn encode_bytes_with<V: Encode<crate::io::VecStream>>(value: V, context: Context) -> EncodingResult<alloc::vec::Vec<u8>> {
let stream = crate::io::VecStream::new(alloc::vec::Vec::new(), 0);
let mut encoder = Encoder::new(stream, context);
value.encode(&mut encoder)?;
Ok(encoder.stream.into_inner())
}
#[inline]
pub fn decode_bytes_with<'a, R: AsRef<[u8]>, V: Decode<Slice<'a>>>(bytes: &'a R, context: Context) -> EncodingResult<V> {
let mut decoder = Encoder::new(Slice::new(bytes.as_ref()), context);
V::decode(&mut decoder)
}
#[inline]
pub fn decode_bytes_with_owned<R: AsRef<[u8]>, V: for<'a> Decode<Slice<'a>>>(bytes: &R, context: Context) -> EncodingResult<V> {
let mut decoder = Encoder::new(Slice::new(bytes.as_ref()), context);
V::decode(&mut decoder)
}
#[inline]
pub fn encode<W: IntoWrite, V: Encode<W::Write>>(
writer: W,
value: V,
) -> EncodingResult<()> {
let mut encoder = Encoder::new(writer.into_write(), Context::default());
value.encode(&mut encoder)
}
#[inline]
pub fn decode<R: IntoRead, V: Decode<R::Read>>(reader: R) -> EncodingResult<V> {
let mut decoder = Encoder::new(reader.into_read(), Context::default());
V::decode(&mut decoder)
}
#[inline]
pub fn encode_with<W: IntoWrite, V: Encode<W::Write>>(
writer: W,
context: Context,
value: V,
) -> EncodingResult<()> {
let mut encoder = Encoder::new(writer.into_write(), context);
value.encode(&mut encoder)
}
#[inline]
pub fn decode_with<R: IntoRead, V: Decode<R::Read>>(reader: R, context: Context) -> EncodingResult<V> {
let mut decoder = Encoder::new(reader.into_read(), context);
V::decode(&mut decoder)
}
pub trait IntoWrite {
type Write: Write;
fn into_write(self) -> Self::Write;
}
impl<T: Write> IntoWrite for T {
type Write = T;
#[inline(always)]
fn into_write(self) -> Self::Write {
self
}
}
impl<'a> IntoWrite for &'a mut [u8] {
type Write = SliceMut<'a>;
#[inline]
fn into_write(self) -> Self::Write {
SliceMut::new(self)
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "unstable", doc(cfg(feature = "alloc")))]
impl IntoWrite for alloc::vec::Vec<u8> {
type Write = crate::io::VecStream;
#[inline]
fn into_write(self) -> Self::Write {
crate::io::VecStream::new(self, 0)
}
}
pub trait IntoRead {
type Read: Read;
fn into_read(self) -> Self::Read;
}
impl<T: Read> IntoRead for T {
type Read = T;
#[inline(always)]
fn into_read(self) -> Self::Read {
self
}
}
impl<'a> IntoRead for &'a [u8] {
type Read = Slice<'a>;
#[inline]
fn into_read(self) -> Self::Read {
Slice::new(self)
}
}
impl<'a> IntoRead for &'a mut [u8] {
type Read = Slice<'a>;
#[inline]
fn into_read(self) -> Self::Read {
Slice::new(self)
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "unstable", doc(cfg(feature = "alloc")))]
impl IntoRead for alloc::vec::Vec<u8> {
type Read = crate::io::VecStream;
#[inline]
fn into_read(self) -> Self::Read {
crate::io::VecStream::new(self, 0)
}
}