Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce SharedBuf trait for Bytes VTable #596

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
fixing miri errors
rrichardson committed Feb 8, 2023
commit 7cc348470faf14576515b6dd179aef0e278cb1e5
29 changes: 25 additions & 4 deletions tests/extern_buf_bytes.rs
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ struct ExternBuf {
impl ExternBuf {
// We're pretending that this is some sort of exotic allocation/recycling scheme
pub fn from_size(sz: usize) -> Self {
let layout = Layout::from_size_align(sz, 4).unwrap();
let layout = Layout::from_size_align(sz, 1).unwrap();
let ptr = NonNull::new(unsafe { alloc(layout) }).unwrap();
let num = ptr.as_ptr() as usize;
println!("Alloc'd {}", num);
@@ -45,11 +45,11 @@ impl From<&[u8]> for ExternBuf {

impl Drop for ExternBuf {
fn drop(&mut self) {
let layout = Layout::from_size_align(self.cap, 4).unwrap();
let layout = Layout::from_size_align(self.cap, 1).unwrap();
unsafe {
let num = self.ptr.as_ptr() as usize;
println!("dealloc'ing {}", num);
dealloc(self.ptr.as_mut(), layout);
dealloc(self.ptr.as_ptr(), layout);
}
}
}
@@ -109,7 +109,7 @@ unsafe impl SharedBuf for ExternBufWrapper {
}

unsafe fn drop(data: &mut AtomicPtr<()>, _ptr: *const u8, _len: usize) {
let inner: *mut ExternBuf = (*data.get_mut()).cast();
let inner: *mut ExternBuf = (data.get_mut()).cast();
if (*inner).ref_count.fetch_sub(1, Ordering::Release) != 1 {
return;
}
@@ -175,6 +175,27 @@ fn roundtrip() {
assert_eq!(a2, b"abcdefgh"[..]);
}

#[test]
fn to_vec() {
let eb = ExternBuf::from(&b"abcdefgh"[..]);
let a = Bytes::from_shared_buf(eb.into_shared());
let v = Vec::from(a);
assert_eq!(v, b"abcdefgh"[..]);
}

#[test]
fn refer_madness() {
let eb = ExternBuf::from(&b"abcdefgh"[..]);
let a = Bytes::from_shared_buf(eb.into_shared());
let b = a.slice(..);
let c = b.slice(..);
let d = c.slice(..5);
let e = d.slice(1..3);
drop(d);
assert_eq!(e, b"bc"[..]);
}

#[ignore]
#[test]
fn from_slice() {
let eb1 = ExternBuf::from(&b"abcdefgh"[..]);