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

Add 'from_header_and_fn' #61

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
24 changes: 24 additions & 0 deletions src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ impl<H, T> Arc<HeaderSlice<H, [T]>> {
}
}

/// Creates an Arc for a HeaderSlice using the function to fill in its tail.
/// The resulting Arc will be fat.
pub fn from_header_and_fn(header: H, num_items:usize, fill: impl FnOnce(&mut [T])) -> Self
where
T: Copy,
{
assert_ne!(mem::size_of::<T>(), 0, "Need to think about ZST");

let inner = Arc::allocate_for_header_and_slice(num_items);

unsafe {
// Write the data.
ptr::write(&mut ((*inner.as_ptr()).data.header), header);
let dst = (*inner.as_ptr()).data.slice.as_mut_ptr();
let dst_slice = core::slice::from_raw_parts_mut(dst, num_items);
fill(dst_slice)
}

Arc {
p: inner,
phantom: PhantomData,
}
}

/// Creates an Arc for a HeaderSlice using the given header struct and
/// iterator to generate the slice. The resulting Arc will be fat.
pub fn from_header_and_slice(header: H, items: &[T]) -> Self
Expand Down