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

feat: Unsafe constructors for producers and consumers #99

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
48 changes: 37 additions & 11 deletions core/src/bbbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,7 @@ impl<'a, const N: usize> BBBuffer<N> {
let nn1 = NonNull::new_unchecked(self as *const _ as *mut _);
let nn2 = NonNull::new_unchecked(self as *const _ as *mut _);

Ok((
Producer {
bbq: nn1,
pd: PhantomData,
},
Consumer {
bbq: nn2,
pd: PhantomData,
},
))
Ok((Producer::new_unchecked(nn1), Consumer::new_unchecked(nn2)))
}
}

Expand All @@ -127,7 +118,12 @@ impl<'a, const N: usize> BBBuffer<N> {
/// section while splitting.
pub fn try_split_framed(&'a self) -> Result<(FrameProducer<'a, N>, FrameConsumer<'a, N>)> {
let (producer, consumer) = self.try_split()?;
Ok((FrameProducer { producer }, FrameConsumer { consumer }))
unsafe {
Ok((
FrameProducer::new_unchecked(producer),
FrameConsumer::new_unchecked(consumer),
))
}
}

/// Attempt to release the Producer and Consumer
Expand Down Expand Up @@ -313,6 +309,21 @@ pub struct Producer<'a, const N: usize> {
unsafe impl<'a, const N: usize> Send for Producer<'a, N> {}

impl<'a, const N: usize> Producer<'a, N> {
/// Create a `Producer` from a non null pointer to a [`BBBuffer`].
///
/// # Safety
///
/// The caller must ensure the [`BBBuffer`] was previously initialized. See
/// [`BBBUffer::try_split`]
///
/// [`BBBuffer`]: crate::BBBuffer
/// [`BBBuffer::try_split`]: crate::BBBuffer::try_split
pub unsafe fn new_unchecked(bbq: NonNull<BBBuffer<N>>) -> Self {
Self {
bbq,
pd: PhantomData,
}
}
/// Request a writable, contiguous section of memory of exactly
/// `sz` bytes. If the buffer size requested is not available,
/// an error will be returned.
Expand Down Expand Up @@ -518,6 +529,21 @@ pub struct Consumer<'a, const N: usize> {
unsafe impl<'a, const N: usize> Send for Consumer<'a, N> {}

impl<'a, const N: usize> Consumer<'a, N> {
/// Create a `Consumer` from a non null pointer to a [`BBBuffer`].
///
/// # Safety
///
/// The caller must ensure the [`BBBuffer`] was previously initialized. See
/// [`BBBUffer::try_split`]
///
/// [`BBBuffer`]: crate::BBBuffer
/// [`BBBuffer::try_split`]: crate::BBBuffer::try_split
pub unsafe fn new_unchecked(bbq: NonNull<BBBuffer<N>>) -> Self {
Self {
bbq,
pd: PhantomData,
}
}
/// Obtains a contiguous slice of committed bytes. This slice may not
/// contain ALL available bytes, if the writer has wrapped around. The
/// remaining bytes will be available after all readable bytes are
Expand Down
24 changes: 24 additions & 0 deletions core/src/framed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ pub struct FrameProducer<'a, const N: usize> {
}

impl<'a, const N: usize> FrameProducer<'a, N> {
/// Create a `FrameProducer` from a non null pointer to a [`BBBuffer`].
///
/// # Safety
///
/// The caller must ensure the [`BBBuffer`] was previously initialized. See
/// [`BBBUffer::try_split_framed`]
///
/// [`BBBuffer`]: crate::BBBuffer
/// [`BBBuffer::try_split_framed`]: crate::BBBuffer::try_split_framed
pub unsafe fn new_unchecked(producer: Producer<'a, N>) -> Self {
Self { producer }
}
/// Receive a grant for a frame with a maximum size of `max_sz` in bytes.
///
/// This size does not include the size of the frame header. The exact size
Expand All @@ -107,6 +119,18 @@ pub struct FrameConsumer<'a, const N: usize> {
}

impl<'a, const N: usize> FrameConsumer<'a, N> {
/// Create a `FrameConsumer` from a non null pointer to a [`BBBuffer`].
///
/// # Safety
///
/// The caller must ensure the [`BBBuffer`] was previously initialized. See
/// [`BBBUffer::try_split_framed`]
///
/// [`BBBuffer`]: crate::BBBuffer
/// [`BBBuffer::try_split_framed`]: crate::BBBuffer::try_split_framed
pub unsafe fn new_unchecked(consumer: Consumer<'a, N>) -> Self {
Self { consumer }
}
/// Obtain the next available frame, if any
pub fn read(&mut self) -> Option<FrameGrantR<'a, N>> {
// Get all available bytes. We never wrap a frame around,
Expand Down