rusanta_core/memory/
buffer.rs1use std::ops::{Index, IndexMut};
2use crate::traits::numeric::Numeric;
3
4#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct Buffer<T> {
10 data: Vec<T>,
11}
12
13impl<T: Numeric> Buffer<T> {
14 #[inline]
16 pub fn new(data: Vec<T>) -> Self {
17 Self { data }
18 }
19
20 #[inline]
22 pub fn filled(value: T, len: usize) -> Self {
23 Self {
24 data: vec![value; len],
25 }
26 }
27
28 #[inline]
30 pub fn len(&self) -> usize {
31 self.data.len()
32 }
33
34 #[inline]
36 pub fn is_empty(&self) -> bool {
37 self.data.is_empty()
38 }
39
40 #[inline]
42 pub fn as_slice(&self) -> &[T] {
43 &self.data
44 }
45
46 #[inline]
48 pub fn as_mut_slice(&mut self) -> &mut [T] {
49 &mut self.data
50 }
51
52 #[inline]
54 pub fn into_vec(self) -> Vec<T> {
55 self.data
56 }
57}
58
59impl<T> From<Vec<T>> for Buffer<T> {
64 #[inline]
65 fn from(data: Vec<T>) -> Self {
66 Self { data }
67 }
68}
69
70impl<T> Index<usize> for Buffer<T> {
71 type Output = T;
72
73 #[inline]
74 fn index(&self, idx: usize) -> &Self::Output {
75 &self.data[idx]
76 }
77}
78
79impl<T> IndexMut<usize> for Buffer<T> {
80 #[inline]
81 fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
82 &mut self.data[idx]
83 }
84}