From 7e53654702584dcca5fda6ff36deea2663dcdd4f Mon Sep 17 00:00:00 2001 From: "Joseph C. Osborn" Date: Wed, 29 Oct 2025 15:36:13 -0700 Subject: [PATCH] Force write_sint to actually write a signed int as per the docs --- rmp/README.md | 4 ++-- rmp/src/encode/sint.rs | 14 +++++--------- rmp/tests/func/encode/int.rs | 12 ++++++------ 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/rmp/README.md b/rmp/README.md index 3638553f..5e690c3f 100644 --- a/rmp/README.md +++ b/rmp/README.md @@ -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 diff --git a/rmp/src/encode/sint.rs b/rmp/src/encode/sint.rs index 778e6b5a..2d0ae86c 100644 --- a/rmp/src/encode/sint.rs +++ b/rmp/src/encode/sint.rs @@ -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 @@ -151,18 +151,14 @@ pub fn write_sint(wr: &mut W, val: i64) -> Result 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)), } } diff --git a/rmp/tests/func/encode/int.rs b/rmp/tests/func/encode/int.rs index 51ca722a..a96b7230 100644 --- a/rmp/tests/func/encode/int.rs +++ b/rmp/tests/func/encode/int.rs @@ -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] @@ -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] @@ -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); }