Files

156 lines
3.9 KiB
Rust
Raw Permalink Normal View History

#![warn(rust_2018_idioms)]
2017-02-28 18:33:34 -08:00
2020-05-22 13:17:30 +09:00
use bytes::{Buf, BufMut, Bytes};
2020-05-21 19:18:20 -07:00
#[cfg(feature = "std")]
2019-06-07 13:10:54 -07:00
use std::io::IoSlice;
2017-02-28 18:33:34 -08:00
#[test]
fn collect_two_bufs() {
let a = Bytes::from(&b"hello"[..]);
let b = Bytes::from(&b"world"[..]);
2017-02-28 18:33:34 -08:00
2020-10-20 11:00:35 -07:00
let res = a.chain(b).copy_to_bytes(10);
2017-02-28 18:33:34 -08:00
assert_eq!(res, &b"helloworld"[..]);
}
#[test]
fn writing_chained() {
let mut a = [0u8; 64];
let mut b = [0u8; 64];
2017-02-28 18:33:34 -08:00
{
let mut buf = (&mut a[..]).chain_mut(&mut b[..]);
2017-02-28 18:33:34 -08:00
2019-08-27 22:09:43 +02:00
for i in 0u8..128 {
buf.put_u8(i);
2017-02-28 18:33:34 -08:00
}
}
for i in 0..64 {
let expect = i as u8;
assert_eq!(expect, a[i]);
assert_eq!(expect + 64, b[i]);
}
}
#[test]
fn iterating_two_bufs() {
let a = Bytes::from(&b"hello"[..]);
let b = Bytes::from(&b"world"[..]);
2017-02-28 18:33:34 -08:00
let res: Vec<u8> = a.chain(b).into_iter().collect();
2017-02-28 18:33:34 -08:00
assert_eq!(res, &b"helloworld"[..]);
}
2020-05-21 19:18:20 -07:00
#[cfg(feature = "std")]
2017-02-28 18:33:34 -08:00
#[test]
fn vectored_read() {
let a = Bytes::from(&b"hello"[..]);
let b = Bytes::from(&b"world"[..]);
2017-02-28 18:33:34 -08:00
let mut buf = a.chain(b);
2017-03-19 09:08:37 -07:00
{
2019-06-07 13:10:54 -07:00
let b1: &[u8] = &mut [];
let b2: &[u8] = &mut [];
let b3: &[u8] = &mut [];
let b4: &[u8] = &mut [];
let mut iovecs = [
IoSlice::new(b1),
IoSlice::new(b2),
IoSlice::new(b3),
IoSlice::new(b4),
];
assert_eq!(2, buf.chunks_vectored(&mut iovecs));
2017-03-19 09:08:37 -07:00
assert_eq!(iovecs[0][..], b"hello"[..]);
assert_eq!(iovecs[1][..], b"world"[..]);
2019-06-07 13:10:54 -07:00
assert_eq!(iovecs[2][..], b""[..]);
assert_eq!(iovecs[3][..], b""[..]);
2017-03-19 09:08:37 -07:00
}
2017-02-28 18:33:34 -08:00
buf.advance(2);
2017-03-19 09:08:37 -07:00
{
2019-06-07 13:10:54 -07:00
let b1: &[u8] = &mut [];
let b2: &[u8] = &mut [];
let b3: &[u8] = &mut [];
let b4: &[u8] = &mut [];
let mut iovecs = [
IoSlice::new(b1),
IoSlice::new(b2),
IoSlice::new(b3),
IoSlice::new(b4),
];
assert_eq!(2, buf.chunks_vectored(&mut iovecs));
2017-03-19 09:08:37 -07:00
assert_eq!(iovecs[0][..], b"llo"[..]);
assert_eq!(iovecs[1][..], b"world"[..]);
2019-06-07 13:10:54 -07:00
assert_eq!(iovecs[2][..], b""[..]);
assert_eq!(iovecs[3][..], b""[..]);
2017-03-19 09:08:37 -07:00
}
2017-02-28 18:33:34 -08:00
buf.advance(3);
2017-03-19 09:08:37 -07:00
{
2019-06-07 13:10:54 -07:00
let b1: &[u8] = &mut [];
let b2: &[u8] = &mut [];
let b3: &[u8] = &mut [];
let b4: &[u8] = &mut [];
let mut iovecs = [
IoSlice::new(b1),
IoSlice::new(b2),
IoSlice::new(b3),
IoSlice::new(b4),
];
assert_eq!(1, buf.chunks_vectored(&mut iovecs));
2017-03-19 09:08:37 -07:00
assert_eq!(iovecs[0][..], b"world"[..]);
2019-06-07 13:10:54 -07:00
assert_eq!(iovecs[1][..], b""[..]);
assert_eq!(iovecs[2][..], b""[..]);
assert_eq!(iovecs[3][..], b""[..]);
2017-03-19 09:08:37 -07:00
}
2017-02-28 18:33:34 -08:00
buf.advance(3);
2017-03-19 09:08:37 -07:00
{
2019-06-07 13:10:54 -07:00
let b1: &[u8] = &mut [];
let b2: &[u8] = &mut [];
let b3: &[u8] = &mut [];
let b4: &[u8] = &mut [];
let mut iovecs = [
IoSlice::new(b1),
IoSlice::new(b2),
IoSlice::new(b3),
IoSlice::new(b4),
];
assert_eq!(1, buf.chunks_vectored(&mut iovecs));
2017-03-19 09:08:37 -07:00
assert_eq!(iovecs[0][..], b"ld"[..]);
2019-06-07 13:10:54 -07:00
assert_eq!(iovecs[1][..], b""[..]);
assert_eq!(iovecs[2][..], b""[..]);
assert_eq!(iovecs[3][..], b""[..]);
2017-03-19 09:08:37 -07:00
}
2017-02-28 18:33:34 -08:00
}
#[test]
fn chain_get_bytes() {
let mut ab = Bytes::copy_from_slice(b"ab");
let mut cd = Bytes::copy_from_slice(b"cd");
let ab_ptr = ab.as_ptr();
let cd_ptr = cd.as_ptr();
let mut chain = (&mut ab).chain(&mut cd);
let a = chain.copy_to_bytes(1);
let bc = chain.copy_to_bytes(2);
let d = chain.copy_to_bytes(1);
assert_eq!(Bytes::copy_from_slice(b"a"), a);
assert_eq!(Bytes::copy_from_slice(b"bc"), bc);
assert_eq!(Bytes::copy_from_slice(b"d"), d);
// assert `get_bytes` did not allocate
assert_eq!(ab_ptr, a.as_ptr());
// assert `get_bytes` did not allocate
assert_eq!(cd_ptr.wrapping_offset(1), d.as_ptr());
}