1 | use super::{LocalRb, SharedRb}; |
2 | use crate::utils::uninit_array; |
3 | use core::mem::MaybeUninit; |
4 | |
5 | #[cfg (feature = "alloc" )] |
6 | use alloc::{collections::TryReserveError, vec::Vec}; |
7 | |
8 | impl<T, const N: usize> Default for LocalRb<T, [MaybeUninit<T>; N]> { |
9 | fn default() -> Self { |
10 | unsafe { Self::from_raw_parts(container:uninit_array(), head:0, tail:0) } |
11 | } |
12 | } |
13 | impl<T, const N: usize> Default for SharedRb<T, [MaybeUninit<T>; N]> { |
14 | fn default() -> Self { |
15 | unsafe { Self::from_raw_parts(container:uninit_array(), head:0, tail:0) } |
16 | } |
17 | } |
18 | |
19 | #[cfg (feature = "alloc" )] |
20 | impl<T> LocalRb<T, Vec<MaybeUninit<T>>> { |
21 | /// Creates a new instance of a ring buffer. |
22 | /// |
23 | /// *Panics if `capacity` is zero.* |
24 | pub fn new(capacity: usize) -> Self { |
25 | let mut data: Vec> = Vec::new(); |
26 | data.resize_with(new_len:capacity, f:MaybeUninit::uninit); |
27 | unsafe { Self::from_raw_parts(container:data, head:0, tail:0) } |
28 | } |
29 | } |
30 | #[cfg (feature = "alloc" )] |
31 | impl<T> SharedRb<T, Vec<MaybeUninit<T>>> { |
32 | /// Creates a new instance of a ring buffer. |
33 | /// |
34 | /// *Panics if allocation failed or `capacity` is zero.* |
35 | pub fn new(capacity: usize) -> Self { |
36 | Self::try_new(capacity).unwrap() |
37 | } |
38 | |
39 | /// Creates a new instance of a ring buffer returning an error if allocation failed. |
40 | /// |
41 | /// *Panics if `capacity` is zero.* |
42 | pub fn try_new(capacity: usize) -> Result<Self, TryReserveError> { |
43 | let mut data: Vec> = Vec::new(); |
44 | data.try_reserve_exact(additional:capacity)?; |
45 | data.resize_with(new_len:capacity, f:MaybeUninit::uninit); |
46 | Ok(unsafe { Self::from_raw_parts(container:data, head:0, tail:0) }) |
47 | } |
48 | } |
49 | |