1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::cell::UnsafeCell;
/// Backing buffer for a queue, allocated on the heap.
///
/// All queues require a backing buffer which implements the [`Storage`] trait. This buffer stores
/// values in a contiguous array allocated on the heap. Compare this to [`StackBuffer`] which uses
/// a contiguous array on the stack. Note that the [`asyncio`], [`blocking`] and [`nonblocking`]
/// queues move their buffer to the heap anyway.
///
/// # Example
///
/// ```
/// use mini_io_queue::nonblocking;
/// use mini_io_queue::storage::HeapBuffer;
///
/// let buffer: HeapBuffer<u8> = HeapBuffer::new(100);
/// let (reader, writer) = nonblocking::queue_from(buffer);
/// ```
///
/// [`Storage`]: crate::storage::Storage
/// [`StackBuffer`]: crate::storage::StackBuffer
/// [`asyncio`]: crate::asyncio
/// [`blocking`]: crate::blocking
/// [`nonblocking`]: crate::nonblocking
#[cfg_attr(docsrs, doc(cfg(feature = "heap-buffer")))]
pub struct HeapBuffer<T>(Box<[UnsafeCell<T>]>);
unsafe impl<T> Send for HeapBuffer<T> where T: Send {}
unsafe impl<T> Sync for HeapBuffer<T> where T: Sync {}
impl<T> HeapBuffer<T>
where
T: Default,
{
/// Creates a buffer with the provided capacity. All elements in the buffer will be initialized
/// to the item's default value.
pub fn new(capacity: usize) -> Self {
let mut vec = Vec::new();
vec.resize_with(capacity, Default::default);
HeapBuffer(vec.into_boxed_slice())
}
}
impl<T> AsRef<[UnsafeCell<T>]> for HeapBuffer<T> {
fn as_ref(&self) -> &[UnsafeCell<T>] {
self.0.as_ref()
}
}
impl<T> From<Box<[T]>> for HeapBuffer<T> {
fn from(boxed_slice: Box<[T]>) -> Self {
// Safety: UnsafeCell is repr(transparent) so this is safe.
// fixme: this might not be stable, ideally we should not rely on it.
let unsafe_cell_box: Box<[UnsafeCell<T>]> = unsafe { core::mem::transmute(boxed_slice) };
HeapBuffer(unsafe_cell_box)
}
}