use super::traits::{Storage, StorageMut};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[expect(
clippy::unsafe_derive_deserialize,
reason = "VecStorage derive is safe for POD storage"
)]
pub struct VecStorage<T> {
data: Vec<T>,
}
impl<T> VecStorage<T> {
#[inline]
pub fn generate<F>(len: usize, mut f: F) -> Self
where
F: FnMut() -> T,
{
let mut data: Vec<T> = Vec::with_capacity(len);
for _ in 0..len {
data.push(f());
}
Self { data }
}
#[inline]
pub fn fill(len: usize, value: T) -> Self
where
T: Clone,
{
Self {
data: vec![value; len],
}
}
#[inline]
#[expect(clippy::uninit_vec, reason = "caller fully overwrites uninit storage")]
pub fn uninit(len: usize) -> Self {
let mut data = Vec::with_capacity(len);
unsafe {
data.set_len(len);
}
Self { data }
}
#[inline]
pub const fn new(data: Vec<T>) -> Self {
Self { data }
}
#[inline]
pub fn into_inner(self) -> Vec<T> {
self.data
}
}
impl<T> Storage<T> for VecStorage<T> {
#[inline]
fn as_slice(&self) -> &[T] {
&self.data
}
}
impl<T> StorageMut<T> for VecStorage<T> {
#[inline]
fn as_mut_slice(&mut self) -> &mut [T] {
&mut self.data
}
}