Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rmp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ assert_eq!([0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a], bufs[4][..]);

But they aren't planned to be widely used. Instead we often need to encode bytes compactly to
save space. In these cases RMP provides functions that guarantee that for encoding the most
compact representation will be chosen.
compact representation with the specified sign will be chosen.

```rust
let mut buf = Vec::new();

rmp::encode::write_sint(&mut buf, 300).unwrap();

assert_eq!([0xcd, 0x1, 0x2c], buf[..]);
assert_eq!([0xd1, 0x1, 0x2c], buf[..]);
```

On the other hand for deserialization it is not matter in which representation the value is
Expand Down
14 changes: 5 additions & 9 deletions rmp/src/encode/sint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{write_marker, RmpWrite};
use crate::encode::{write_pfix, write_u16, write_u32, write_u64, write_u8, ValueWriteError};
use crate::encode::{write_pfix, ValueWriteError};
use crate::Marker;

/// Encodes and attempts to write a negative small integer value as a negative fixnum into the
Expand Down Expand Up @@ -151,18 +151,14 @@ pub fn write_sint<W: RmpWrite>(wr: &mut W, val: i64) -> Result<Marker, ValueWrit
.and(Ok(Marker::FixNeg(val as i8)))
.map_err(ValueWriteError::InvalidMarkerWrite)
}
val if -128 <= val && val < -32 => write_i8(wr, val as i8).and(Ok(Marker::I8)),
val if -32768 <= val && val < -128 => write_i16(wr, val as i16).and(Ok(Marker::I16)),
val if -2147483648 <= val && val < -32768 => write_i32(wr, val as i32).and(Ok(Marker::I32)),
val if val < -2147483648 => write_i64(wr, val).and(Ok(Marker::I64)),
val if 0 <= val && val < 128 => {
write_pfix(wr, val as u8)
.and(Ok(Marker::FixPos(val as u8)))
.map_err(ValueWriteError::InvalidMarkerWrite)
}
val if val < 256 => write_u8(wr, val as u8).and(Ok(Marker::U8)),
val if val < 65536 => write_u16(wr, val as u16).and(Ok(Marker::U16)),
val if val < 4294967296 => write_u32(wr, val as u32).and(Ok(Marker::U32)),
val => write_u64(wr, val as u64).and(Ok(Marker::U64)),
val if -128 <= val && val < 128 => write_i8(wr, val as i8).and(Ok(Marker::I8)),
val if -32768 <= val && val < 32768 => write_i16(wr, val as i16).and(Ok(Marker::I16)),
val if -2147483648 <= val && val < 2147483648 => write_i32(wr, val as i32).and(Ok(Marker::I32)),
val => write_i64(wr, val).and(Ok(Marker::I64)),
}
}
12 changes: 6 additions & 6 deletions rmp/tests/func/encode/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ fn pass_pack_sint_i16_min() {
fn pass_pack_sint_i16_max() {
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];

assert_eq!(Marker::U16, write_sint(&mut &mut buf[..], 32767).ok().unwrap());
assert_eq!(Marker::I16, write_sint(&mut &mut buf[..], 32767).ok().unwrap());

assert_eq!([0xcd, 0x7f, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buf);
assert_eq!([0xd1, 0x7f, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buf);
}

#[test]
Expand All @@ -228,9 +228,9 @@ fn pass_pack_sint_i32_min() {
fn pass_pack_sint_i32_max() {
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];

assert_eq!(Marker::U32, write_sint(&mut &mut buf[..], 2147483647).ok().unwrap());
assert_eq!(Marker::I32, write_sint(&mut &mut buf[..], 2147483647).ok().unwrap());

assert_eq!([0xce, 0x7f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00], buf);
assert_eq!([0xd2, 0x7f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00], buf);
}

#[test]
Expand All @@ -246,7 +246,7 @@ fn pass_pack_sint_i64_min() {
fn pass_pack_sint_i64_max() {
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];

assert_eq!(Marker::U64, write_sint(&mut &mut buf[..], 9223372036854775807).ok().unwrap());
assert_eq!(Marker::I64, write_sint(&mut &mut buf[..], 9223372036854775807).ok().unwrap());

assert_eq!([0xcf, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], buf);
assert_eq!([0xd3, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], buf);
}