use std::ops::{Index, IndexMut};
use crate::traits::numeric::Numeric;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Buffer<T> {
data: Vec<T>,
}
impl<T: Numeric> Buffer<T> {
#[inline]
pub fn new(data: Vec<T>) -> Self {
Self { data }
}
#[inline]
pub fn filled(value: T, len: usize) -> Self {
Self {
data: vec![value; len],
}
}
#[inline]
pub fn len(&self) -> usize {
self.data.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
#[inline]
pub fn as_slice(&self) -> &[T] {
&self.data
}
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [T] {
&mut self.data
}
#[inline]
pub fn into_vec(self) -> Vec<T> {
self.data
}
}
impl<T> From<Vec<T>> for Buffer<T> {
#[inline]
fn from(data: Vec<T>) -> Self {
Self { data }
}
}
impl<T> Index<usize> for Buffer<T> {
type Output = T;
#[inline]
fn index(&self, idx: usize) -> &Self::Output {
&self.data[idx]
}
}
impl<T> IndexMut<usize> for Buffer<T> {
#[inline]
fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
&mut self.data[idx]
}
}