diff --git a/src/bipack_sink.rs b/src/bipack_sink.rs index 5978a82..bb8c5df 100644 --- a/src/bipack_sink.rs +++ b/src/bipack_sink.rs @@ -89,6 +89,19 @@ pub trait BipackSink { self.put_fixed_bytes(&result); } + fn put_i64(self: &mut Self, value: i64) { + self.put_u64(value as u64) + } + fn put_i32(self: &mut Self, value: i32) { + self.put_u32(value as u32) + } + fn put_i16(self: &mut Self, value: i16) { + self.put_u16(value as u16) + } + fn put_i8(self: &mut Self, value: i8) { + self.put_u8(value as u8) + } + /// Put unsigned value to compressed variable-length format, `Smartint` in the bipack /// terms. This format is used to store size of variable-length binaries and strings. /// Use [crate::bipack_source::BipackSource::get_unsigned] to unpack it. diff --git a/src/bipack_source.rs b/src/bipack_source.rs index 6a23960..f04185f 100644 --- a/src/bipack_source.rs +++ b/src/bipack_source.rs @@ -61,6 +61,19 @@ pub trait BipackSource { Ok(((self.get_u32()? as u64) << 32) | (self.get_u32()? as u64)) } + fn get_i64(self: &mut Self) -> Result { + Ok(self.get_u64()? as i64) + } + fn get_i32(self: &mut Self) -> Result { + Ok(self.get_u32()? as i32) + } + fn get_i16(self: &mut Self) -> Result { + Ok(self.get_u16()? as i16) + } + fn get_i8(self: &mut Self) -> Result { + Ok(self.get_u8()? as i8) + } + /// Unpack variable-length packed unsigned value, used aslo internally to store size /// of arrays, binary data, strings, etc. To pack use /// [crate::bipack_sink::BipackSink::put_unsigned()]. diff --git a/src/lib.rs b/src/lib.rs index e7d1e1b..f9a4a25 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -189,6 +189,44 @@ mod tests { assert_eq!("Hello, rupack!", src.get_str().unwrap()); } + #[test] + fn test_signed() -> Result<()> { + fn test64(value: i64) -> Result<()> { + let mut x = Vec::new(); + x.put_i64(value); + assert_eq!(value, SliceSource::from(&x).get_i64()?); + Ok(()) + } + test64(0)?; + test64(1)?; + test64(-1)?; + test64(9223372036854775807)?; + test64(-9223372036854775808)?; + fn test32(value: i32) -> Result<()> { + let mut x = Vec::new(); + x.put_i32(value); + assert_eq!(value, SliceSource::from(&x).get_i32()?); + Ok(()) + } + test32(0)?; + test32(1)?; + test32(-1)?; + test32(2147483647)?; + test32(-2147483648)?; + fn test16(value: i16) -> Result<()> { + let mut x = Vec::new(); + x.put_i16(value); + assert_eq!(value, SliceSource::from(&x).get_i16()?); + Ok(()) + } + test16(0)?; + test16(1)?; + test16(-1)?; + test16(32767)?; + test16(-32768)?; + Ok(()) + } + #[test] fn test_dump() { for l in 0..64 {