Skip to content

Commit 85e8ed3

Browse files
committed
Hack to use bytes
1 parent 1d9856b commit 85e8ed3

File tree

6 files changed

+384
-10
lines changed

6 files changed

+384
-10
lines changed

Diff for: rmp-serde/src/bytes.rs

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/// Hacky serializer that only allows `u8`
2+
3+
use std::fmt;
4+
use serde::ser::Impossible;
5+
use serde::Serialize;
6+
7+
pub(crate) struct OnlyBytes;
8+
pub(crate) struct Nope;
9+
10+
impl std::error::Error for Nope {
11+
}
12+
13+
impl std::fmt::Display for Nope {
14+
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
15+
Ok(())
16+
}
17+
}
18+
19+
impl std::fmt::Debug for Nope {
20+
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
21+
Ok(())
22+
}
23+
}
24+
25+
impl serde::ser::Error for Nope {
26+
fn custom<T: fmt::Display>(_: T) -> Self {
27+
Self
28+
}
29+
}
30+
31+
impl serde::de::Error for Nope {
32+
fn custom<T: fmt::Display>(_: T) -> Self {
33+
Self
34+
}
35+
}
36+
37+
impl serde::Serializer for OnlyBytes {
38+
type Ok = u8;
39+
type Error = Nope;
40+
type SerializeSeq = Impossible<u8, Nope>;
41+
type SerializeTuple = Impossible<u8, Nope>;
42+
type SerializeTupleStruct = Impossible<u8, Nope>;
43+
type SerializeTupleVariant = Impossible<u8, Nope>;
44+
type SerializeMap = Impossible<u8, Nope>;
45+
type SerializeStruct = Impossible<u8, Nope>;
46+
type SerializeStructVariant = Impossible<u8, Nope>;
47+
48+
fn serialize_u8(self, val: u8) -> Result<u8, Nope> {
49+
Ok(val)
50+
}
51+
52+
fn serialize_bool(self, _: bool) -> Result<u8, Nope> {
53+
Err(Nope)
54+
}
55+
56+
fn serialize_i8(self, _: i8) -> Result<u8, Nope> {
57+
Err(Nope)
58+
}
59+
60+
fn serialize_i16(self, _: i16) -> Result<u8, Nope> {
61+
Err(Nope)
62+
}
63+
64+
fn serialize_i32(self, _: i32) -> Result<u8, Nope> {
65+
Err(Nope)
66+
}
67+
68+
fn serialize_i64(self, _: i64) -> Result<u8, Nope> {
69+
Err(Nope)
70+
}
71+
72+
fn serialize_u16(self, _: u16) -> Result<u8, Nope> {
73+
Err(Nope)
74+
}
75+
76+
fn serialize_u32(self, _: u32) -> Result<u8, Nope> {
77+
Err(Nope)
78+
}
79+
80+
fn serialize_u64(self, _: u64) -> Result<u8, Nope> {
81+
Err(Nope)
82+
}
83+
84+
fn serialize_f32(self, _: f32) -> Result<u8, Nope> {
85+
Err(Nope)
86+
}
87+
88+
fn serialize_f64(self, _: f64) -> Result<u8, Nope> {
89+
Err(Nope)
90+
}
91+
92+
fn serialize_char(self, _: char) -> Result<u8, Nope> {
93+
Err(Nope)
94+
}
95+
96+
fn serialize_str(self, _: &str) -> Result<u8, Nope> {
97+
Err(Nope)
98+
}
99+
100+
fn serialize_bytes(self, _: &[u8]) -> Result<u8, Nope> {
101+
Err(Nope)
102+
}
103+
104+
fn serialize_none(self) -> Result<u8, Nope> {
105+
Err(Nope)
106+
}
107+
108+
fn serialize_some<T: ?Sized>(self, _: &T) -> Result<u8, Nope> where T: Serialize {
109+
Err(Nope)
110+
}
111+
112+
fn serialize_unit(self) -> Result<u8, Nope> {
113+
Err(Nope)
114+
}
115+
116+
fn serialize_unit_struct(self, _: &'static str) -> Result<u8, Nope> {
117+
Err(Nope)
118+
}
119+
120+
fn serialize_unit_variant(self, _: &'static str, _: u32, _: &'static str) -> Result<u8, Nope> {
121+
Err(Nope)
122+
}
123+
124+
fn serialize_newtype_struct<T: ?Sized>(self, _: &'static str, _: &T) -> Result<u8, Nope> where T: Serialize {
125+
Err(Nope)
126+
}
127+
128+
fn serialize_newtype_variant<T: ?Sized>(self, _: &'static str, _: u32, _: &'static str, _: &T) -> Result<u8, Nope> where T: Serialize {
129+
Err(Nope)
130+
}
131+
132+
fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Nope> {
133+
Err(Nope)
134+
}
135+
136+
fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Nope> {
137+
Err(Nope)
138+
}
139+
140+
fn serialize_tuple_struct(self, _: &'static str, _: usize) -> Result<Self::SerializeTupleStruct, Nope> {
141+
Err(Nope)
142+
}
143+
144+
fn serialize_tuple_variant(self, _: &'static str, _: u32, _: &'static str, _: usize) -> Result<Self::SerializeTupleVariant, Nope> {
145+
Err(Nope)
146+
}
147+
148+
fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, Nope> {
149+
Err(Nope)
150+
}
151+
152+
fn serialize_struct(self, _: &'static str, _: usize) -> Result<Self::SerializeStruct, Nope> {
153+
Err(Nope)
154+
}
155+
156+
fn serialize_struct_variant(self, _: &'static str, _: u32, _: &'static str, _: usize) -> Result<Self::SerializeStructVariant, Nope> {
157+
Err(Nope)
158+
}
159+
160+
fn collect_seq<I>(self, _: I) -> Result<u8, Nope> where I: IntoIterator, <I as IntoIterator>::Item: Serialize {
161+
Err(Nope)
162+
}
163+
164+
fn collect_map<K, V, I>(self, _: I) -> Result<u8, Nope> where K: Serialize, V: Serialize, I: IntoIterator<Item = (K, V)> {
165+
Err(Nope)
166+
}
167+
168+
fn collect_str<T: ?Sized>(self, _: &T) -> Result<u8, Nope> where T: fmt::Display {
169+
Err(Nope)
170+
}
171+
}

