mirror of
https://github.com/openharmony/third_party_rust_bytes.git
synced 2026-07-19 17:05:29 -04:00
3ede899c82
- All the `get_*` and `put_*` methods that take `T: ByteOrder` have a `where Self: Sized` bound added, so that they are only usable from sized types. It was impossible to make `Buf` or `BufMut` into trait objects before, so this change doesn't break anyone. - Add `get_n_be`/`get_n_le`/`put_n_be`/`put_n_le` methods that can be used on trait objects. - Deprecate the export of `ByteOrder` and methods generic on it. Fixes #163
59 lines
1.1 KiB
Rust
59 lines
1.1 KiB
Rust
extern crate bytes;
|
|
extern crate byteorder;
|
|
extern crate iovec;
|
|
|
|
use bytes::Buf;
|
|
use iovec::IoVec;
|
|
use std::io::Cursor;
|
|
|
|
#[test]
|
|
fn test_fresh_cursor_vec() {
|
|
let mut buf = Cursor::new(b"hello".to_vec());
|
|
|
|
assert_eq!(buf.remaining(), 5);
|
|
assert_eq!(buf.bytes(), b"hello");
|
|
|
|
buf.advance(2);
|
|
|
|
assert_eq!(buf.remaining(), 3);
|
|
assert_eq!(buf.bytes(), b"llo");
|
|
|
|
buf.advance(3);
|
|
|
|
assert_eq!(buf.remaining(), 0);
|
|
assert_eq!(buf.bytes(), b"");
|
|
}
|
|
|
|
#[test]
|
|
fn test_get_u8() {
|
|
let mut buf = Cursor::new(b"\x21zomg");
|
|
assert_eq!(0x21, buf.get_u8());
|
|
}
|
|
|
|
#[test]
|
|
fn test_get_u16() {
|
|
let buf = b"\x21\x54zomg";
|
|
assert_eq!(0x2154, Cursor::new(buf).get_u16_be());
|
|
assert_eq!(0x5421, Cursor::new(buf).get_u16_le());
|
|
}
|
|
|
|
#[test]
|
|
#[should_panic]
|
|
fn test_get_u16_buffer_underflow() {
|
|
let mut buf = Cursor::new(b"\x21");
|
|
buf.get_u16_be();
|
|
}
|
|
|
|
#[test]
|
|
fn test_bufs_vec() {
|
|
let buf = Cursor::new(b"hello world");
|
|
|
|
let b1: &[u8] = &mut [0];
|
|
let b2: &[u8] = &mut [0];
|
|
|
|
let mut dst: [&IoVec; 2] =
|
|
[b1.into(), b2.into()];
|
|
|
|
assert_eq!(1, buf.bytes_vec(&mut dst[..]));
|
|
}
|