1 | #![warn (rust_2018_idioms)] |
2 | |
3 | use bytes::buf::Buf; |
4 | use bytes::Bytes; |
5 | |
6 | #[test] |
7 | fn long_take() { |
8 | // Tests that get a take with a size greater than the buffer length will not |
9 | // overrun the buffer. Regression test for #138. |
10 | let buf = b"hello world" .take(100); |
11 | assert_eq!(11, buf.remaining()); |
12 | assert_eq!(b"hello world" , buf.chunk()); |
13 | } |
14 | |
15 | #[test] |
16 | fn take_copy_to_bytes() { |
17 | let mut abcd = Bytes::copy_from_slice(b"abcd" ); |
18 | let abcd_ptr = abcd.as_ptr(); |
19 | let mut take = (&mut abcd).take(2); |
20 | let a = take.copy_to_bytes(1); |
21 | assert_eq!(Bytes::copy_from_slice(b"a" ), a); |
22 | // assert `to_bytes` did not allocate |
23 | assert_eq!(abcd_ptr, a.as_ptr()); |
24 | assert_eq!(Bytes::copy_from_slice(b"bcd" ), abcd); |
25 | } |
26 | |
27 | #[test] |
28 | #[should_panic ] |
29 | fn take_copy_to_bytes_panics() { |
30 | let abcd = Bytes::copy_from_slice(b"abcd" ); |
31 | abcd.take(2).copy_to_bytes(3); |
32 | } |
33 | |