Diff for: rmp-serde/src/config.rs

+54
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub trait SerializerConfig: sealed::SerializerConfig {}
99
impl<T: sealed::SerializerConfig> SerializerConfig for T {}
1010

1111
pub(crate) mod sealed {
12+
use crate::config::BytesMode;
13+
1214
/// This is the inner trait - the real `SerializerConfig`.
1315
///
1416
/// This hack disallows external implementations and usage of `SerializerConfig` and thus
@@ -20,20 +22,46 @@ pub(crate) mod sealed {
2022

2123
/// String struct fields
2224
fn is_named(&self) -> bool;
25+
fn bytes(&self) -> BytesMode;
2326
}
2427
}
2528

2629
#[derive(Copy, Clone, Debug)]
2730
pub(crate) struct RuntimeConfig {
2831
pub(crate) is_human_readable: bool,
2932
pub(crate) is_named: bool,
33+
pub(crate) bytes: BytesMode,
34+
}
35+
36+
/// When to encode `[u8]` as `bytes` rather than a sequence
37+
/// of integers. Serde without `serde_bytes` has trouble
38+
/// using `bytes`, and this is hack to force it. It may
39+
/// break some data types.
40+
#[non_exhaustive]
41+
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
42+
pub enum BytesMode {
43+
/// Use bytes only when Serde requires it
44+
/// (typically only when `serde_bytes` is used)
45+
#[default]
46+
Normal,
47+
/// Use bytes for slices, `Vec`, and a few other types that
48+
/// use `Iterator` in Serde.
49+
///
50+
/// This may break some implementations of `Deserialize`.
51+
///
52+
/// This does not include fixed-length arrays.
53+
ForceIterables,
54+
/// Use bytes for everything that looks like a container of `u8`.
55+
/// This breaks some implementations of `Deserialize`.
56+
ForceAll,
3057
}
3158

3259
impl RuntimeConfig {
3360
pub(crate) fn new(other: impl sealed::SerializerConfig) -> Self {
3461
Self {
3562
is_human_readable: other.is_human_readable(),
3663
is_named: other.is_named(),
64+
bytes: other.bytes(),
3765
}
3866
}
3967
}
@@ -48,6 +76,11 @@ impl sealed::SerializerConfig for RuntimeConfig {
4876
fn is_named(&self) -> bool {
4977
self.is_named
5078
}
79+
80+
#[inline]
81+
fn bytes(&self) -> BytesMode {
82+
self.bytes
83+
}
5184
}
5285

5386
/// The default serializer/deserializer configuration.
@@ -71,6 +104,11 @@ impl sealed::SerializerConfig for DefaultConfig {
71104
fn is_human_readable(&self) -> bool {
72105
false
73106
}
107+
108+
#[inline(always)]
109+
fn bytes(&self) -> BytesMode {
110+
BytesMode::default()
111+
}
74112
}
75113

76114
/// Config wrapper, that overrides struct serialization by packing as a map with field names.
@@ -104,6 +142,10 @@ where
104142
fn is_human_readable(&self) -> bool {
105143
self.0.is_human_readable()
106144
}
145+
146+
fn bytes(&self) -> BytesMode {
147+
self.0.bytes()
148+
}
107149
}
108150

109151
/// Config wrapper that overrides struct serlization by packing as a tuple without field
@@ -132,6 +174,10 @@ where
132174
fn is_human_readable(&self) -> bool {
133175
self.0.is_human_readable()
134176
}
177+
178+
fn bytes(&self) -> BytesMode {
179+
self.0.bytes()
180+
}
135181
}
136182

137183
/// Config wrapper that overrides `Serializer::is_human_readable` and
@@ -160,6 +206,10 @@ where
160206
fn is_human_readable(&self) -> bool {
161207
true
162208
}
209+
210+
fn bytes(&self) -> BytesMode {
211+
self.0.bytes()
212+
}
163213
}
164214

165215
/// Config wrapper that overrides `Serializer::is_human_readable` and
@@ -188,4 +238,8 @@ where
188238
fn is_human_readable(&self) -> bool {
189239
false
190240
}
241+
242+
fn bytes(&self) -> BytesMode {
243+
self.0.bytes()
244+
}
191245
}

0 commit comments

Comments
 (0)