mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-14 00:00:13 +02:00
Write docs and remove unecessary fns and types
This commit is contained in:
+37
-25
@@ -17,7 +17,7 @@ fn test_bounds() {
|
||||
|
||||
#[test]
|
||||
fn from_slice() {
|
||||
let a = Bytes::from_slice(b"abcdefgh");
|
||||
let a = Bytes::from(&b"abcdefgh"[..]);
|
||||
assert_eq!(a, b"abcdefgh"[..]);
|
||||
assert_eq!(a, &b"abcdefgh"[..]);
|
||||
assert_eq!(a, Vec::from(&b"abcdefgh"[..]));
|
||||
@@ -25,7 +25,7 @@ fn from_slice() {
|
||||
assert_eq!(&b"abcdefgh"[..], a);
|
||||
assert_eq!(Vec::from(&b"abcdefgh"[..]), a);
|
||||
|
||||
let a = BytesMut::from_slice(b"abcdefgh");
|
||||
let a = BytesMut::from(&b"abcdefgh"[..]);
|
||||
assert_eq!(a, b"abcdefgh"[..]);
|
||||
assert_eq!(a, &b"abcdefgh"[..]);
|
||||
assert_eq!(a, Vec::from(&b"abcdefgh"[..]));
|
||||
@@ -36,39 +36,39 @@ fn from_slice() {
|
||||
|
||||
#[test]
|
||||
fn fmt() {
|
||||
let a = format!("{:?}", Bytes::from_slice(b"abcdefg"));
|
||||
let a = format!("{:?}", Bytes::from(&b"abcdefg"[..]));
|
||||
let b = format!("{:?}", b"abcdefg");
|
||||
|
||||
assert_eq!(a, b);
|
||||
|
||||
let a = format!("{:?}", BytesMut::from_slice(b"abcdefg"));
|
||||
let a = format!("{:?}", BytesMut::from(&b"abcdefg"[..]));
|
||||
assert_eq!(a, b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn len() {
|
||||
let a = Bytes::from_slice(b"abcdefg");
|
||||
let a = Bytes::from(&b"abcdefg"[..]);
|
||||
assert_eq!(a.len(), 7);
|
||||
|
||||
let a = BytesMut::from_slice(b"abcdefg");
|
||||
let a = BytesMut::from(&b"abcdefg"[..]);
|
||||
assert_eq!(a.len(), 7);
|
||||
|
||||
let a = Bytes::from_slice(b"");
|
||||
let a = Bytes::from(&b""[..]);
|
||||
assert!(a.is_empty());
|
||||
|
||||
let a = BytesMut::from_slice(b"");
|
||||
let a = BytesMut::from(&b""[..]);
|
||||
assert!(a.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn index() {
|
||||
let a = Bytes::from_slice(b"hello world");
|
||||
let a = Bytes::from(&b"hello world"[..]);
|
||||
assert_eq!(a[0..5], *b"hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slice() {
|
||||
let a = Bytes::from_slice(b"hello world");
|
||||
let a = Bytes::from(&b"hello world"[..]);
|
||||
|
||||
let b = a.slice(3, 5);
|
||||
assert_eq!(b, b"lo"[..]);
|
||||
@@ -83,26 +83,26 @@ fn slice() {
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn slice_oob_1() {
|
||||
let a = Bytes::from_slice(b"hello world");
|
||||
let a = Bytes::from(&b"hello world"[..]);
|
||||
a.slice(5, 25);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn slice_oob_2() {
|
||||
let a = Bytes::from_slice(b"hello world");
|
||||
let a = Bytes::from(&b"hello world"[..]);
|
||||
a.slice(25, 30);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_off() {
|
||||
let hello = Bytes::from_slice(b"helloworld");
|
||||
let hello = Bytes::from(&b"helloworld"[..]);
|
||||
let world = hello.split_off(5);
|
||||
|
||||
assert_eq!(hello, &b"hello"[..]);
|
||||
assert_eq!(world, &b"world"[..]);
|
||||
|
||||
let hello = BytesMut::from_slice(b"helloworld");
|
||||
let hello = BytesMut::from(&b"helloworld"[..]);
|
||||
let world = hello.split_off(5);
|
||||
|
||||
assert_eq!(hello, &b"hello"[..]);
|
||||
@@ -112,21 +112,21 @@ fn split_off() {
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn split_off_oob() {
|
||||
let hello = Bytes::from_slice(b"helloworld");
|
||||
let hello = Bytes::from(&b"helloworld"[..]);
|
||||
hello.split_off(25);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn split_off_oob_mut() {
|
||||
let hello = BytesMut::from_slice(b"helloworld");
|
||||
let hello = BytesMut::from(&b"helloworld"[..]);
|
||||
hello.split_off(25);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_off_uninitialized() {
|
||||
let bytes = BytesMut::with_capacity(1024);
|
||||
let other = bytes.split_off(128);
|
||||
let mut bytes = BytesMut::with_capacity(1024);
|
||||
let other = bytes.split_off_mut(128);
|
||||
|
||||
assert_eq!(bytes.len(), 0);
|
||||
assert_eq!(bytes.capacity(), 128);
|
||||
@@ -138,20 +138,20 @@ fn split_off_uninitialized() {
|
||||
#[test]
|
||||
fn drain_to_1() {
|
||||
// Inline
|
||||
let a = Bytes::from_slice(SHORT);
|
||||
let a = Bytes::from(SHORT);
|
||||
let b = a.drain_to(4);
|
||||
|
||||
assert_eq!(SHORT[4..], a);
|
||||
assert_eq!(SHORT[..4], b);
|
||||
|
||||
// Allocated
|
||||
let a = Bytes::from_slice(LONG);
|
||||
let a = Bytes::from(LONG);
|
||||
let b = a.drain_to(4);
|
||||
|
||||
assert_eq!(LONG[4..], a);
|
||||
assert_eq!(LONG[..4], b);
|
||||
|
||||
let a = Bytes::from_slice(LONG);
|
||||
let a = Bytes::from(LONG);
|
||||
let b = a.drain_to(30);
|
||||
|
||||
assert_eq!(LONG[30..], a);
|
||||
@@ -161,21 +161,21 @@ fn drain_to_1() {
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn drain_to_oob() {
|
||||
let hello = Bytes::from_slice(b"helloworld");
|
||||
let hello = Bytes::from(&b"helloworld"[..]);
|
||||
hello.drain_to(30);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn drain_to_oob_mut() {
|
||||
let hello = BytesMut::from_slice(b"helloworld");
|
||||
let hello = BytesMut::from(&b"helloworld"[..]);
|
||||
hello.drain_to(30);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drain_to_uninitialized() {
|
||||
let bytes = BytesMut::with_capacity(1024);
|
||||
let other = bytes.drain_to(128);
|
||||
let mut bytes = BytesMut::with_capacity(1024);
|
||||
let other = bytes.drain_to_mut(128);
|
||||
|
||||
assert_eq!(bytes.len(), 0);
|
||||
assert_eq!(bytes.capacity(), 896);
|
||||
@@ -183,3 +183,15 @@ fn drain_to_uninitialized() {
|
||||
assert_eq!(other.len(), 0);
|
||||
assert_eq!(other.capacity(), 128);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fns_defined_for_bytes_mut() {
|
||||
let mut bytes = BytesMut::from(&b"hello world"[..]);
|
||||
|
||||
bytes.as_ptr();
|
||||
bytes.as_mut_ptr();
|
||||
|
||||
// Iterator
|
||||
let v: Vec<u8> = bytes.iter().map(|b| *b).collect();
|
||||
assert_eq!(&v[..], &bytes[..]);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
extern crate bytes;
|
||||
extern crate byteorder;
|
||||
|
||||
use bytes::{Buf, BufMut, ByteBuf};
|
||||
use bytes::{BufMut, BytesMut};
|
||||
use std::usize;
|
||||
use std::fmt::Write;
|
||||
|
||||
@@ -49,10 +49,10 @@ fn test_put_u16() {
|
||||
|
||||
#[test]
|
||||
fn test_clone() {
|
||||
let mut buf = ByteBuf::with_capacity(100);
|
||||
let mut buf = BytesMut::with_capacity(100);
|
||||
buf.write_str("this is a test").unwrap();
|
||||
let buf2 = buf.clone();
|
||||
|
||||
buf.write_str(" of our emergecy broadcast system").unwrap();
|
||||
assert!(buf.bytes() != buf2.bytes());
|
||||
assert!(buf != buf2);
|
||||
}
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
extern crate bytes;
|
||||
|
||||
use bytes::{Buf, BufMut, SliceBuf};
|
||||
|
||||
#[test]
|
||||
fn test_initial_buf_empty() {
|
||||
let mut mem = [0u8; 100];
|
||||
let buf = SliceBuf::new(&mut mem[..]);
|
||||
|
||||
assert!(buf.capacity() == 100);
|
||||
assert!(buf.remaining_mut() == 100);
|
||||
assert!(buf.remaining() == 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_slice_buf_bytes() {
|
||||
let mut mem = [0u8; 32];
|
||||
let mut buf = SliceBuf::new(&mut mem[..]);
|
||||
|
||||
buf.copy_from(&b"hello "[..]);
|
||||
assert_eq!(&b"hello "[..], buf.bytes());
|
||||
|
||||
buf.copy_from(&b"world"[..]);
|
||||
assert_eq!(&b"hello world"[..], buf.bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_byte_buf_read_write() {
|
||||
let mut mem = [0u8; 32];
|
||||
let mut buf = SliceBuf::new(&mut mem[..]);
|
||||
|
||||
buf.copy_from(&b"hello world"[..]);
|
||||
assert_eq!(21, buf.remaining_mut());
|
||||
|
||||
buf.copy_from(&b" goodbye"[..]);
|
||||
assert_eq!(13, buf.remaining_mut());
|
||||
|
||||
let mut dst = [0; 5];
|
||||
|
||||
let pos = buf.position();
|
||||
buf.copy_to(&mut dst[..]);
|
||||
assert_eq!(b"hello", &dst);
|
||||
|
||||
buf.set_position(pos);
|
||||
buf.copy_to(&mut dst[..]);
|
||||
assert_eq!(b"hello", &dst);
|
||||
|
||||
buf.copy_to(&mut dst[..]);
|
||||
assert_eq!(b" worl", &dst);
|
||||
|
||||
let mut dst = [0; 2];
|
||||
buf.copy_to(&mut dst[..]);
|
||||
assert_eq!(b"d ", &dst);
|
||||
|
||||
let mut dst = [0; 7];
|
||||
buf.copy_to(&mut dst[..]);
|
||||
assert_eq!(b"goodbye", &dst);
|
||||
|
||||
assert_eq!(13, buf.remaining_mut());
|
||||
|
||||
buf.copy_from(&b" have fun"[..]);
|
||||
assert_eq!(4, buf.remaining_mut());
|
||||
|
||||
assert_eq!(buf.bytes(), b" have fun");
|
||||
|
||||
buf.set_position(0);
|
||||
assert_eq!(buf.bytes(), b"hello world goodbye have fun");
|
||||
|
||||
buf.clear();
|
||||
assert_eq!(buf.bytes(), b"");
|
||||
}
|
||||
Reference in New Issue
Block a user