use super::{LocalRb, SharedRb}; use crate::utils::uninit_array; use core::mem::MaybeUninit; #[cfg(feature = "alloc")] use alloc::{collections::TryReserveError, vec::Vec}; impl Default for LocalRb; N]> { fn default() -> Self { unsafe { Self::from_raw_parts(uninit_array(), 0, 0) } } } impl Default for SharedRb; N]> { fn default() -> Self { unsafe { Self::from_raw_parts(uninit_array(), 0, 0) } } } #[cfg(feature = "alloc")] impl LocalRb>> { /// Creates a new instance of a ring buffer. /// /// *Panics if `capacity` is zero.* pub fn new(capacity: usize) -> Self { let mut data = Vec::new(); data.resize_with(capacity, MaybeUninit::uninit); unsafe { Self::from_raw_parts(data, 0, 0) } } } #[cfg(feature = "alloc")] impl SharedRb>> { /// Creates a new instance of a ring buffer. /// /// *Panics if allocation failed or `capacity` is zero.* pub fn new(capacity: usize) -> Self { Self::try_new(capacity).unwrap() } /// Creates a new instance of a ring buffer returning an error if allocation failed. /// /// *Panics if `capacity` is zero.* pub fn try_new(capacity: usize) -> Result { let mut data = Vec::new(); data.try_reserve_exact(capacity)?; data.resize_with(capacity, MaybeUninit::uninit); Ok(unsafe { Self::from_raw_parts(data, 0, 0) }) } }