Skip to content

Commit 926682d

Browse files
committed
Smaller write len
1 parent 06414f5 commit 926682d

File tree

3 files changed

+57
-65
lines changed

3 files changed

+57
-65
lines changed

rmp/src/encode/bin.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,22 @@ use crate::Marker;
1313
/// This function will return `ValueWriteError` on any I/O error occurred while writing either the
1414
/// marker or the data.
1515
pub fn write_bin_len<W: RmpWrite>(wr: &mut W, len: u32) -> Result<Marker, ValueWriteError<W::Error>> {
16-
if len < 256 {
17-
write_marker(&mut *wr, Marker::Bin8)?;
18-
wr.write_data_u8(len as u8)?;
19-
Ok(Marker::Bin8)
16+
let marker = if len < 256 {
17+
Marker::Bin8
2018
} else if len <= u16::MAX as u32 {
21-
write_marker(&mut *wr, Marker::Bin16)?;
22-
wr.write_data_u16(len as u16)?;
23-
Ok(Marker::Bin16)
19+
Marker::Bin16
2420
} else {
25-
write_marker(&mut *wr, Marker::Bin32)?;
21+
Marker::Bin32
22+
};
23+
write_marker(&mut *wr, marker)?;
24+
if marker == Marker::Bin8 {
25+
wr.write_data_u8(len as u8)?;
26+
} else if marker == Marker::Bin16 {
27+
wr.write_data_u16(len as u16)?;
28+
} else if marker == Marker::Bin32 {
2629
wr.write_data_u32(len)?;
27-
Ok(Marker::Bin32)
2830
}
31+
Ok(marker)
2932
}
3033

3134
/// Encodes and attempts to write the most efficient binary implementation to the given `Write`.

rmp/src/encode/mod.rs

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -257,18 +257,19 @@ impl<E: RmpWriteErr> Display for ValueWriteError<E> {
257257
/// marker or the data.
258258
pub fn write_array_len<W: RmpWrite>(wr: &mut W, len: u32) -> Result<Marker, ValueWriteError<W::Error>> {
259259
let marker = if len < 16 {
260-
write_marker(wr, Marker::FixArray(len as u8))?;
261260
Marker::FixArray(len as u8)
262261
} else if len <= u16::MAX as u32 {
263-
write_marker(wr, Marker::Array16)?;
264-
wr.write_data_u16(len as u16)?;
265262
Marker::Array16
266263
} else {
267-
write_marker(wr, Marker::Array32)?;
268-
wr.write_data_u32(len)?;
269264
Marker::Array32
270265
};
271266

267+
write_marker(wr, marker)?;
268+
if marker == Marker::Array16 {
269+
wr.write_data_u16(len as u16)?;
270+
} else if marker == Marker::Array32 {
271+
wr.write_data_u32(len)?;
272+
}
272273
Ok(marker)
273274
}
274275

@@ -281,18 +282,19 @@ pub fn write_array_len<W: RmpWrite>(wr: &mut W, len: u32) -> Result<Marker, Valu
281282
/// marker or the data.
282283
pub fn write_map_len<W: RmpWrite>(wr: &mut W, len: u32) -> Result<Marker, ValueWriteError<W::Error>> {
283284
let marker = if len < 16 {
284-
write_marker(wr, Marker::FixMap(len as u8))?;
285285
Marker::FixMap(len as u8)
286286
} else if len <= u16::MAX as u32 {
287-
write_marker(wr, Marker::Map16)?;
288-
wr.write_data_u16(len as u16)?;
289287
Marker::Map16
290288
} else {
291-
write_marker(wr, Marker::Map32)?;
292-
wr.write_data_u32(len)?;
293289
Marker::Map32
294290
};
295291

292+
write_marker(wr, marker)?;
293+
if marker == Marker::Map16 {
294+
wr.write_data_u16(len as u16)?;
295+
} else if marker == Marker::Map32 {
296+
wr.write_data_u32(len)?;
297+
}
296298
Ok(marker)
297299
}
298300

@@ -310,42 +312,24 @@ pub fn write_map_len<W: RmpWrite>(wr: &mut W, len: u32) -> Result<Marker, ValueW
310312
/// 2-byte type information.
311313
pub fn write_ext_meta<W: RmpWrite>(wr: &mut W, len: u32, ty: i8) -> Result<Marker, ValueWriteError<W::Error>> {
312314
let marker = match len {
313-
1 => {
314-
write_marker(wr, Marker::FixExt1)?;
315-
Marker::FixExt1
316-
}
317-
2 => {
318-
write_marker(wr, Marker::FixExt2)?;
319-
Marker::FixExt2
320-
}
321-
4 => {
322-
write_marker(wr, Marker::FixExt4)?;
323-
Marker::FixExt4
324-
}
325-
8 => {
326-
write_marker(wr, Marker::FixExt8)?;
327-
Marker::FixExt8
328-
}
329-
16 => {
330-
write_marker(wr, Marker::FixExt16)?;
331-
Marker::FixExt16
332-
}
333-
len if len < 256 => {
334-
write_marker(wr, Marker::Ext8)?;
335-
wr.write_data_u8(len as u8)?;
336-
Marker::Ext8
337-
}
338-
len if len < 65536 => {
339-
write_marker(wr, Marker::Ext16)?;
340-
wr.write_data_u16(len as u16)?;
341-
Marker::Ext16
342-
}
343-
len => {
344-
write_marker(wr, Marker::Ext32)?;
345-
wr.write_data_u32(len)?;
346-
Marker::Ext32
347-
}
315+
1 => Marker::FixExt1,
316+
2 => Marker::FixExt2,
317+
4 => Marker::FixExt4,
318+
8 => Marker::FixExt8,
319+
16 => Marker::FixExt16,
320+
0..=255 => Marker::Ext8,
321+
256..=65535 => Marker::Ext16,
322+
_ => Marker::Ext32,
348323
};
324+
write_marker(wr, marker)?;
325+
326+
if marker == Marker::Ext8 {
327+
wr.write_data_u8(len as u8)?;
328+
} else if marker == Marker::Ext16 {
329+
wr.write_data_u16(len as u16)?;
330+
} else if marker == Marker::Ext32 {
331+
wr.write_data_u32(len)?;
332+
}
349333

350334
wr.write_data_i8(ty)?;
351335

rmp/src/encode/str.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,27 @@ use crate::Marker;
1010
/// This function will return `ValueWriteError` on any I/O error occurred while writing either the
1111
/// marker or the data.
1212
pub fn write_str_len<W: RmpWrite>(wr: &mut W, len: u32) -> Result<Marker, ValueWriteError<W::Error>> {
13-
if len < 32 {
14-
write_marker(wr, Marker::FixStr(len as u8))?;
15-
Ok(Marker::FixStr(len as u8))
13+
let marker = if len < 32 {
14+
Marker::FixStr(len as u8)
1615
} else if len < 256 {
17-
write_marker(wr, Marker::Str8)?;
18-
wr.write_data_u8(len as u8)?;
19-
Ok(Marker::Str8)
16+
Marker::Str8
2017
} else if len <= u16::MAX as u32 {
21-
write_marker(wr, Marker::Str16)?;
22-
wr.write_data_u16(len as u16)?;
23-
Ok(Marker::Str16)
18+
Marker::Str16
2419
} else {
25-
write_marker(wr, Marker::Str32)?;
20+
Marker::Str32
21+
};
22+
23+
write_marker(wr, marker)?;
24+
if marker == Marker::Str8 {
25+
wr.write_data_u8(len as u8)?;
26+
}
27+
if marker == Marker::Str16 {
28+
wr.write_data_u16(len as u16)?;
29+
}
30+
if marker == Marker::Str32 {
2631
wr.write_data_u32(len)?;
27-
Ok(Marker::Str32)
2832
}
33+
Ok(marker)
2934
}
3035

3136
/// Encodes and attempts to write the most efficient string binary representation to the

0 commit comments

Comments
 (0)