deser_core/foreign_impls/
bytes.rs1use ::bytes::{Bytes, BytesMut};
7use alloc::vec::Vec;
8
9use crate::State;
10use crate::adapters::bytes::{BytesBufImpl, encoding_adapter};
11use crate::de::impls::{Via, deserialize_via};
12use crate::de::{Deserialize, SinkHandle};
13use crate::error::Error;
14use crate::event::Atom;
15use crate::ser::{Chunk, Serialize, plain_atom};
16
17macro_rules! byte_buffer {
18 ($($ty:ty => $convert:expr;)*) => {
19 $(
20 impl Serialize for $ty {
21 begin_without_finish!();
22 plain_atom!(|v| Atom::Bytes(crate::Bytes::new(&v[..])));
23
24 fn serialize(&self, _state: &mut State) -> Result<Chunk<'_>, Error> {
25 Ok(Chunk::Atom(Atom::Bytes(crate::Bytes::new(&self[..]))))
26 }
27 }
28
29 impl Via<Vec<u8>> for $ty {
30 #[inline]
31 fn convert(value: Vec<u8>) -> Result<Self, Error> {
32 Ok($convert(value))
33 }
34 }
35
36 deserialize_via! {
37 [] $ty => Vec<u8>;
38 }
39
40 impl BytesBufImpl for $ty {
41 #[inline]
42 fn bytes(&self) -> &[u8] {
43 self
44 }
45
46 #[inline]
47 fn from_vec(bytes: Vec<u8>) -> Result<Self, Error> {
48 Ok($convert(bytes))
49 }
50
51 #[inline]
52 fn deserialize_into<'a, 'de>(out: &'a mut Option<Self>, state: &mut State) -> SinkHandle<'a, 'de> {
53 Deserialize::deserialize_into(out, state)
54 }
55 }
56
57 encoding_adapter!([] $ty);
58 )*
59 };
60}
61
62byte_buffer! {
63 Bytes => Bytes::from;
64 BytesMut => |value| BytesMut::from(Bytes::from(value));
66}