1use crate::private::layout::{
25 ListBuilder, ListReader, PointerBuilder, PointerReader, PrimitiveElement, TwoBytes,
26};
27use crate::traits::{FromPointerBuilder, FromPointerReader, IndexMove, ListIter};
28use crate::{NotInSchema, Result};
29
30use core::marker::PhantomData;
31
32#[derive(Clone, Copy)]
33pub struct Owned<T> {
34 marker: PhantomData<T>,
35}
36
37impl<T> crate::introspect::Introspect for Owned<T>
38where
39 T: crate::introspect::Introspect,
40{
41 fn introspect() -> crate::introspect::Type {
42 crate::introspect::Type::list_of(T::introspect())
43 }
44}
45
46impl<T> crate::traits::Owned for Owned<T>
47where
48 T: TryFrom<u16, Error = NotInSchema> + crate::introspect::Introspect,
49{
50 type Reader<'a> = Reader<'a, T>;
51 type Builder<'a> = Builder<'a, T>;
52}
53
54#[derive(Clone, Copy)]
55pub struct Reader<'a, T> {
56 marker: PhantomData<T>,
57 reader: ListReader<'a>,
58}
59
60impl<'a, T: TryFrom<u16, Error = NotInSchema>> Reader<'a, T> {
61 pub fn len(&self) -> u32 {
62 self.reader.len()
63 }
64
65 pub fn is_empty(&self) -> bool {
66 self.len() == 0
67 }
68
69 pub fn iter(self) -> ListIter<Reader<'a, T>, ::core::result::Result<T, NotInSchema>> {
70 let l = self.len();
71 ListIter::new(self, l)
72 }
73}
74
75impl<'a, T: TryFrom<u16, Error = NotInSchema>> FromPointerReader<'a> for Reader<'a, T> {
76 fn get_from_pointer(
77 reader: &PointerReader<'a>,
78 default: Option<&'a [crate::Word]>,
79 ) -> Result<Reader<'a, T>> {
80 Ok(Reader {
81 reader: reader.get_list(TwoBytes, default)?,
82 marker: PhantomData,
83 })
84 }
85}
86
87impl<T: TryFrom<u16, Error = NotInSchema>> IndexMove<u32, ::core::result::Result<T, NotInSchema>>
88 for Reader<'_, T>
89{
90 fn index_move(&self, index: u32) -> ::core::result::Result<T, NotInSchema> {
91 self.get(index)
92 }
93}
94
95impl<T: TryFrom<u16, Error = NotInSchema>> Reader<'_, T> {
96 pub fn get(&self, index: u32) -> ::core::result::Result<T, NotInSchema> {
99 assert!(index < self.len());
100 let result: u16 = PrimitiveElement::get(&self.reader, index);
101 result.try_into()
102 }
103
104 pub fn try_get(&self, index: u32) -> Option<::core::result::Result<T, NotInSchema>> {
107 if index < self.len() {
108 let result: u16 = PrimitiveElement::get(&self.reader, index);
109 Some(result.try_into())
110 } else {
111 None
112 }
113 }
114}
115
116impl<'a, T> crate::traits::IntoInternalListReader<'a> for Reader<'a, T> {
117 fn into_internal_list_reader(self) -> ListReader<'a> {
118 self.reader
119 }
120}
121
122pub struct Builder<'a, T> {
123 marker: PhantomData<T>,
124 builder: ListBuilder<'a>,
125}
126
127impl<'a, T: Into<u16> + TryFrom<u16, Error = NotInSchema>> Builder<'a, T> {
128 pub fn len(&self) -> u32 {
129 self.builder.len()
130 }
131
132 pub fn is_empty(&self) -> bool {
133 self.len() == 0
134 }
135
136 pub fn into_reader(self) -> Reader<'a, T> {
137 Reader {
138 reader: self.builder.into_reader(),
139 marker: PhantomData,
140 }
141 }
142
143 pub fn set(&mut self, index: u32, value: T) {
144 assert!(index < self.len());
145 PrimitiveElement::set(&self.builder, index, value.into());
146 }
147}
148
149impl<'a, T: TryFrom<u16, Error = NotInSchema>> FromPointerBuilder<'a> for Builder<'a, T> {
150 fn init_pointer(builder: PointerBuilder<'a>, size: u32) -> Builder<'a, T> {
151 Builder {
152 builder: builder.init_list(TwoBytes, size),
153 marker: PhantomData,
154 }
155 }
156 fn get_from_pointer(
157 builder: PointerBuilder<'a>,
158 default: Option<&'a [crate::Word]>,
159 ) -> Result<Builder<'a, T>> {
160 Ok(Builder {
161 builder: builder.get_list(TwoBytes, default)?,
162 marker: PhantomData,
163 })
164 }
165}
166
167impl<T: Into<u16> + TryFrom<u16, Error = NotInSchema>> Builder<'_, T> {
168 pub fn get(&self, index: u32) -> ::core::result::Result<T, NotInSchema> {
171 assert!(index < self.len());
172 let result: u16 = PrimitiveElement::get_from_builder(&self.builder, index);
173 result.try_into()
174 }
175
176 pub fn try_get(&self, index: u32) -> Option<::core::result::Result<T, NotInSchema>> {
179 if index < self.len() {
180 let result: u16 = PrimitiveElement::get_from_builder(&self.builder, index);
181 Some(result.try_into())
182 } else {
183 None
184 }
185 }
186
187 pub fn reborrow(&mut self) -> Builder<'_, T> {
188 Builder {
189 builder: self.builder.reborrow(),
190 marker: PhantomData,
191 }
192 }
193}
194
195impl<'a, T> crate::traits::SetterInput<Owned<T>> for Reader<'a, T> {
196 #[inline]
197 fn set_pointer_builder<'b>(
198 mut pointer: crate::private::layout::PointerBuilder<'b>,
199 value: Reader<'a, T>,
200 canonicalize: bool,
201 ) -> Result<()> {
202 pointer.set_list(&value.reader, canonicalize)
203 }
204}
205
206impl<'a, T: Copy + Into<u16>> crate::traits::SetterInput<Owned<T>> for &'a [T] {
207 #[inline]
208 fn set_pointer_builder<'b>(
209 pointer: crate::private::layout::PointerBuilder<'b>,
210 value: &'a [T],
211 _canonicalize: bool,
212 ) -> Result<()> {
213 let builder = pointer.init_list(
214 crate::private::layout::ElementSize::TwoBytes,
215 value
216 .len()
217 .try_into()
218 .expect("list len too big to fit in u32"),
219 );
220 for (idx, v) in value.iter().enumerate() {
221 <u16 as PrimitiveElement>::set(&builder, idx.try_into().unwrap(), (*v).into())
222 }
223 Ok(())
224 }
225}
226
227impl<'a, T: Copy + Into<u16>, const N: usize> crate::traits::SetterInput<Owned<T>> for &'a [T; N] {
228 #[inline]
229 fn set_pointer_builder<'b>(
230 pointer: crate::private::layout::PointerBuilder<'b>,
231 value: &'a [T; N],
232 canonicalize: bool,
233 ) -> Result<()> {
234 crate::traits::SetterInput::set_pointer_builder(pointer, &value[..], canonicalize)
235 }
236}
237
238impl<'a, T: TryFrom<u16, Error = NotInSchema>> ::core::iter::IntoIterator for Reader<'a, T> {
239 type Item = ::core::result::Result<T, NotInSchema>;
240 type IntoIter = ListIter<Reader<'a, T>, Self::Item>;
241
242 fn into_iter(self) -> Self::IntoIter {
243 self.iter()
244 }
245}
246
247impl<'a, T: TryFrom<u16, Error = NotInSchema> + crate::introspect::Introspect> From<Reader<'a, T>>
248 for crate::dynamic_value::Reader<'a>
249{
250 fn from(t: Reader<'a, T>) -> crate::dynamic_value::Reader<'a> {
251 crate::dynamic_value::Reader::List(crate::dynamic_list::Reader::new(
252 t.reader,
253 T::introspect(),
254 ))
255 }
256}
257
258impl<'a, T: TryFrom<u16, Error = NotInSchema> + crate::introspect::Introspect>
259 crate::dynamic_value::DowncastReader<'a> for Reader<'a, T>
260{
261 fn downcast_reader(v: crate::dynamic_value::Reader<'a>) -> Self {
262 let dl: crate::dynamic_list::Reader = v.downcast();
263 assert!(dl.element_type() == T::introspect());
264 Reader {
265 reader: dl.reader,
266 marker: PhantomData,
267 }
268 }
269}
270
271impl<'a, T: TryFrom<u16, Error = NotInSchema> + crate::introspect::Introspect> From<Builder<'a, T>>
272 for crate::dynamic_value::Builder<'a>
273{
274 fn from(t: Builder<'a, T>) -> crate::dynamic_value::Builder<'a> {
275 crate::dynamic_value::Builder::List(crate::dynamic_list::Builder::new(
276 t.builder,
277 T::introspect(),
278 ))
279 }
280}
281
282impl<'a, T: TryFrom<u16, Error = NotInSchema> + crate::introspect::Introspect>
283 crate::dynamic_value::DowncastBuilder<'a> for Builder<'a, T>
284{
285 fn downcast_builder(v: crate::dynamic_value::Builder<'a>) -> Self {
286 let dl: crate::dynamic_list::Builder = v.downcast();
287 assert!(dl.element_type() == T::introspect());
288 Builder {
289 builder: dl.builder,
290 marker: PhantomData,
291 }
292 }
293}
294
295impl<T: Copy + TryFrom<u16, Error = NotInSchema> + crate::introspect::Introspect> core::fmt::Debug
296 for Reader<'_, T>
297{
298 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
299 core::fmt::Debug::fmt(&crate::dynamic_value::Reader::from(*self), f)
300 }
301}