macro_rules! rb_impl_init { ($type:ident) => { impl Default for $type> { fn default() -> Self { unsafe { Self::from_raw_parts(crate::utils::uninit_array().into(), usize::default(), usize::default()) } } } impl From<[T; N]> for $type> { fn from(value: [T; N]) -> Self { let (read, write) = (0, value.len()); unsafe { Self::from_raw_parts(crate::utils::array_to_uninit(value).into(), read, write) } } } #[cfg(feature = "alloc")] impl $type> { /// Creates a new instance of a ring buffer. /// /// *Panics if allocation failed, `capacity` is zero, or it exceeds `usize::MAX / 2`.* pub fn new(capacity: usize) -> Self { $crate::rb::utils::assert_capacity(capacity); unsafe { Self::from_raw_parts(crate::storage::Heap::::new(capacity), usize::default(), usize::default()) } } /// Creates a new instance of a ring buffer returning an error if allocation failed. /// /// *Panics if `capacity` is zero or exceeds `usize::MAX / 2`.* pub fn try_new(capacity: usize) -> Result { $crate::rb::utils::assert_capacity(capacity); let mut vec = alloc::vec::Vec::>::new(); vec.try_reserve_exact(capacity)?; unsafe { vec.set_len(capacity) }; Ok(unsafe { Self::from_raw_parts(vec.into_boxed_slice().into(), usize::default(), usize::default()) }) } } #[cfg(feature = "alloc")] impl From> for $type> { fn from(value: alloc::vec::Vec) -> Self { let (read, write) = (0, value.len()); unsafe { Self::from_raw_parts(crate::utils::vec_to_uninit(value).into(), read, write) } } } #[cfg(feature = "alloc")] impl From> for $type> { fn from(value: alloc::boxed::Box<[T]>) -> Self { let (read, write) = (0, value.len()); unsafe { Self::from_raw_parts(crate::utils::boxed_slice_to_uninit(value).into(), read, write) } } } }; } pub(crate) use rb_impl_init;