2020-07-10 01:12:39 +09:00
|
|
|
#![warn(rust_2018_idioms)]
|
2016-11-01 08:14:31 -07:00
|
|
|
|
2020-05-22 13:17:30 +09:00
|
|
|
use bytes::{BufMut, BytesMut};
|
2020-05-21 19:18:20 -07:00
|
|
|
use core::fmt::Write;
|
2020-05-22 13:17:30 +09:00
|
|
|
use core::usize;
|
2016-08-31 12:07:44 -07:00
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 08:14:31 -07:00
|
|
|
fn test_vec_as_mut_buf() {
|
2016-08-31 12:07:44 -07:00
|
|
|
let mut buf = Vec::with_capacity(64);
|
|
|
|
|
|
2016-11-01 08:14:31 -07:00
|
|
|
assert_eq!(buf.remaining_mut(), usize::MAX);
|
2016-08-31 12:07:44 -07:00
|
|
|
|
2019-10-24 14:40:45 -07:00
|
|
|
assert!(buf.bytes_mut().len() >= 64);
|
2016-08-31 12:07:44 -07:00
|
|
|
|
2017-02-16 11:15:46 -08:00
|
|
|
buf.put(&b"zomg"[..]);
|
2016-08-31 12:07:44 -07:00
|
|
|
|
|
|
|
|
assert_eq!(&buf, b"zomg");
|
|
|
|
|
|
2016-11-01 08:14:31 -07:00
|
|
|
assert_eq!(buf.remaining_mut(), usize::MAX - 4);
|
2016-08-31 12:07:44 -07:00
|
|
|
assert_eq!(buf.capacity(), 64);
|
|
|
|
|
|
|
|
|
|
for _ in 0..16 {
|
2017-02-16 11:15:46 -08:00
|
|
|
buf.put(&b"zomg"[..]);
|
2016-08-31 12:07:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert_eq!(buf.len(), 68);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 08:14:31 -07:00
|
|
|
fn test_put_u8() {
|
2016-08-31 12:07:44 -07:00
|
|
|
let mut buf = Vec::with_capacity(8);
|
2019-08-27 22:09:43 +02:00
|
|
|
buf.put_u8(33);
|
2016-08-31 12:07:44 -07:00
|
|
|
assert_eq!(b"\x21", &buf[..]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2016-11-01 08:14:31 -07:00
|
|
|
fn test_put_u16() {
|
2016-08-31 12:07:44 -07:00
|
|
|
let mut buf = Vec::with_capacity(8);
|
2018-03-12 09:27:09 -07:00
|
|
|
buf.put_u16(8532);
|
2016-08-31 12:07:44 -07:00
|
|
|
assert_eq!(b"\x21\x54", &buf[..]);
|
|
|
|
|
|
|
|
|
|
buf.clear();
|
2018-03-12 09:27:09 -07:00
|
|
|
buf.put_u16_le(8532);
|
2016-08-31 12:07:44 -07:00
|
|
|
assert_eq!(b"\x54\x21", &buf[..]);
|
|
|
|
|
}
|
2016-11-21 15:43:59 -08:00
|
|
|
|
2017-04-30 16:14:54 -07:00
|
|
|
#[test]
|
2020-03-13 16:50:04 -05:00
|
|
|
#[should_panic(expected = "cannot advance")]
|
2017-04-30 16:14:54 -07:00
|
|
|
fn test_vec_advance_mut() {
|
2020-03-13 16:50:04 -05:00
|
|
|
// Verify fix for #354
|
2017-04-30 16:14:54 -07:00
|
|
|
let mut buf = Vec::with_capacity(8);
|
|
|
|
|
unsafe {
|
|
|
|
|
buf.advance_mut(12);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 15:43:59 -08:00
|
|
|
#[test]
|
|
|
|
|
fn test_clone() {
|
2017-02-15 14:48:49 -08:00
|
|
|
let mut buf = BytesMut::with_capacity(100);
|
2016-11-21 15:43:59 -08:00
|
|
|
buf.write_str("this is a test").unwrap();
|
|
|
|
|
let buf2 = buf.clone();
|
|
|
|
|
|
2019-07-09 18:02:23 +07:00
|
|
|
buf.write_str(" of our emergency broadcast system").unwrap();
|
2017-02-15 14:48:49 -08:00
|
|
|
assert!(buf != buf2);
|
2016-11-21 15:43:59 -08:00
|
|
|
}
|
2017-02-28 11:15:35 -08:00
|
|
|
|
2019-06-07 12:31:10 -07:00
|
|
|
#[test]
|
|
|
|
|
fn test_mut_slice() {
|
|
|
|
|
let mut v = vec![0, 0, 0, 0];
|
|
|
|
|
let mut s = &mut v[..];
|
|
|
|
|
s.put_u32(42);
|
|
|
|
|
}
|
2019-12-06 16:33:04 -08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_deref_bufmut_forwards() {
|
|
|
|
|
struct Special;
|
|
|
|
|
|
|
|
|
|
impl BufMut for Special {
|
|
|
|
|
fn remaining_mut(&self) -> usize {
|
|
|
|
|
unreachable!("remaining_mut");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn bytes_mut(&mut self) -> &mut [std::mem::MaybeUninit<u8>] {
|
|
|
|
|
unreachable!("bytes_mut");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe fn advance_mut(&mut self, _: usize) {
|
|
|
|
|
unreachable!("advance");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn put_u8(&mut self, _: u8) {
|
|
|
|
|
// specialized!
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// these should all use the specialized method
|
|
|
|
|
Special.put_u8(b'x');
|
|
|
|
|
(&mut Special as &mut dyn BufMut).put_u8(b'x');
|
|
|
|
|
(Box::new(Special) as Box<dyn BufMut>).put_u8(b'x');
|
|
|
|
|
Box::new(Special).put_u8(b'x');
|
|
|
|
|
}
|