1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use crate::*;
use std::{
borrow::Cow,
cell::{Cell, RefCell},
rc::Rc,
sync::Arc,
};
macro_rules! impls {
[Encoder for $($name:ty),*] => ($(
impl<T: Encoder + ?Sized> Encoder for $name {
#[inline]
fn encoder(&self, c: &mut impl Write) -> io::Result<()> { (**self).encoder(c) }
}
)*);
[Decoder for $($name:ident),*] => ($(
impl<'de, T: Decoder<'de>> Decoder<'de> for $name<T> {
#[inline]
fn decoder(c: &mut &'de [u8]) -> Result<Self> { T::decoder(c).map(Self::from) }
}
)*);
}
impls!(Encoder for &T, &mut T, Box<T>, Rc<T>, Arc<T>);
impls!(Decoder for Box, Rc, Arc, Cell, RefCell);
macro_rules! impl_sp {
[$($name: ident),*] => ($(
impl<'de> Decoder<'de> for $name<str> {
#[inline] fn decoder(c: &mut &'de [u8]) -> Result<Self> { <&'de str>::decoder(c).map(Self::from) }
}
impl<'de, T: Decoder<'de>> Decoder<'de> for $name<[T]> {
#[inline] fn decoder(c: &mut &'de [u8]) -> Result<Self> { Vec::<T>::decoder(c).map(Self::from) }
}
)*);
}
impl_sp!(Box, Rc, Arc);
impl<T> Encoder for std::marker::PhantomData<T> {
#[inline]
fn encoder(&self, _: &mut impl Write) -> io::Result<()> {
Ok(())
}
}
impl<T> Decoder<'_> for std::marker::PhantomData<T> {
#[inline]
fn decoder(_: &mut &[u8]) -> Result<Self> {
Ok(std::marker::PhantomData)
}
}
impl<T: Encoder + Copy> Encoder for Cell<T> {
#[inline]
fn encoder(&self, c: &mut impl Write) -> io::Result<()> {
self.get().encoder(c)
}
}
impl<T: Encoder> Encoder for RefCell<T> {
#[inline]
fn encoder(&self, c: &mut impl Write) -> io::Result<()> {
self.try_borrow().map_err(invalid_input)?.encoder(c)
}
}
impl<'a, T> Encoder for Cow<'a, T>
where
T: ?Sized + Encoder + ToOwned,
{
fn encoder(&self, c: &mut impl Write) -> io::Result<()> {
(**self).encoder(c)
}
}
impl<'de, 'a, T: ?Sized> Decoder<'de> for Cow<'a, T>
where
T: ToOwned,
T::Owned: Decoder<'de>,
{
#[inline]
fn decoder(c: &mut &'de [u8]) -> Result<Self> {
T::Owned::decoder(c).map(Cow::Owned)
}
}