Skip to content

Commit fa9cbf1

Browse files
authored
Clarify BufPut::put_int behavior (#486)
* writes low bytes, discards high bytes * panics if `nbytes` is greater than 8
1 parent ab8e3c0 commit fa9cbf1

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/buf/buf_mut.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ pub unsafe trait BufMut {
703703
self.put_slice(&n.to_le_bytes()[0..nbytes]);
704704
}
705705

706-
/// Writes a signed n-byte integer to `self` in big-endian byte order.
706+
/// Writes low `nbytes` of a signed integer to `self` in big-endian byte order.
707707
///
708708
/// The current position is advanced by `nbytes`.
709709
///
@@ -713,19 +713,19 @@ pub unsafe trait BufMut {
713713
/// use bytes::BufMut;
714714
///
715715
/// let mut buf = vec![];
716-
/// buf.put_int(0x010203, 3);
716+
/// buf.put_int(0x0504010203, 3);
717717
/// assert_eq!(buf, b"\x01\x02\x03");
718718
/// ```
719719
///
720720
/// # Panics
721721
///
722722
/// This function panics if there is not enough remaining capacity in
723-
/// `self`.
723+
/// `self` or if `nbytes` is greater than 8.
724724
fn put_int(&mut self, n: i64, nbytes: usize) {
725725
self.put_slice(&n.to_be_bytes()[mem::size_of_val(&n) - nbytes..]);
726726
}
727727

728-
/// Writes a signed n-byte integer to `self` in little-endian byte order.
728+
/// Writes low `nbytes` of a signed integer to `self` in little-endian byte order.
729729
///
730730
/// The current position is advanced by `nbytes`.
731731
///
@@ -735,14 +735,14 @@ pub unsafe trait BufMut {
735735
/// use bytes::BufMut;
736736
///
737737
/// let mut buf = vec![];
738-
/// buf.put_int_le(0x010203, 3);
738+
/// buf.put_int_le(0x0504010203, 3);
739739
/// assert_eq!(buf, b"\x03\x02\x01");
740740
/// ```
741741
///
742742
/// # Panics
743743
///
744744
/// This function panics if there is not enough remaining capacity in
745-
/// `self`.
745+
/// `self` or if `nbytes` is greater than 8.
746746
fn put_int_le(&mut self, n: i64, nbytes: usize) {
747747
self.put_slice(&n.to_le_bytes()[0..nbytes]);
748748
}

tests/test_buf_mut.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,34 @@ fn test_put_u16() {
4545
assert_eq!(b"\x54\x21", &buf[..]);
4646
}
4747

48+
#[test]
49+
fn test_put_int() {
50+
let mut buf = Vec::with_capacity(8);
51+
buf.put_int(0x1020304050607080, 3);
52+
assert_eq!(b"\x60\x70\x80", &buf[..]);
53+
}
54+
55+
#[test]
56+
#[should_panic]
57+
fn test_put_int_nbytes_overflow() {
58+
let mut buf = Vec::with_capacity(8);
59+
buf.put_int(0x1020304050607080, 9);
60+
}
61+
62+
#[test]
63+
fn test_put_int_le() {
64+
let mut buf = Vec::with_capacity(8);
65+
buf.put_int_le(0x1020304050607080, 3);
66+
assert_eq!(b"\x80\x70\x60", &buf[..]);
67+
}
68+
69+
#[test]
70+
#[should_panic]
71+
fn test_put_int_le_nbytes_overflow() {
72+
let mut buf = Vec::with_capacity(8);
73+
buf.put_int_le(0x1020304050607080, 9);
74+
}
75+
4876
#[test]
4977
#[should_panic(expected = "cannot advance")]
5078
fn test_vec_advance_mut() {

0 commit comments

Comments
 (0)