ringbuf/rb/
macros.rs
1macro_rules! rb_impl_init {
2 ($type:ident) => {
3 impl<T, const N: usize> Default for $type<crate::storage::Array<T, N>> {
4 fn default() -> Self {
5 unsafe { Self::from_raw_parts(crate::utils::uninit_array().into(), usize::default(), usize::default()) }
6 }
7 }
8
9 impl<T, const N: usize> From<[T; N]> for $type<crate::storage::Array<T, N>> {
10 fn from(value: [T; N]) -> Self {
11 let (read, write) = (0, value.len());
12 unsafe { Self::from_raw_parts(crate::utils::array_to_uninit(value).into(), read, write) }
13 }
14 }
15
16 #[cfg(feature = "alloc")]
17 impl<T> $type<crate::storage::Heap<T>> {
18 pub fn new(capacity: usize) -> Self {
22 Self::try_new(capacity).unwrap()
23 }
24 pub fn try_new(capacity: usize) -> Result<Self, alloc::collections::TryReserveError> {
28 let mut vec = alloc::vec::Vec::new();
29 vec.try_reserve_exact(capacity)?;
30 Ok(unsafe { Self::from_raw_parts(vec.into(), usize::default(), usize::default()) })
31 }
32 }
33
34 #[cfg(feature = "alloc")]
35 impl<T> From<alloc::vec::Vec<T>> for $type<crate::storage::Heap<T>> {
36 fn from(value: alloc::vec::Vec<T>) -> Self {
37 let (read, write) = (0, value.len());
38 unsafe { Self::from_raw_parts(crate::utils::vec_to_uninit(value).into(), read, write) }
39 }
40 }
41
42 #[cfg(feature = "alloc")]
43 impl<T> From<alloc::boxed::Box<[T]>> for $type<crate::storage::Heap<T>> {
44 fn from(value: alloc::boxed::Box<[T]>) -> Self {
45 let (read, write) = (0, value.len());
46 unsafe { Self::from_raw_parts(crate::utils::boxed_slice_to_uninit(value).into(), read, write) }
47 }
48 }
49 };
50}
51
52pub(crate) use rb_impl_init;