#![cfg_attr(
feature = "derive",
doc = "[container-adapters]: crate::derive#container-adapters"
)]
#![cfg_attr(
not(feature = "derive"),
doc = "[container-adapters]: https://docs.rs/deser/latest/deser/derive/index.html#container-adapters"
)]
use alloc::borrow::Cow;
use alloc::vec::Vec;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};
use crate::State;
use crate::de::{Deserialize, InlineSeq, OwnedSink, SinkHandle};
use crate::error::Error;
use crate::event::{Atom, ContainerShape};
use crate::ser::{Begin, Describe, Emit, PlainSink, Serialize};
pub(crate) mod bytes;
mod derived;
mod stock;
mod text;
pub use self::bytes::{
Base64, Base64NoPad, Base64Url, Base64UrlNoPad, BytesBuf, BytesEncoding, BytesFallback,
BytesFallbackFormat, IntSeq,
};
pub use self::derived::Derived;
#[doc(hidden)]
pub use self::derived::{DerivedDeserialize, DerivedSerialize};
pub use self::stock::{
Borrowed, DefaultOnError, DisplayFromStr, Flag, FromInto, MapSkipError, TryFromInto,
VecSkipError,
};
pub use self::text::{Separated, SkipBlank, TrimWhitespace};
#[allow(unused_imports)]
pub(crate) use self::stock::skip_map_sink;
pub struct Same;
impl<'de, T: Deserialize<'de>> Deserialize<'de, T> for Same {
#[inline]
fn deserialize_into<'out>(
out: &'out mut Option<T>,
state: &mut State,
) -> SinkHandle<'out, 'de> {
T::deserialize_into(out, state)
}
fn expecting() -> Cow<'static, str> {
T::expecting()
}
#[inline]
fn initial_value() -> Option<T> {
T::initial_value()
}
#[inline]
fn deserialize_update<'out>(value: &'out mut T, state: &mut State) -> SinkHandle<'out, 'de> {
T::deserialize_update(value, state)
}
#[inline]
fn __private_atom_into(
out: &mut Option<T>,
atom: Atom,
state: &mut State,
) -> Result<(), Error> {
T::__private_atom_into(out, atom, state)
}
#[inline]
fn __private_borrowed_atom_into(
out: &mut Option<T>,
atom: Atom<'de>,
state: &mut State,
) -> Result<(), Error> {
T::__private_borrowed_atom_into(out, atom, state)
}
#[inline]
fn __private_is_bytes() -> bool {
T::__private_is_bytes()
}
#[inline]
fn __private_vec_from_bytes(bytes: Vec<u8>) -> Option<Vec<T>> {
T::__private_vec_from_bytes(bytes)
}
#[inline]
fn __private_array_from_bytes<const N: usize>(bytes: &[u8]) -> Option<[T; N]> {
T::__private_array_from_bytes(bytes)
}
#[inline]
fn __private_atom_default() -> Option<T> {
T::__private_atom_default()
}
#[inline]
fn __private_inline_seq() -> Option<InlineSeq<T>> {
T::__private_inline_seq()
}
#[inline(always)]
fn __private_raw() -> Option<&'static crate::ext::RawFormatInfo> {
T::__private_raw()
}
fn describe_type(d: &mut dyn Describe) {
T::describe_type(d)
}
#[inline]
fn __private_collects() -> bool {
T::__private_collects()
}
#[inline]
fn __private_collect_into<'out>(
out: &'out mut Option<T>,
state: &mut State,
) -> SinkHandle<'out, 'de> {
T::__private_collect_into(out, state)
}
#[inline]
fn __private_collect_update<'out>(
value: &'out mut T,
first: bool,
state: &mut State,
) -> SinkHandle<'out, 'de> {
T::__private_collect_update(value, first, state)
}
#[inline]
fn __private_collect_empty() -> Option<T> {
T::__private_collect_empty()
}
}
impl<T: Serialize + ?Sized> Serialize<T> for Same {
#[inline]
fn serialize<'a>(value: &'a T, state: &mut State) -> Result<Emit<'a>, Error> {
T::serialize(value, state)
}
#[inline]
fn finish(value: &T, state: &mut State) -> Result<(), Error> {
T::finish(value, state)
}
#[inline]
fn is_optional(value: &T) -> bool {
T::is_optional(value)
}
#[inline]
fn container_shape(value: &T) -> ContainerShape {
T::container_shape(value)
}
fn describe(value: &T, d: &mut dyn Describe) {
T::describe(value, d)
}
#[inline]
fn __private_begin<'a>(value: &'a T, state: &mut State) -> Result<Begin<'a>, Error> {
T::__private_begin(value, state)
}
#[inline]
fn __private_is_plain() -> bool {
T::__private_is_plain()
}
#[inline]
fn __private_is_plain_value(value: &T) -> bool {
T::__private_is_plain_value(value)
}
#[inline]
fn __private_emit_plain(value: &T, sink: &mut dyn PlainSink) -> Result<(), Error> {
T::__private_emit_plain(value, sink)
}
#[inline]
fn __private_plain_cost(value: &T, budget: usize) -> Option<usize> {
T::__private_plain_cost(value, budget)
}
#[inline]
fn __private_slice_as_bytes(val: &[T]) -> Option<Cow<'_, [u8]>>
where
T: Sized,
{
T::__private_slice_as_bytes(val)
}
}
#[repr(transparent)]
pub struct As<T, A> {
value: T,
_marker: PhantomData<fn() -> A>,
}
impl<T, A> As<T, A> {
#[inline(always)]
pub fn new(value: T) -> As<T, A> {
As {
value,
_marker: PhantomData,
}
}
#[inline(always)]
pub fn into_inner(self) -> T {
self.value
}
}
impl<T, A> From<T> for As<T, A> {
fn from(value: T) -> As<T, A> {
As::new(value)
}
}
impl<T, A> Deref for As<T, A> {
type Target = T;
fn deref(&self) -> &T {
&self.value
}
}
impl<T, A> DerefMut for As<T, A> {
fn deref_mut(&mut self) -> &mut T {
&mut self.value
}
}
impl<T: fmt::Debug, A> fmt::Debug for As<T, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.value, f)
}
}
impl<T: Clone, A> Clone for As<T, A> {
fn clone(&self) -> Self {
As::new(self.value.clone())
}
}
impl<T: Copy, A> Copy for As<T, A> {}
impl<T: Default, A> Default for As<T, A> {
fn default() -> Self {
As::new(T::default())
}
}
impl<T: PartialEq, A> PartialEq for As<T, A> {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
impl<T: Eq, A> Eq for As<T, A> {}
impl<T: PartialOrd, A> PartialOrd for As<T, A> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.value.partial_cmp(&other.value)
}
}
impl<T: Ord, A> Ord for As<T, A> {
fn cmp(&self, other: &Self) -> Ordering {
self.value.cmp(&other.value)
}
}
impl<T: Hash, A> Hash for As<T, A> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.value.hash(state)
}
}
impl<'de, T: Send, A: Deserialize<'de, T>> Deserialize<'de> for As<T, A> {
fn deserialize_into<'out>(
out: &'out mut Option<Self>,
state: &mut State,
) -> SinkHandle<'out, 'de> {
crate::de::mapped::MappedSink::handle(
out,
OwnedSink::deserialize_as::<A>(state),
|value| Ok(As::new(value)),
state,
)
}
fn expecting() -> Cow<'static, str> {
A::expecting()
}
fn initial_value() -> Option<Self> {
A::initial_value().map(As::new)
}
#[inline]
fn __private_atom_into(
out: &mut Option<Self>,
atom: Atom,
state: &mut State,
) -> Result<(), Error> {
let mut inner = None;
A::__private_atom_into(&mut inner, atom, state)?;
*out = inner.map(As::new);
Ok(())
}
#[inline]
fn __private_borrowed_atom_into(
out: &mut Option<Self>,
atom: Atom<'de>,
state: &mut State,
) -> Result<(), Error> {
let mut inner = None;
A::__private_borrowed_atom_into(&mut inner, atom, state)?;
*out = inner.map(As::new);
Ok(())
}
#[inline]
fn __private_is_bytes() -> bool {
A::__private_is_bytes()
}
#[inline(always)]
fn __private_raw() -> Option<&'static crate::ext::RawFormatInfo> {
A::__private_raw()
}
fn describe_type(d: &mut dyn Describe) {
A::describe_type(d)
}
fn __private_vec_from_bytes(bytes: Vec<u8>) -> Option<Vec<Self>> {
A::__private_vec_from_bytes(bytes).map(|x| x.into_iter().map(As::new).collect())
}
fn __private_array_from_bytes<const N: usize>(bytes: &[u8]) -> Option<[Self; N]> {
A::__private_array_from_bytes::<N>(bytes).map(|x| x.map(As::new))
}
}
impl<T: Sync, A: Serialize<T>> Serialize for As<T, A> {
#[inline]
fn serialize<'a>(value: &'a Self, state: &mut State) -> Result<Emit<'a>, Error> {
A::serialize(&value.value, state)
}
#[inline]
fn finish(value: &Self, state: &mut State) -> Result<(), Error> {
A::finish(&value.value, state)
}
#[inline]
fn is_optional(value: &Self) -> bool {
A::is_optional(&value.value)
}
#[inline]
fn container_shape(value: &Self) -> ContainerShape {
A::container_shape(&value.value)
}
#[inline]
fn describe(value: &Self, d: &mut dyn Describe) {
A::describe(&value.value, d)
}
#[inline]
fn __private_begin<'a>(value: &'a Self, state: &mut State) -> Result<Begin<'a>, Error> {
A::__private_begin(&value.value, state)
}
#[inline]
fn __private_is_plain() -> bool {
A::__private_is_plain()
}
#[inline]
fn __private_is_plain_value(value: &Self) -> bool {
A::__private_is_plain_value(&value.value)
}
#[inline]
fn __private_emit_plain(value: &Self, sink: &mut dyn PlainSink) -> Result<(), Error> {
A::__private_emit_plain(&value.value, sink)
}
#[inline]
fn __private_plain_cost(value: &Self, budget: usize) -> Option<usize> {
A::__private_plain_cost(&value.value, budget)
}
#[inline]
fn __private_slice_as_bytes(val: &[Self]) -> Option<Cow<'_, [u8]>> {
let val = unsafe { &*(val as *const [Self] as *const [T]) };
A::__private_slice_as_bytes(val)
}
}