1#![cfg_attr(channels_nightly, feature(doc_auto_cfg))]
45#![cfg_attr(not(feature = "std"), no_std)]
46
47extern crate alloc;
48
49use alloc::vec::Vec;
50
51pub trait Serializer<T> {
56 type Error;
58
59 fn serialize(&mut self, t: &T) -> Result<Vec<u8>, Self::Error>;
61}
62
63macro_rules! forward_serializer_impl {
64 ($to:ty) => {
65 type Error = <$to>::Error;
66
67 fn serialize(
68 &mut self,
69 t: &T,
70 ) -> Result<Vec<u8>, Self::Error> {
71 <$to>::serialize(self, t)
72 }
73 };
74}
75
76pub trait Deserializer<T> {
80 type Error;
82
83 fn deserialize(
87 &mut self,
88 buf: &mut [u8],
89 ) -> Result<T, Self::Error>;
90}
91
92macro_rules! forward_deserializer_impl {
93 ($to:ty) => {
94 type Error = <$to>::Error;
95
96 fn deserialize(
97 &mut self,
98 buf: &mut [u8],
99 ) -> Result<T, Self::Error> {
100 <$to>::deserialize(self, buf)
101 }
102 };
103}
104
105impl<T, U: Serializer<T> + ?Sized> Serializer<T> for &mut U {
106 forward_serializer_impl!(U);
107}
108
109impl<T, U: Deserializer<T> + ?Sized> Deserializer<T> for &mut U {
110 forward_deserializer_impl!(U);
111}
112
113#[cfg(feature = "alloc")]
114impl<T, U: Serializer<T> + ?Sized> Serializer<T>
115 for alloc::boxed::Box<U>
116{
117 forward_serializer_impl!(U);
118}
119
120#[cfg(feature = "alloc")]
121impl<T, U: Deserializer<T> + ?Sized> Deserializer<T>
122 for alloc::boxed::Box<U>
123{
124 forward_deserializer_impl!(U);
125}
126
127macro_rules! serdes_impl {
128 ($module:ident :: $impl:ident if $($cfg:tt)+) => {
129 #[cfg($($cfg)+)]
130 pub mod $module;
131 #[cfg($($cfg)+)]
132 pub use self::$module::$impl;
133 };
134}
135
136#[cfg(feature = "aead")]
137pub mod aead;
138
139serdes_impl! { bincode::Bincode if feature = "bincode" }
140serdes_impl! { cbor::Cbor if feature = "cbor" }
141serdes_impl! { json::Json if feature = "json" }
142serdes_impl! { borsh::Borsh if feature = "borsh" }
143serdes_impl! { crc::Crc if feature = "crc" }
144serdes_impl! { deflate::Deflate if feature = "deflate" }
145serdes_impl! { hmac::Hmac if feature = "hmac" }