use core::fmt;
use crate::Context;
use crate::expecting::{self, Expecting};
use crate::hint::{MapHint, SequenceHint};
use super::{Encode, EntriesEncoder, MapEncoder, SequenceEncoder, VariantEncoder, utils};
#[non_exhaustive]
pub enum TryFastEncode<T, E>
where
E: Encoder,
{
Ok,
Unsupported(T, E),
}
#[must_use = "Encoders must be consumed through one of its encode_* methods"]
#[allow(unused_variables)]
pub trait Encoder: Sized {
type Cx: Context<Error = Self::Error>;
type Error;
type Mode: 'static;
type EncodePack: SequenceEncoder<Cx = Self::Cx, Error = Self::Error, Mode = Self::Mode>;
type EncodeSome: Encoder<Cx = Self::Cx, Error = Self::Error, Mode = Self::Mode>;
type EncodeSequence: SequenceEncoder<Cx = Self::Cx, Error = Self::Error, Mode = Self::Mode>;
type EncodeMap: MapEncoder<Cx = Self::Cx, Error = Self::Error, Mode = Self::Mode>;
type EncodeMapEntries: EntriesEncoder<Cx = Self::Cx, Error = Self::Error, Mode = Self::Mode>;
type EncodeVariant: VariantEncoder<Cx = Self::Cx, Error = Self::Error, Mode = Self::Mode>;
type EncodeSequenceVariant: SequenceEncoder<Cx = Self::Cx, Error = Self::Error, Mode = Self::Mode>;
type EncodeMapVariant: MapEncoder<Cx = Self::Cx, Error = Self::Error, Mode = Self::Mode>;
#[doc(hidden)]
type __UseMusliEncoderAttributeMacro;
fn cx(&self) -> Self::Cx;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
#[inline]
fn try_fast_encode<T>(self, value: T) -> Result<TryFastEncode<T, Self>, Self::Error>
where
T: Encode<Self::Mode>,
{
Ok(TryFastEncode::Unsupported(value, self))
}
#[inline]
fn encode<T>(self, value: T) -> Result<(), Self::Error>
where
T: Encode<Self::Mode>,
{
match self.try_fast_encode(value)? {
TryFastEncode::Ok => Ok(()),
TryFastEncode::Unsupported(value, this) => value.encode(this),
}
}
#[inline]
fn encode_empty(self) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Empty,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_bool(self, v: bool) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Bool,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_char(self, v: char) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Char,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_u8(self, v: u8) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Unsigned8,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_u16(self, v: u16) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Unsigned16,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_u32(self, v: u32) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Unsigned32,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_u64(self, v: u64) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Unsigned64,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_u128(self, v: u128) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Unsigned128,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_i8(self, v: i8) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Signed8,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_i16(self, v: i16) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Signed16,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_i32(self, v: i32) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Signed32,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_i64(self, v: i64) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Signed64,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_i128(self, v: i128) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Signed128,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_usize(self, v: usize) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Usize,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_isize(self, v: isize) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Isize,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_f32(self, v: f32) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Float32,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_f64(self, v: f64) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Float64,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_array<const N: usize>(self, array: &[u8; N]) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Array,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_bytes(self, bytes: &[u8]) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Bytes,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_bytes_vectored<I>(self, len: usize, vectors: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item: AsRef<[u8]>>,
{
Err(self.cx().message(expecting::unsupported_type(
&expecting::Bytes,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_string(self, string: &str) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::String,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn collect_string<T>(self, value: &T) -> Result<(), Self::Error>
where
T: ?Sized + fmt::Display,
{
let cx = self.cx();
let buf = crate::alloc::collect_string(cx.alloc(), value).map_err(cx.map())?;
self.encode_string(buf.as_ref())
}
#[inline]
fn encode_some(self) -> Result<Self::EncodeSome, Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Option,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_none(self) -> Result<(), Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Option,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_pack(self) -> Result<Self::EncodePack, Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Pack,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_pack_fn<F>(self, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self::EncodePack) -> Result<(), Self::Error>,
{
let mut pack = self.encode_pack()?;
f(&mut pack)?;
pack.finish_sequence()
}
#[inline]
fn encode_slice<T>(self, slice: impl AsRef<[T]>) -> Result<(), Self::Error>
where
T: Encode<Self::Mode>,
{
utils::default_encode_slice(self, slice)
}
#[inline]
fn encode_slices<T>(
self,
len: usize,
slices: impl IntoIterator<Item: AsRef<[T]>>,
) -> Result<(), Self::Error>
where
T: Encode<Self::Mode>,
{
utils::default_encode_slices(self, len, slices)
}
#[inline]
fn encode_sequence(self, hint: impl SequenceHint) -> Result<Self::EncodeSequence, Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::SequenceWith(hint.size_hint()),
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_sequence_fn<F>(self, hint: impl SequenceHint, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self::EncodeSequence) -> Result<(), Self::Error>,
{
let mut seq = self.encode_sequence(hint)?;
f(&mut seq)?;
seq.finish_sequence()
}
#[inline]
#[must_use = "Map encoders must be consumed"]
fn encode_map(self, hint: impl MapHint) -> Result<Self::EncodeMap, Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Map,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_map_fn<F>(self, hint: impl MapHint, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self::EncodeMap) -> Result<(), Self::Error>,
{
let mut map = self.encode_map(hint)?;
f(&mut map)?;
map.finish_map()
}
#[inline]
fn encode_map_entries(self, hint: impl MapHint) -> Result<Self::EncodeMapEntries, Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::MapWith(hint.size_hint()),
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_variant(self) -> Result<Self::EncodeVariant, Self::Error> {
Err(self.cx().message(expecting::unsupported_type(
&expecting::Variant,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_variant_fn<F>(self, f: F) -> Result<(), Self::Error>
where
F: FnOnce(&mut Self::EncodeVariant) -> Result<(), Self::Error>,
{
let mut variant = self.encode_variant()?;
f(&mut variant)?;
variant.finish_variant()
}
#[inline]
fn encode_unit_variant<T>(self, tag: &T) -> Result<(), Self::Error>
where
T: ?Sized + Encode<Self::Mode>,
{
self.encode_variant_fn(|variant| {
variant.encode_tag()?.encode(tag)?;
variant.encode_data()?.encode_empty()?;
Ok(())
})
}
#[inline]
fn encode_sequence_variant<T>(
self,
tag: &T,
hint: impl SequenceHint,
) -> Result<Self::EncodeSequenceVariant, Self::Error>
where
T: ?Sized + Encode<Self::Mode>,
{
Err(self.cx().message(expecting::unsupported_type(
&expecting::SequenceVariant,
ExpectingWrapper::new(&self),
)))
}
#[inline]
fn encode_map_variant<T>(
self,
tag: &T,
hint: impl MapHint,
) -> Result<Self::EncodeMapVariant, Self::Error>
where
T: ?Sized + Encode<Self::Mode>,
{
Err(self.cx().message(expecting::unsupported_type(
&expecting::MapVariant,
ExpectingWrapper::new(&self),
)))
}
}
#[repr(transparent)]
struct ExpectingWrapper<T> {
inner: T,
}
impl<T> ExpectingWrapper<T> {
#[inline]
const fn new(value: &T) -> &Self {
unsafe { &*(value as *const T as *const Self) }
}
}
impl<T> Expecting for ExpectingWrapper<T>
where
T: Encoder,
{
#[inline]
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.expecting(f)
}
}