-
Notifications
You must be signed in to change notification settings - Fork 168
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
Implement the Buffer trait #908
Changes from 1 commit
c7de321
2e2417a
3731c53
5a34b01
7697e6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
//! Code for handling buffers to write into. | ||
|
||
#![allow(unsafe_code)] | ||
|
||
use core::mem::MaybeUninit; | ||
use core::slice; | ||
|
||
/// A buffer that the system can write into. | ||
pub unsafe trait Buffer<T: AnyBitPattern>: Sized { | ||
/// The result of the process operation. | ||
type Result; | ||
|
||
/// Convert this buffer into a pointer to a buffer and its capacity. | ||
fn as_buffer(&mut self) -> (*mut T, usize); | ||
|
||
/// Convert a finished buffer pointer into its result. | ||
unsafe fn finish(self, len: usize) -> Self::Result; | ||
} | ||
|
||
unsafe impl<T: AnyBitPattern> Buffer<T> for &mut [T] { | ||
type Result = usize; | ||
|
||
#[inline] | ||
fn as_buffer(&mut self) -> (*mut T, usize) { | ||
(self.as_mut_ptr(), self.len()) | ||
} | ||
|
||
#[inline] | ||
unsafe fn finish(self, len: usize) -> Self::Result { | ||
len | ||
} | ||
} | ||
|
||
unsafe impl<'a, T: AnyBitPattern> Buffer<T> for &'a mut [MaybeUninit<T>] { | ||
type Result = (&'a mut [T], &'a mut [MaybeUninit<T>]); | ||
|
||
#[inline] | ||
fn as_buffer(&mut self) -> (*mut T, usize) { | ||
(self.as_mut_ptr().cast(), self.len()) | ||
} | ||
|
||
#[inline] | ||
unsafe fn finish(self, len: usize) -> Self::Result { | ||
let (init, uninit) = self.split_at_mut(len); | ||
|
||
// SAFETY: The user asserts that the slice is now initialized. | ||
let init = slice::from_raw_parts_mut( | ||
init.as_mut_ptr().cast(), | ||
init.len() | ||
); | ||
|
||
(init, uninit) | ||
} | ||
} | ||
|
||
/// Implements [`Buffer`] around the `Vec` type. | ||
/// | ||
/// This implementation fills the buffer with data and sets the length. | ||
#[cfg(feature = "alloc")] | ||
unsafe impl<T: AnyBitPattern> Buffer<T> for alloc::vec::Vec<T> { | ||
type Result = alloc::vec::Vec<T>; | ||
|
||
#[inline] | ||
fn as_buffer(&mut self) -> (*mut T, usize) { | ||
(self.as_mut_ptr(), self.len()) | ||
} | ||
|
||
#[inline] | ||
unsafe fn finish(mut self, len: usize) -> Self::Result { | ||
self.set_len(len); | ||
self | ||
} | ||
} | ||
|
||
/// Types made up of plain-old-data. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this is supposed to be equivalent to requirements of the Bytemuck distinguishes that from its There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A secondary goal for this trait is to act as a buffer for |
||
/// | ||
/// # Safety | ||
/// | ||
/// - The OS can write any byte pattern to this structure. | ||
/// - This type does not implement `Drop`. | ||
pub unsafe trait AnyBitPattern {} | ||
|
||
macro_rules! impl_pod { | ||
($($ty:ty),*) => { | ||
$( | ||
unsafe impl AnyBitPattern for $ty {} | ||
)* | ||
} | ||
} | ||
|
||
impl_pod! { | ||
u8, i8, u16, i16, u32, i32, u64, i64, | ||
usize, isize | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could get away with using
self.capacity()
here rather than justself.len()
, which would make this approach more attractive because one could pass in aVec::with_capacity(n)
and read into it without initializing it first, or take an existingVec
,clear()
it, and then reuse the existing memory directly.Another option would be to use
.
spare_capacity_mut
().as_buffer()
, and say the behavior here is to append elements to theVec
rather than rewriting the wholeVec
. That seems nice forread_to_end
kinds of use cases. On the other hand, I wonder if that's too subtle. Given av: Vec<T>
, passing inv
would append, while&mut v
would start from the beginning. And passing&v
to write would still start from the beginning.