1use std::ops::Index;
5use std::vec::Vec;
6
7#[cfg(feature = "serde")]
8use serde::{Deserialize, Serialize};
9
10#[derive(Debug, Clone, PartialEq, Eq)]
12#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
13#[cfg_attr(
14 feature = "serde",
15 serde(bound(serialize = "T: Serialize", deserialize = "T: Deserialize<'de>"))
16)]
17pub struct NeoArray<T> {
18 data: Vec<T>,
19}
20
21impl<T> NeoArray<T> {
22 pub fn new() -> Self {
23 Self { data: Vec::new() }
24 }
25
26 pub fn with_capacity(capacity: usize) -> Self {
27 Self {
28 data: Vec::with_capacity(capacity),
29 }
30 }
31
32 pub fn from_vec(data: Vec<T>) -> Self {
33 Self { data }
34 }
35
36 pub fn push(&mut self, item: T) {
37 self.data.push(item);
38 }
39
40 pub fn pop(&mut self) -> Option<T> {
41 self.data.pop()
42 }
43
44 pub fn len(&self) -> usize {
45 self.data.len()
46 }
47
48 pub fn is_empty(&self) -> bool {
49 self.data.is_empty()
50 }
51
52 pub fn get(&self, index: usize) -> Option<&T> {
53 self.data.get(index)
54 }
55
56 pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
57 self.data.get_mut(index)
58 }
59
60 pub fn iter(&self) -> impl Iterator<Item = &T> {
61 self.data.iter()
62 }
63}
64
65impl<T> Default for NeoArray<T> {
66 fn default() -> Self {
67 Self::new()
68 }
69}
70
71impl<T> From<Vec<T>> for NeoArray<T> {
72 fn from(data: Vec<T>) -> Self {
73 Self { data }
74 }
75}
76
77impl<T> FromIterator<T> for NeoArray<T> {
78 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
79 Self {
80 data: Vec::from_iter(iter),
81 }
82 }
83}
84
85impl<T> Extend<T> for NeoArray<T> {
86 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
87 self.data.extend(iter);
88 }
89}
90
91impl<T> Index<usize> for NeoArray<T> {
92 type Output = T;
93 fn index(&self, index: usize) -> &T {
94 &self.data[index]
95 }
96}
97
98impl<T> IntoIterator for NeoArray<T> {
99 type Item = T;
100 type IntoIter = std::vec::IntoIter<T>;
101 fn into_iter(self) -> Self::IntoIter {
102 self.data.into_iter()
103 }
104}
105
106impl<'a, T> IntoIterator for &'a NeoArray<T> {
107 type Item = &'a T;
108 type IntoIter = std::slice::Iter<'a, T>;
109 fn into_iter(self) -> Self::IntoIter {
110 self.data.iter()
111 }
112}
113
114impl<T: PartialEq> PartialEq<Vec<T>> for NeoArray<T> {
115 fn eq(&self, other: &Vec<T>) -> bool {
116 self.data == *other
117 }
118}
119
120impl<T: PartialEq> PartialEq<NeoArray<T>> for Vec<T> {
121 fn eq(&self, other: &NeoArray<T>) -> bool {
122 *self == other.data
123 }
124}