Skip to content

Commit

Permalink
Create StaticRb from filled array
Browse files Browse the repository at this point in the history
  • Loading branch information
agerasev committed Jan 22, 2024
1 parent 568b20b commit 812c7cd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/rb/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ macro_rules! rb_impl_init {
}
}

impl<T, const N: usize> From<[T; N]> for $type<crate::storage::Array<T, N>> {
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<T> $type<crate::storage::Heap<T>> {
/// Creates a new instance of a ring buffer.
Expand Down
29 changes: 25 additions & 4 deletions src/tests/init.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
use super::Rb;
use crate::{storage::Heap, traits::*};
#[cfg(feature = "alloc")]
use crate::storage::Heap;
use crate::traits::*;
#[cfg(feature = "alloc")]
use alloc::{boxed::Box, vec::Vec};

#[test]
fn from_array() {
let mut rb = Rb::from([123, 321]);
let (mut prod, mut cons) = rb.split_ref();

assert_eq!(prod.capacity().get(), 2);
assert_eq!(cons.occupied_len(), 2);

assert_eq!(prod.try_push(444), Err(444));

assert_eq!(cons.try_pop(), Some(123));
assert_eq!(cons.try_pop(), Some(321));
assert_eq!(cons.try_pop(), None);
}

#[cfg(feature = "alloc")]
#[test]
fn new() {
const CAP: usize = 2;
Expand All @@ -19,11 +38,12 @@ fn new() {
assert_eq!(cons.try_pop(), None);
}

#[cfg(feature = "alloc")]
#[test]
fn from_vec() {
let mut vec = Vec::with_capacity(2);
let mut vec = Vec::<i32>::with_capacity(2);
vec.push(123);
let rb = Rb::<Heap<i32>>::from(vec);
let rb = Rb::from(vec);
let (mut prod, mut cons) = rb.split();

assert_eq!(prod.capacity().get(), 2);
Expand All @@ -37,10 +57,11 @@ fn from_vec() {
assert_eq!(cons.try_pop(), None);
}

#[cfg(feature = "alloc")]
#[test]
fn from_boxed_slice() {
let boxed_slice = Box::new([123, 321]) as Box<[i32]>;
let rb = Rb::<Heap<i32>>::from(boxed_slice);
let rb = Rb::from(boxed_slice);
let (mut prod, mut cons) = rb.split();

assert_eq!(prod.capacity().get(), 2);
Expand Down
1 change: 0 additions & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ mod drop;
mod fmt_write;
mod frozen;
mod hold;
#[cfg(feature = "alloc")]
mod init;
mod iter;
mod overwrite;
Expand Down
6 changes: 6 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ pub unsafe fn write_uninit_slice<'a, T: Copy>(dst: &'a mut [T], src: &[MaybeUnin
dst
}

pub fn array_to_uninit<T, const N: usize>(value: [T; N]) -> [MaybeUninit<T>; N] {
let value = mem::ManuallyDrop::new(value);
let ptr = &value as *const _ as *const [MaybeUninit<T>; N];
unsafe { ptr.read() }
}

#[cfg(feature = "alloc")]
pub fn vec_to_uninit<T>(value: Vec<T>) -> Vec<MaybeUninit<T>> {
let value = mem::ManuallyDrop::new(value);
Expand Down

0 comments on commit 812c7cd

Please sign in to comment.