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 src/buf/buf_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2084,7 +2084,7 @@ pub trait Buf {
///
/// This function panics if `nbytes` is greater than 8.
fn try_get_int(&mut self, nbytes: usize) -> Result<i64, TryGetError> {
buf_try_get_impl!(be => self, i64, nbytes);
Ok(sign_extend(self.try_get_uint(nbytes)?, nbytes))
}

/// Gets a signed n-byte integer from `self` in little-endian byte order.
Expand Down Expand Up @@ -2116,7 +2116,7 @@ pub trait Buf {
///
/// This function panics if `nbytes` is greater than 8.
fn try_get_int_le(&mut self, nbytes: usize) -> Result<i64, TryGetError> {
buf_try_get_impl!(le => self, i64, nbytes);
Ok(sign_extend(self.try_get_uint_le(nbytes)?, nbytes))
}

/// Gets a signed n-byte integer from `self` in native-endian byte order.
Expand Down
32 changes: 32 additions & 0 deletions tests/test_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,35 @@ fn copy_to_bytes_mut() {
let bytes_mut2 = BytesMut::from(ret);
assert_eq!(bytes_mut2.as_ptr(), ptr);
}

#[test]
fn try_get_int_sign_extends() {
use ::bytes::TryGetError;
// 3-byte big-endian 0xffffff == -1; must match get_int
let mut a = &[0xff, 0xff, 0xff][..];
assert_eq!(a.try_get_int(3), Ok(-1));
assert_eq!(a.remaining(), 0);
let mut ctrl = &[0xff, 0xff, 0xff][..];
assert_eq!(ctrl.get_int(3), -1);
// little-endian variant
let mut le = &[0xff, 0xff, 0xff][..];
assert_eq!(le.try_get_int_le(3), Ok(-1));
// native-endian delegates
let mut ne = &[0xff, 0xff, 0xff][..];
assert_eq!(ne.try_get_int_ne(3), Ok(-1));
// positive value unchanged
let mut pos = &[0x01, 0x02, 0x03][..];
assert_eq!(pos.try_get_int(3), Ok(0x010203));
// full width
let mut full = &[0xff; 8][..];
assert_eq!(full.try_get_int(8), Ok(-1));
// insufficient data still errors with the documented shape
let mut short = &[0x01, 0x02, 0x03][..];
assert_eq!(
short.try_get_int(4),
Err(TryGetError {
requested: 4,
available: 3
})
);
}
Loading