1use alloc::vec::{self, Vec};
4use core::{
5 fmt,
6 ops::{Deref, DerefMut},
7 slice,
8};
9
10use crate::{Value, tree};
11
12#[derive(Default)]
36pub struct Array<'a> {
37 items: Vec<Value<'a>>,
38}
39
40impl<'a> Array<'a> {
41 #[inline]
49 #[must_use]
50 pub const fn new() -> Self {
51 Self { items: Vec::new() }
52 }
53
54 #[inline]
63 #[must_use]
64 pub fn with_capacity(capacity: usize) -> Self {
65 Self {
66 items: Vec::with_capacity(capacity),
67 }
68 }
69
70 #[inline]
81 #[must_use]
82 pub fn into_vec(mut self) -> Vec<Value<'a>> {
83 core::mem::take(&mut self.items)
84 }
85
86 #[must_use]
102 pub fn into_owned(self) -> Array<'static> {
103 match tree::into_owned(Value::Array(self)) {
104 Value::Array(a) => a,
105 _ => Array::new(),
106 }
107 }
108
109 #[inline]
110 pub(crate) fn items_mut(&mut self) -> &mut Vec<Value<'a>> {
111 &mut self.items
112 }
113}
114
115impl<'a> Deref for Array<'a> {
116 type Target = Vec<Value<'a>>;
117
118 #[inline]
119 fn deref(&self) -> &Self::Target {
120 &self.items
121 }
122}
123
124impl DerefMut for Array<'_> {
125 #[inline]
126 fn deref_mut(&mut self) -> &mut Self::Target {
127 &mut self.items
128 }
129}
130
131impl Drop for Array<'_> {
132 fn drop(&mut self) {
133 tree::drop_items(&mut self.items);
136 }
137}
138
139impl Clone for Array<'_> {
140 fn clone(&self) -> Self {
141 Self {
142 items: tree::clone_items(&self.items),
143 }
144 }
145}
146
147impl PartialEq for Array<'_> {
148 fn eq(&self, other: &Self) -> bool {
149 self.items.len() == other.items.len()
150 && self
151 .items
152 .iter()
153 .zip(&other.items)
154 .all(|(a, b)| tree::eq(a, b))
155 }
156}
157
158impl Eq for Array<'_> {}
159
160impl fmt::Debug for Array<'_> {
161 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
162 f.debug_list().entries(self.items.iter()).finish()
163 }
164}
165
166impl<'a> From<Vec<Value<'a>>> for Array<'a> {
167 #[inline]
168 fn from(items: Vec<Value<'a>>) -> Self {
169 Self { items }
170 }
171}
172
173impl<'a, T: Into<Value<'a>>> FromIterator<T> for Array<'a> {
174 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
175 Self {
176 items: iter.into_iter().map(Into::into).collect(),
177 }
178 }
179}
180
181impl<'a, T: Into<Value<'a>>> Extend<T> for Array<'a> {
182 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
183 self.items.extend(iter.into_iter().map(Into::into));
184 }
185}
186
187impl<'a> IntoIterator for Array<'a> {
188 type Item = Value<'a>;
189 type IntoIter = vec::IntoIter<Value<'a>>;
190
191 #[inline]
192 fn into_iter(self) -> Self::IntoIter {
193 self.into_vec().into_iter()
194 }
195}
196
197impl<'r, 'a> IntoIterator for &'r Array<'a> {
198 type Item = &'r Value<'a>;
199 type IntoIter = slice::Iter<'r, Value<'a>>;
200
201 #[inline]
202 fn into_iter(self) -> Self::IntoIter {
203 self.items.iter()
204 }
205}
206
207impl<'r, 'a> IntoIterator for &'r mut Array<'a> {
208 type Item = &'r mut Value<'a>;
209 type IntoIter = slice::IterMut<'r, Value<'a>>;
210
211 #[inline]
212 fn into_iter(self) -> Self::IntoIter {
213 self.items.iter_mut()
214 }
215}