use std::fmt;
mod impls;
mod size_hint {
use std::cmp;
#[inline]
pub fn cautious(hint: Option<usize>) -> usize {
cmp::min(hint.unwrap_or(0), 4096)
}
}
pub trait Error: Send + Sized + std::error::Error {
fn custom<T: fmt::Display>(msg: T) -> Self;
fn invalid_type<U: fmt::Display, E: fmt::Display>(unexp: U, exp: E) -> Self {
Error::custom(format_args!("invalid type: {}, expected {}", unexp, exp))
}
fn invalid_value<U: fmt::Display, E: fmt::Display>(unexp: U, exp: E) -> Self {
Error::custom(format_args!("invalid value: {}, expected {}", unexp, exp))
}
fn invalid_length<E: fmt::Display>(len: usize, exp: E) -> Self {
Error::custom(format_args!("invalid length: {}, expected {}", len, exp))
}
}
#[trait_variant::make(Send)]
pub trait Decoder: Send {
type Error: Error;
async fn decode_any<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_bool<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_bytes<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_i8<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_i16<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_i32<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_i64<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_u8<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_u16<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_u32<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_u64<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_f32<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_f64<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_array_bool<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_array_i8<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_array_i16<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_array_i32<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_array_i64<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_array_u8<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_array_u16<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_array_u32<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_array_u64<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_array_f32<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_array_f64<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_map<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_option<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_seq<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_string<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_tuple<V: Visitor>(
&mut self,
len: usize,
visitor: V,
) -> Result<V::Value, Self::Error>;
async fn decode_unit<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_uuid<V: Visitor>(&mut self, visitor: V) -> Result<V::Value, Self::Error>;
async fn decode_ignored_any<V: Visitor>(&mut self, visitor: V)
-> Result<V::Value, Self::Error>;
}
#[trait_variant::make(Send)]
pub trait FromStream: Send + Sized {
type Context: Send;
async fn from_stream<D: Decoder>(
context: Self::Context,
decoder: &mut D,
) -> Result<Self, D::Error>;
}
#[trait_variant::make(Send)]
pub trait ArrayAccess<T>: Send {
type Error: Error;
async fn buffer(&mut self, buffer: &mut [T]) -> Result<usize, Self::Error>;
}
#[trait_variant::make(Send)]
pub trait MapAccess: Send {
type Error: Error;
async fn next_key<K: FromStream>(
&mut self,
context: K::Context,
) -> Result<Option<K>, Self::Error>;
async fn next_value<V: FromStream>(&mut self, context: V::Context) -> Result<V, Self::Error>;
#[inline]
fn size_hint(&self) -> Option<usize> {
None
}
}
#[trait_variant::make(Send)]
pub trait SeqAccess: Send {
type Error: Error;
async fn next_element<T: FromStream>(
&mut self,
context: T::Context,
) -> Result<Option<T>, Self::Error>;
async fn expect_next<T: FromStream>(&mut self, context: T::Context) -> Result<T, Self::Error> {
async {
if let Some(element) = self.next_element(context).await? {
Ok(element)
} else {
Err(Error::custom("expected sequence element is missing"))
}
}
}
#[inline]
fn size_hint(&self) -> Option<usize> {
None
}
}
#[trait_variant::make(Send)]
pub trait Visitor: Send + Sized {
type Value;
fn expecting() -> &'static str;
fn visit_bool<E: Error>(self, v: bool) -> Result<Self::Value, E> {
Err(Error::invalid_type(v, Self::expecting()))
}
#[inline]
fn visit_i8<E: Error>(self, v: i8) -> Result<Self::Value, E> {
self.visit_i64(v as i64)
}
#[inline]
fn visit_i16<E: Error>(self, v: i16) -> Result<Self::Value, E> {
self.visit_i64(v as i64)
}
#[inline]
fn visit_i32<E: Error>(self, v: i32) -> Result<Self::Value, E> {
self.visit_i64(v as i64)
}
fn visit_i64<E: Error>(self, v: i64) -> Result<Self::Value, E> {
Err(Error::invalid_type(v, Self::expecting()))
}
#[inline]
fn visit_u8<E: Error>(self, v: u8) -> Result<Self::Value, E> {
self.visit_u64(v as u64)
}
#[inline]
fn visit_u16<E: Error>(self, v: u16) -> Result<Self::Value, E> {
self.visit_u64(v as u64)
}
#[inline]
fn visit_u32<E: Error>(self, v: u32) -> Result<Self::Value, E> {
self.visit_u64(v as u64)
}
fn visit_u64<E: Error>(self, v: u64) -> Result<Self::Value, E> {
Err(Error::invalid_type(v, Self::expecting()))
}
#[inline]
fn visit_f32<E: Error>(self, v: f32) -> Result<Self::Value, E> {
self.visit_f64(v as f64)
}
fn visit_f64<E: Error>(self, v: f64) -> Result<Self::Value, E> {
Err(Error::invalid_type(v, Self::expecting()))
}
#[allow(unused_variables)]
async fn visit_array_bool<A: ArrayAccess<bool>>(
self,
array: A,
) -> Result<Self::Value, A::Error> {
async { Err(Error::invalid_type("boolean array", Self::expecting())) }
}
#[allow(unused_variables)]
async fn visit_array_i8<A: ArrayAccess<i8>>(self, array: A) -> Result<Self::Value, A::Error> {
async { Err(Error::invalid_type("i8 array", Self::expecting())) }
}
#[allow(unused_variables)]
async fn visit_array_i16<A: ArrayAccess<i16>>(self, array: A) -> Result<Self::Value, A::Error> {
async { Err(Error::invalid_type("i16 array", Self::expecting())) }
}
#[allow(unused_variables)]
async fn visit_array_i32<A: ArrayAccess<i32>>(self, array: A) -> Result<Self::Value, A::Error> {
async { Err(Error::invalid_type("i32 array", Self::expecting())) }
}
#[allow(unused_variables)]
async fn visit_array_i64<A: ArrayAccess<i64>>(self, array: A) -> Result<Self::Value, A::Error> {
async { Err(Error::invalid_type("i64 array", Self::expecting())) }
}
#[allow(unused_variables)]
async fn visit_array_u8<A: ArrayAccess<u8>>(self, array: A) -> Result<Self::Value, A::Error> {
async { Err(Error::invalid_type("u8 array", Self::expecting())) }
}
#[allow(unused_variables)]
async fn visit_array_u16<A: ArrayAccess<u16>>(self, array: A) -> Result<Self::Value, A::Error> {
async { Err(Error::invalid_type("u16 array", Self::expecting())) }
}
#[allow(unused_variables)]
async fn visit_array_u32<A: ArrayAccess<u32>>(self, array: A) -> Result<Self::Value, A::Error> {
async { Err(Error::invalid_type("u32 array", Self::expecting())) }
}
#[allow(unused_variables)]
async fn visit_array_u64<A: ArrayAccess<u64>>(self, array: A) -> Result<Self::Value, A::Error> {
async { Err(Error::invalid_type("u64 array", Self::expecting())) }
}
#[allow(unused_variables)]
async fn visit_array_f32<A: ArrayAccess<f32>>(self, array: A) -> Result<Self::Value, A::Error> {
async { Err(Error::invalid_type("f32 array", Self::expecting())) }
}
#[allow(unused_variables)]
async fn visit_array_f64<A: ArrayAccess<f64>>(self, array: A) -> Result<Self::Value, A::Error> {
async { Err(Error::invalid_type("f64 array", Self::expecting())) }
}
fn visit_string<E: Error>(self, v: String) -> Result<Self::Value, E> {
Err(Error::invalid_type(v, Self::expecting()))
}
fn visit_unit<E: Error>(self) -> Result<Self::Value, E> {
Err(Error::invalid_type("unit", Self::expecting()))
}
fn visit_none<E: Error>(self) -> Result<Self::Value, E> {
Err(Error::invalid_type("Option::None", Self::expecting()))
}
#[allow(unused_variables)]
async fn visit_some<D: Decoder>(self, decoder: &mut D) -> Result<Self::Value, D::Error> {
async { Err(Error::invalid_type("Option::Some", Self::expecting())) }
}
#[allow(unused_variables)]
async fn visit_map<A: MapAccess>(self, map: A) -> Result<Self::Value, A::Error> {
async { Err(Error::invalid_type("map", Self::expecting())) }
}
#[allow(unused_variables)]
async fn visit_seq<A: SeqAccess>(self, seq: A) -> Result<Self::Value, A::Error> {
async { Err(Error::invalid_type("sequence", Self::expecting())) }
}
}
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct IgnoredAny;