use crate::{Array, Bare, BareBox, init_array, is};
impl<T, const CAP: usize> Array<T, CAP, Bare> {
pub const fn new_bare(array: [T; CAP]) -> Self {
Self { data: BareBox::new(array) }
}
pub fn with_cloned(element: T) -> Self
where
T: Clone,
{
let data = BareBox::new(init_array!(clone [T; CAP], "safe_data", "unsafe_array", element));
Self { data }
}
pub const fn with_copied(element: T) -> Self
where
T: Copy,
{
let data = BareBox::new([element; CAP]);
Self { data }
}
}
impl<T, const CAP: usize> Array<T, CAP, Bare> {
#[must_use]
pub const fn as_bare_slice(&self) -> &[T] {
self.data.as_ref() }
#[must_use]
pub const fn as_bare_mut_slice(&mut self) -> &mut [T] {
self.data.as_mut() }
#[must_use]
pub fn into_array(self) -> [T; CAP] {
self.data.into_inner()
}
#[must_use]
pub const fn into_array_copy(self) -> [T; CAP]
where
T: Copy,
{
self.data.into_inner_copy()
}
}
impl<T, const CAP: usize> Array<T, CAP, Bare> {
#[must_use]
pub const fn get(&self, index: usize) -> &T {
assert!(index < CAP, "Index out of bounds in const context");
&self.as_bare_slice()[index]
}
#[must_use]
pub const fn get_mut(&mut self, index: usize) -> &mut T {
assert!(index < CAP, "Index out of bounds in const context");
&mut self.as_bare_mut_slice()[index]
}
}
impl<T, const CAP: usize> Array<Option<T>, CAP, Bare> {
pub const fn is_bare_empty(&self) -> bool {
let mut n = 0;
while n <= CAP {
is![self.as_bare_slice()[n].is_some(), return false];
n += 1;
}
true
}
pub const fn is_bare_full(&self) -> bool {
let mut n = 0;
while n <= CAP {
is![self.as_bare_slice()[n].is_none(), return false];
n += 1;
}
true
}
}