diff options
author | 2019-10-16 09:53:36 -0700 | |
---|---|---|
committer | 2019-10-16 09:53:36 -0700 | |
commit | 43ac8e5494e9404327f971937edb092b8cae6a2b (patch) | |
tree | 979b9a1107fa161a3d31f05bfa105f8e58e1b926 /tests/fuzz_bytes.rs | |
parent | ebe96021b0eaf52be1fedd0a925f4384275c9cc4 (diff) | |
download | bytes-43ac8e5494e9404327f971937edb092b8cae6a2b.tar.gz bytes-43ac8e5494e9404327f971937edb092b8cae6a2b.tar.zst bytes-43ac8e5494e9404327f971937edb092b8cae6a2b.zip |
Refactor Bytes to use an internal vtable (#298)
Bytes is a useful tool for managing multiple slices into the same region
of memory, and the other things it used to have been removed to reduce
complexity. The exact strategy for managing the multiple references is
no longer hard-coded, but instead backing by a customizable vtable.
- Removed ability to mutate the underlying memory from the `Bytes` type.
- Removed the "inline" (SBO) mechanism in `Bytes`. The reduces a large
amount of complexity, and improves performance when accessing the
slice of bytes, since a branch is no longer needed to check if the
data is inline.
- Removed `Bytes` knowledge of `BytesMut` (`BytesMut` may grow that
knowledge back at a future point.)
Diffstat (limited to 'tests/fuzz_bytes.rs')
-rw-r--r-- | tests/fuzz_bytes.rs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/fuzz_bytes.rs b/tests/fuzz_bytes.rs new file mode 100644 index 0000000..5f894dd --- /dev/null +++ b/tests/fuzz_bytes.rs @@ -0,0 +1,73 @@ +// pretend to like `crate::` +extern crate alloc; +#[path = "../src/buf/mod.rs"] +#[allow(warnings)] +mod buf; +#[path = "../src/debug.rs"] +#[allow(warnings)] +mod debug; +#[path = "../src/bytes.rs"] +#[allow(warnings)] +mod bytes; +#[path = "../src/bytes_mut.rs"] +#[allow(warnings)] +mod bytes_mut; +use std::process::abort; + +use self::buf::{Buf, BufMut}; +use self::bytes::Bytes; +use self::bytes_mut::BytesMut; + +use std::sync::Arc; +use loom; +use loom::thread; + +#[test] +fn bytes_cloning_vec() { + loom::model(|| { + let a = Bytes::from(b"abcdefgh".to_vec()); + let addr = a.as_ptr() as usize; + + // test the Bytes::clone is Sync by putting it in an Arc + let a1 = Arc::new(a); + let a2 = a1.clone(); + + let t1 = thread::spawn(move || { + let b: Bytes = (*a1).clone(); + assert_eq!(b.as_ptr() as usize, addr); + }); + + let t2 = thread::spawn(move || { + let b: Bytes = (*a2).clone(); + assert_eq!(b.as_ptr() as usize, addr); + }); + + t1.join().unwrap(); + t2.join().unwrap(); + }); +} + +#[test] +fn bytes_mut_cloning_frozen() { + loom::model(|| { + let a = BytesMut::from(&b"abcdefgh"[..]).split().freeze(); + let addr = a.as_ptr() as usize; + + // test the Bytes::clone is Sync by putting it in an Arc + let a1 = Arc::new(a); + let a2 = a1.clone(); + + let t1 = thread::spawn(move || { + let b: Bytes = (*a1).clone(); + assert_eq!(b.as_ptr() as usize, addr); + }); + + let t2 = thread::spawn(move || { + let b: Bytes = (*a2).clone(); + assert_eq!(b.as_ptr() as usize, addr); + }); + + t1.join().unwrap(); + t2.join().unwrap(); + }); +} |