1use crate::de::{Deserialize, Seq, Visitor};
2use crate::error::Result;
3use crate::json::{drop, Value};
4use crate::private;
5use crate::ser::{Fragment, Serialize};
6use alloc::boxed::Box;
7use alloc::vec::Vec;
8use core::fmt::{self, Debug};
9use core::mem::{self, ManuallyDrop};
10use core::ops::{Deref, DerefMut};
11use core::ptr;
12
13#[derive(Default)]
15pub struct Array {
16 inner: Vec<Value>,
17}
18
19impl Drop for Array {
20 fn drop(&mut self) {
21 self.inner.drain(..).for_each(drop::safely);
22 }
23}
24
25fn take(array: Array) -> Vec<Value> {
26 let array = ManuallyDrop::new(array);
27 unsafe { ptr::read(&array.inner) }
28}
29
30impl Array {
31 pub fn new() -> Self {
32 Array { inner: Vec::new() }
33 }
34}
35
36impl Deref for Array {
37 type Target = Vec<Value>;
38
39 fn deref(&self) -> &Self::Target {
40 &self.inner
41 }
42}
43
44impl DerefMut for Array {
45 fn deref_mut(&mut self) -> &mut Self::Target {
46 &mut self.inner
47 }
48}
49
50impl Clone for Array {
51 fn clone(&self) -> Self {
52 Array {
53 inner: self.inner.clone(),
54 }
55 }
56
57 fn clone_from(&mut self, other: &Self) {
58 self.inner.clone_from(&other.inner);
59 }
60}
61
62impl IntoIterator for Array {
63 type Item = Value;
64 type IntoIter = <Vec<Value> as IntoIterator>::IntoIter;
65
66 fn into_iter(self) -> Self::IntoIter {
67 take(self).into_iter()
68 }
69}
70
71impl<'a> IntoIterator for &'a Array {
72 type Item = &'a Value;
73 type IntoIter = <&'a Vec<Value> as IntoIterator>::IntoIter;
74
75 fn into_iter(self) -> Self::IntoIter {
76 self.iter()
77 }
78}
79
80impl<'a> IntoIterator for &'a mut Array {
81 type Item = &'a mut Value;
82 type IntoIter = <&'a mut Vec<Value> as IntoIterator>::IntoIter;
83
84 fn into_iter(self) -> Self::IntoIter {
85 self.iter_mut()
86 }
87}
88
89impl FromIterator<Value> for Array {
90 fn from_iter<I>(iter: I) -> Self
91 where
92 I: IntoIterator<Item = Value>,
93 {
94 Array {
95 inner: Vec::from_iter(iter),
96 }
97 }
98}
99
100impl Debug for Array {
101 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
102 formatter.write_str("Array ")?;
103 formatter.debug_list().entries(self).finish()
104 }
105}
106
107impl Serialize for Array {
108 fn begin(&self) -> Fragment {
109 private::stream_slice(self)
110 }
111}
112
113impl Deserialize for Array {
114 fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
115 make_place!(Place);
116
117 impl Visitor for Place<Array> {
118 fn seq(&mut self) -> Result<Box<dyn Seq + '_>> {
119 Ok(Box::new(ArrayBuilder {
120 out: &mut self.out,
121 array: Array::new(),
122 element: None,
123 }))
124 }
125 }
126
127 struct ArrayBuilder<'a> {
128 out: &'a mut Option<Array>,
129 array: Array,
130 element: Option<Value>,
131 }
132
133 impl<'a> ArrayBuilder<'a> {
134 fn shift(&mut self) {
135 if let Some(e) = self.element.take() {
136 self.array.push(e);
137 }
138 }
139 }
140
141 impl<'a> Seq for ArrayBuilder<'a> {
142 fn element(&mut self) -> Result<&mut dyn Visitor> {
143 self.shift();
144 Ok(Deserialize::begin(&mut self.element))
145 }
146
147 fn finish(&mut self) -> Result<()> {
148 self.shift();
149 *self.out = Some(mem::replace(&mut self.array, Array::new()));
150 Ok(())
151 }
152 }
153
154 Place::new(out)
155 }
156}