mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-24 00:00:14 +02:00
Clarify BufPut::put_int behavior (#486)
* writes low bytes, discards high bytes * panics if `nbytes` is greater than 8
This commit is contained in:
+6
-6
@@ -703,7 +703,7 @@ pub unsafe trait BufMut {
|
|||||||
self.put_slice(&n.to_le_bytes()[0..nbytes]);
|
self.put_slice(&n.to_le_bytes()[0..nbytes]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Writes a signed n-byte integer to `self` in big-endian byte order.
|
/// Writes low `nbytes` of a signed integer to `self` in big-endian byte order.
|
||||||
///
|
///
|
||||||
/// The current position is advanced by `nbytes`.
|
/// The current position is advanced by `nbytes`.
|
||||||
///
|
///
|
||||||
@@ -713,19 +713,19 @@ pub unsafe trait BufMut {
|
|||||||
/// use bytes::BufMut;
|
/// use bytes::BufMut;
|
||||||
///
|
///
|
||||||
/// let mut buf = vec![];
|
/// let mut buf = vec![];
|
||||||
/// buf.put_int(0x010203, 3);
|
/// buf.put_int(0x0504010203, 3);
|
||||||
/// assert_eq!(buf, b"\x01\x02\x03");
|
/// assert_eq!(buf, b"\x01\x02\x03");
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// This function panics if there is not enough remaining capacity in
|
/// This function panics if there is not enough remaining capacity in
|
||||||
/// `self`.
|
/// `self` or if `nbytes` is greater than 8.
|
||||||
fn put_int(&mut self, n: i64, nbytes: usize) {
|
fn put_int(&mut self, n: i64, nbytes: usize) {
|
||||||
self.put_slice(&n.to_be_bytes()[mem::size_of_val(&n) - nbytes..]);
|
self.put_slice(&n.to_be_bytes()[mem::size_of_val(&n) - nbytes..]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Writes a signed n-byte integer to `self` in little-endian byte order.
|
/// Writes low `nbytes` of a signed integer to `self` in little-endian byte order.
|
||||||
///
|
///
|
||||||
/// The current position is advanced by `nbytes`.
|
/// The current position is advanced by `nbytes`.
|
||||||
///
|
///
|
||||||
@@ -735,14 +735,14 @@ pub unsafe trait BufMut {
|
|||||||
/// use bytes::BufMut;
|
/// use bytes::BufMut;
|
||||||
///
|
///
|
||||||
/// let mut buf = vec![];
|
/// let mut buf = vec![];
|
||||||
/// buf.put_int_le(0x010203, 3);
|
/// buf.put_int_le(0x0504010203, 3);
|
||||||
/// assert_eq!(buf, b"\x03\x02\x01");
|
/// assert_eq!(buf, b"\x03\x02\x01");
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// This function panics if there is not enough remaining capacity in
|
/// This function panics if there is not enough remaining capacity in
|
||||||
/// `self`.
|
/// `self` or if `nbytes` is greater than 8.
|
||||||
fn put_int_le(&mut self, n: i64, nbytes: usize) {
|
fn put_int_le(&mut self, n: i64, nbytes: usize) {
|
||||||
self.put_slice(&n.to_le_bytes()[0..nbytes]);
|
self.put_slice(&n.to_le_bytes()[0..nbytes]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,6 +45,34 @@ fn test_put_u16() {
|
|||||||
assert_eq!(b"\x54\x21", &buf[..]);
|
assert_eq!(b"\x54\x21", &buf[..]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_put_int() {
|
||||||
|
let mut buf = Vec::with_capacity(8);
|
||||||
|
buf.put_int(0x1020304050607080, 3);
|
||||||
|
assert_eq!(b"\x60\x70\x80", &buf[..]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn test_put_int_nbytes_overflow() {
|
||||||
|
let mut buf = Vec::with_capacity(8);
|
||||||
|
buf.put_int(0x1020304050607080, 9);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_put_int_le() {
|
||||||
|
let mut buf = Vec::with_capacity(8);
|
||||||
|
buf.put_int_le(0x1020304050607080, 3);
|
||||||
|
assert_eq!(b"\x80\x70\x60", &buf[..]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn test_put_int_le_nbytes_overflow() {
|
||||||
|
let mut buf = Vec::with_capacity(8);
|
||||||
|
buf.put_int_le(0x1020304050607080, 9);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic(expected = "cannot advance")]
|
#[should_panic(expected = "cannot advance")]
|
||||||
fn test_vec_advance_mut() {
|
fn test_vec_advance_mut() {
|
||||||
|
|||||||
Reference in New Issue
Block a user