mod de_borrowed;
mod de_owned;
mod ser;
pub use self::de_borrowed::*;
pub use self::de_owned::*;
pub use self::ser::*;
use serde::de::Visitor;
#[cfg(feature = "async-fiber")]
#[doc(alias = "decode_serde_async")]
#[doc(inline)]
pub use crate::decode_serde_async as decode_async;
#[cfg(feature = "async-fiber")]
#[doc(alias = "decode_serde_async_with_context")]
#[doc(inline)]
pub use crate::decode_serde_async_with_context as decode_async_with_context;
#[cfg(all(feature = "tokio", feature = "async-fiber", feature = "serde"))]
#[doc(alias = "decode_serde_async_tokio")]
#[doc(inline)]
pub use crate::decode_serde_tokio_async as decode_async_tokio;
#[cfg(all(feature = "tokio", feature = "async-fiber", feature = "serde"))]
#[doc(alias = "decode_serde_async_tokio_with_context")]
#[doc(inline)]
pub use crate::decode_serde_tokio_async_with_context as decode_async_tokio_with_context;
#[doc(hidden)]
#[macro_export]
#[rustfmt::skip]
macro_rules! bincode_error_serde {
(
$(#[$meta:meta])*
$vis:vis enum $name:ident {
$(
$(#[$variant_meta:meta])*
$variant:ident $( { $( $(#[$field_meta:meta])* $field:ident : $ftype:ty ),* $(,)? } )? $( ( $( $(#[$tuple_meta:meta])* $tname:ident : $ttype:ty ),* $(,)? ) )?
),* $(,)?
}
) => {
$(#[$meta])*
$vis enum $name {
$(
$(#[$variant_meta])*
$variant $( { $( $(#[$field_meta])* $field : $ftype ),* } )? $( ( $( $(#[$tuple_meta])* $ttype ),* ) )?,
)*
}
pastey::paste! {
$(
$(#[$variant_meta])*
#[doc(hidden)]
#[cold]
#[track_caller]
#[inline(never)]
pub const fn [<cold_ $name:snake _ $variant:snake>]<'de, V>(
$($($field : $ftype),*)?
$($($tname : $ttype),*)?
) -> core::result::Result<<V as Visitor<'de>>::Value, $name>
where
V: Visitor<'de>,
{
core::result::Result::Err($name::$variant $( { $($field),* } )? $( ( $( $tname ),* ) )?)
}
)*
}
};
}
bincode_error_serde! {
#[derive(Debug)]
#[non_exhaustive]
pub enum DecodeError {
AnyNotSupported,
IdentifierNotSupported,
IgnoredAnyNotSupported,
CannotBorrowOwnedData,
#[cfg(not(feature = "alloc"))]
CannotAllocate,
#[cfg(not(feature = "alloc"))]
CustomError,
}
}
#[cfg(feature = "alloc")]
impl serde::de::Error for crate::error::DecodeError {
fn custom<T>(msg: T) -> Self
where
T: core::fmt::Display,
{
use alloc::string::ToString;
crate::error::cold_decode_error_other_string::<()>(msg.to_string()).unwrap_err()
}
}
#[cfg(not(feature = "alloc"))]
impl serde::de::Error for crate::error::DecodeError {
fn custom<T>(_: T) -> Self
where
T: core::fmt::Display,
{
crate::error::cold_decode_error_serde::<()>(DecodeError::CustomError).unwrap_err()
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum EncodeError {
SequenceMustHaveLength,
#[cfg(not(feature = "alloc"))]
CannotCollectStr,
#[cfg(not(feature = "alloc"))]
CustomError,
}
#[allow(clippy::from_over_into)]
impl Into<crate::error::EncodeError> for EncodeError {
fn into(self) -> crate::error::EncodeError {
crate::error::cold_encode_error_serde::<()>(self).unwrap_err()
}
}
#[cfg(feature = "alloc")]
impl serde::ser::Error for crate::error::EncodeError {
fn custom<T>(msg: T) -> Self
where
T: core::fmt::Display,
{
use alloc::string::ToString;
crate::error::cold_encode_error_other_string::<()>(msg.to_string()).unwrap_err()
}
}
#[cfg(not(feature = "alloc"))]
impl serde::ser::Error for crate::error::EncodeError {
fn custom<T>(_: T) -> Self
where
T: core::fmt::Display,
{
crate::error::cold_encode_error_serde::<()>(EncodeError::CustomError).unwrap_err()
}
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Compat<T>(pub T);
impl<Context, T> crate::Decode<Context> for Compat<T>
where
T: serde::de::DeserializeOwned,
{
fn decode<D: crate::de::Decoder>(decoder: &mut D) -> Result<Self, crate::error::DecodeError> {
let serde_decoder = de_owned::SerdeDecoder { de: decoder };
T::deserialize(serde_decoder).map(Compat)
}
}
impl<'de, T, Context> crate::BorrowDecode<'de, Context> for Compat<T>
where
T: serde::de::DeserializeOwned,
{
fn borrow_decode<D: crate::de::BorrowDecoder<'de>>(
decoder: &mut D
) -> Result<Self, crate::error::DecodeError> {
let serde_decoder = de_owned::SerdeDecoder { de: decoder };
T::deserialize(serde_decoder).map(Compat)
}
}
impl<T> crate::Encode for Compat<T>
where
T: serde::Serialize,
{
fn encode<E: crate::enc::Encoder>(
&self,
encoder: &mut E,
) -> Result<(), crate::error::EncodeError> {
let serializer = ser::SerdeEncoder { enc: encoder };
self.0.serialize(serializer)?;
Ok(())
}
}
impl<T> core::fmt::Debug for Compat<T>
where
T: core::fmt::Debug,
{
fn fmt(
&self,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
f.debug_tuple("Compat").field(&self.0).finish()
}
}
impl<T> core::fmt::Display for Compat<T>
where
T: core::fmt::Display,
{
fn fmt(
&self,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
self.0.fmt(f)
}
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct BorrowCompat<T>(pub T);
impl<'de, T, Context> crate::de::BorrowDecode<'de, Context> for BorrowCompat<T>
where
T: serde::de::Deserialize<'de>,
{
fn borrow_decode<D: crate::de::BorrowDecoder<'de>>(
decoder: &mut D
) -> Result<Self, crate::error::DecodeError> {
let serde_decoder = de_borrowed::SerdeDecoder {
de: decoder,
pd: core::marker::PhantomData,
};
T::deserialize(serde_decoder).map(BorrowCompat)
}
}
impl<T> crate::Encode for BorrowCompat<T>
where
T: serde::Serialize,
{
fn encode<E: crate::enc::Encoder>(
&self,
encoder: &mut E,
) -> Result<(), crate::error::EncodeError> {
let serializer = ser::SerdeEncoder { enc: encoder };
self.0.serialize(serializer)?;
Ok(())
}
}
impl<T> core::fmt::Debug for BorrowCompat<T>
where
T: core::fmt::Debug,
{
fn fmt(
&self,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
f.debug_tuple("BorrowCompat").field(&self.0).finish()
}
}
impl<T> core::fmt::Display for BorrowCompat<T>
where
T: core::fmt::Display,
{
fn fmt(
&self,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
self.0.fmt(f)
}
}