Skip to content

Commit e43cd65

Browse files
committed
Specialize u8 writes
1 parent 69f90c6 commit e43cd65

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

rmp/src/encode/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub use self::bin::{write_bin, write_bin_len};
1313
pub use self::dec::{write_f32, write_f64};
1414
pub use self::sint::{write_i16, write_i32, write_i64, write_i8, write_nfix, write_sint};
1515
pub use self::str::{write_str, write_str_len};
16-
pub use self::uint::{write_pfix, write_u16, write_u32, write_u64, write_u8, write_uint};
16+
pub use self::uint::{write_pfix, write_u16, write_u32, write_u64, write_u8, write_uint, write_uint8};
1717

1818
#[cfg(feature = "std")]
1919
use std::error;

rmp/src/encode/uint.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ pub fn write_u64<W: RmpWrite>(wr: &mut W, val: u64) -> Result<(), ValueWriteErro
121121
Ok(())
122122
}
123123

124+
/// Encodes and attempts to write an `u8` value into the given write using the most efficient
125+
/// representation, returning the marker used.
126+
///
127+
/// See [`write_uint`] for more info.
128+
pub fn write_uint8<W: RmpWrite>(wr: &mut W, val: u8) -> Result<Marker, ValueWriteError<W::Error>> {
129+
if val < 128 {
130+
write_pfix(wr, val as u8)
131+
.and(Ok(Marker::FixPos(val as u8)))
132+
.map_err(ValueWriteError::InvalidMarkerWrite)
133+
} else {
134+
write_u8(wr, val as u8).and(Ok(Marker::U8))
135+
}
136+
}
137+
124138
/// Encodes and attempts to write an `u64` value into the given write using the most efficient
125139
/// representation, returning the marker used.
126140
///
@@ -135,12 +149,8 @@ pub fn write_u64<W: RmpWrite>(wr: &mut W, val: u64) -> Result<(), ValueWriteErro
135149
/// This function will return `ValueWriteError` on any I/O error occurred while writing either the
136150
/// marker or the data.
137151
pub fn write_uint<W: RmpWrite>(wr: &mut W, val: u64) -> Result<Marker, ValueWriteError<W::Error>> {
138-
if val < 128 {
139-
write_pfix(wr, val as u8)
140-
.and(Ok(Marker::FixPos(val as u8)))
141-
.map_err(ValueWriteError::InvalidMarkerWrite)
142-
} else if val < 256 {
143-
write_u8(wr, val as u8).and(Ok(Marker::U8))
152+
if val < 256 {
153+
write_uint8(wr, val as u8)
144154
} else if val < 65536 {
145155
write_u16(wr, val as u16).and(Ok(Marker::U16))
146156
} else if val < 4294967296 {

0 commit comments

Comments
 (0)