/rust/registry/src/index.crates.io-1949cf8c6b5b557f/bytes-1.11.0/src/serde.rs
Line | Count | Source |
1 | | use super::{Bytes, BytesMut}; |
2 | | use alloc::string::String; |
3 | | use alloc::vec::Vec; |
4 | | use core::{cmp, fmt}; |
5 | | use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; |
6 | | |
7 | | macro_rules! serde_impl { |
8 | | ($ty:ident, $visitor_ty:ident, $from_slice:ident, $from_vec:ident) => { |
9 | | impl Serialize for $ty { |
10 | | #[inline] |
11 | 0 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
12 | 0 | where |
13 | 0 | S: Serializer, |
14 | | { |
15 | 0 | serializer.serialize_bytes(&self) |
16 | 0 | } Unexecuted instantiation: <bytes::bytes::Bytes as serde_core::ser::Serialize>::serialize::<bincode::features::serde::ser::SerdeEncoder<bincode::enc::encoder::EncoderImpl<bincode::features::impl_std::IoWriter<std::io::buffered::bufwriter::BufWriter<&mut std::fs::File>>, bincode::config::Configuration>>> Unexecuted instantiation: <bytes::bytes::Bytes as serde_core::ser::Serialize>::serialize::<bincode::features::serde::ser::SerdeEncoder<bincode::enc::encoder::EncoderImpl<bincode::features::impl_std::IoWriter<surrealmx::compression::CompressedWriter>, bincode::config::Configuration>>> Unexecuted instantiation: <bytes::bytes::Bytes as serde_core::ser::Serialize>::serialize::<_> Unexecuted instantiation: <bytes::bytes_mut::BytesMut as serde_core::ser::Serialize>::serialize::<_> |
17 | | } |
18 | | |
19 | | struct $visitor_ty; |
20 | | |
21 | | impl<'de> de::Visitor<'de> for $visitor_ty { |
22 | | type Value = $ty; |
23 | | |
24 | 0 | fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
25 | 0 | formatter.write_str("byte array") |
26 | 0 | } Unexecuted instantiation: <bytes::serde::BytesMutVisitor as serde_core::de::Visitor>::expecting Unexecuted instantiation: <bytes::serde::BytesVisitor as serde_core::de::Visitor>::expecting |
27 | | |
28 | | #[inline] |
29 | 0 | fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error> |
30 | 0 | where |
31 | 0 | V: de::SeqAccess<'de>, |
32 | | { |
33 | 0 | let len = cmp::min(seq.size_hint().unwrap_or(0), 4096); |
34 | 0 | let mut values: Vec<u8> = Vec::with_capacity(len); |
35 | | |
36 | 0 | while let Some(value) = seq.next_element()? { |
37 | 0 | values.push(value); |
38 | 0 | } |
39 | | |
40 | 0 | Ok($ty::$from_vec(values)) |
41 | 0 | } Unexecuted instantiation: <bytes::serde::BytesMutVisitor as serde_core::de::Visitor>::visit_seq::<_> Unexecuted instantiation: <bytes::serde::BytesVisitor as serde_core::de::Visitor>::visit_seq::<_> |
42 | | |
43 | | #[inline] |
44 | 0 | fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> |
45 | 0 | where |
46 | 0 | E: de::Error, |
47 | | { |
48 | 0 | Ok($ty::$from_slice(v)) |
49 | 0 | } Unexecuted instantiation: <bytes::serde::BytesMutVisitor as serde_core::de::Visitor>::visit_bytes::<_> Unexecuted instantiation: <bytes::serde::BytesVisitor as serde_core::de::Visitor>::visit_bytes::<_> |
50 | | |
51 | | #[inline] |
52 | 0 | fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> |
53 | 0 | where |
54 | 0 | E: de::Error, |
55 | | { |
56 | 0 | Ok($ty::$from_vec(v)) |
57 | 0 | } Unexecuted instantiation: <bytes::serde::BytesVisitor as serde_core::de::Visitor>::visit_byte_buf::<bincode::error::DecodeError> Unexecuted instantiation: <bytes::serde::BytesMutVisitor as serde_core::de::Visitor>::visit_byte_buf::<_> Unexecuted instantiation: <bytes::serde::BytesVisitor as serde_core::de::Visitor>::visit_byte_buf::<_> |
58 | | |
59 | | #[inline] |
60 | 0 | fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> |
61 | 0 | where |
62 | 0 | E: de::Error, |
63 | | { |
64 | 0 | Ok($ty::$from_slice(v.as_bytes())) |
65 | 0 | } Unexecuted instantiation: <bytes::serde::BytesMutVisitor as serde_core::de::Visitor>::visit_str::<_> Unexecuted instantiation: <bytes::serde::BytesVisitor as serde_core::de::Visitor>::visit_str::<_> |
66 | | |
67 | | #[inline] |
68 | 0 | fn visit_string<E>(self, v: String) -> Result<Self::Value, E> |
69 | 0 | where |
70 | 0 | E: de::Error, |
71 | | { |
72 | 0 | Ok($ty::$from_vec(v.into_bytes())) |
73 | 0 | } Unexecuted instantiation: <bytes::serde::BytesMutVisitor as serde_core::de::Visitor>::visit_string::<_> Unexecuted instantiation: <bytes::serde::BytesVisitor as serde_core::de::Visitor>::visit_string::<_> |
74 | | } |
75 | | |
76 | | impl<'de> Deserialize<'de> for $ty { |
77 | | #[inline] |
78 | 0 | fn deserialize<D>(deserializer: D) -> Result<$ty, D::Error> |
79 | 0 | where |
80 | 0 | D: Deserializer<'de>, |
81 | | { |
82 | 0 | deserializer.deserialize_byte_buf($visitor_ty) |
83 | 0 | } Unexecuted instantiation: <bytes::bytes::Bytes as serde_core::de::Deserialize>::deserialize::<bincode::features::serde::de_owned::SerdeDecoder<bincode::de::decoder::DecoderImpl<bincode::features::impl_std::IoReader<&mut std::io::buffered::bufreader::BufReader<std::fs::File>>, bincode::config::Configuration, ()>>> Unexecuted instantiation: <bytes::bytes::Bytes as serde_core::de::Deserialize>::deserialize::<bincode::features::serde::de_owned::SerdeDecoder<bincode::de::decoder::DecoderImpl<bincode::features::impl_std::IoReader<&mut surrealmx::compression::CompressedReader>, bincode::config::Configuration, ()>>> Unexecuted instantiation: <bytes::bytes::Bytes as serde_core::de::Deserialize>::deserialize::<_> Unexecuted instantiation: <bytes::bytes_mut::BytesMut as serde_core::de::Deserialize>::deserialize::<_> |
84 | | } |
85 | | }; |
86 | | } |
87 | | |
88 | | serde_impl!(Bytes, BytesVisitor, copy_from_slice, from); |
89 | | serde_impl!(BytesMut, BytesMutVisitor, from, from_vec); |