Skip to content

Commit 80e5fe2

Browse files
stepancheglelongg
authored andcommitted
Clarify BufPut::put_int behavior (tokio-rs#486)
* writes low bytes, discards high bytes * panics if `nbytes` is greater than 8
1 parent 4522ab3 commit 80e5fe2

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
@@ -704,7 +704,7 @@ pub unsafe trait BufMut {
704704
self.put_slice(&n.to_le_bytes()[0..nbytes]);
705705
}
706706

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

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

tests/test_buf_mut.rs

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

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

0 commit comments

Comments
 (0)