Skip to content

Implement Bytes::from_raw_parts #684

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

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,53 @@ impl Bytes {
self.len -= by;
self.ptr = self.ptr.add(by);
}

/// Creates a new `Bytes` from a raw pointer and a length.
///
/// # Examples
/// To create a new `Bytes` where the start position and end position of the backing store are
/// aligned to 32-byte boundaries:
///
/// ```
/// use bytes::Bytes;
/// use std::alloc::{alloc, Layout, handle_alloc_error};
///
/// const LEN: usize = 64;
/// const ALIGN: usize = 32;
///
/// let layout = Layout::from_size_align(LEN, ALIGN)
/// .expect("failed to create Layout!")
/// .pad_to_align();
/// let ptr = unsafe { alloc(layout) };
/// if ptr.is_null() {
/// handle_alloc_error(layout);
/// };
///
/// // (Not shown here: Pass `ptr` to code that will write data into the buffer.
/// // For example, pass `ptr` to a system call to load data from disk using `O_DIRECT`.)
///
/// let buf = Bytes::from_raw_parts(ptr, layout.size());
/// assert_eq!(buf.len(), layout.size());
/// ```
pub fn from_raw_parts(ptr: *const u8, len: usize) -> Self {
let ptr = ptr as *mut u8;
if ptr as usize & 0x1 == 0 {
let data = ptr_map(ptr, |addr| addr | KIND_VEC);
Bytes {
ptr,
len,
data: AtomicPtr::new(data.cast()),
vtable: &PROMOTABLE_EVEN_VTABLE,
}
} else {
Bytes {
ptr,
len,
data: AtomicPtr::new(ptr.cast()),
vtable: &PROMOTABLE_ODD_VTABLE,
}
}
}
}

// Vtable must enforce this behavior
Expand Down