capnp/
list_list.rs

1// Copyright (c) 2013-2015 Sandstorm Development Group, Inc. and contributors
2// Licensed under the MIT License:
3//
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20// THE SOFTWARE.
21
22//! List of lists.
23
24use crate::introspect;
25use crate::private::layout::{ListBuilder, ListReader, Pointer, PointerBuilder, PointerReader};
26use crate::traits::{FromPointerBuilder, FromPointerReader, IndexMove, ListIter, SetterInput};
27use crate::Result;
28
29#[derive(Clone, Copy)]
30pub struct Owned<T>
31where
32    T: crate::traits::Owned,
33{
34    marker: ::core::marker::PhantomData<T>,
35}
36
37impl<T> introspect::Introspect for Owned<T>
38where
39    T: introspect::Introspect + crate::traits::Owned,
40{
41    fn introspect() -> introspect::Type {
42        introspect::Type::list_of(T::introspect())
43    }
44}
45
46impl<T> crate::traits::Owned for Owned<T>
47where
48    T: crate::traits::Owned,
49{
50    type Reader<'a> = Reader<'a, T>;
51    type Builder<'a> = Builder<'a, T>;
52}
53
54pub struct Reader<'a, T>
55where
56    T: crate::traits::Owned,
57{
58    marker: ::core::marker::PhantomData<T::Reader<'a>>,
59    reader: ListReader<'a>,
60}
61
62impl<'a, T> Reader<'a, T>
63where
64    T: crate::traits::Owned,
65{
66    pub fn len(&self) -> u32 {
67        self.reader.len()
68    }
69
70    pub fn is_empty(&self) -> bool {
71        self.len() == 0
72    }
73
74    pub fn iter(self) -> ListIter<Reader<'a, T>, Result<T::Reader<'a>>> {
75        ListIter::new(self, self.len())
76    }
77}
78
79impl<'a, T> Clone for Reader<'a, T>
80where
81    T: crate::traits::Owned,
82{
83    fn clone(&self) -> Reader<'a, T> {
84        *self
85    }
86}
87
88impl<T> Copy for Reader<'_, T> where T: crate::traits::Owned {}
89
90impl<'a, T> IndexMove<u32, Result<T::Reader<'a>>> for Reader<'a, T>
91where
92    T: crate::traits::Owned,
93{
94    fn index_move(&self, index: u32) -> Result<T::Reader<'a>> {
95        self.get(index)
96    }
97}
98
99impl<'a, T> FromPointerReader<'a> for Reader<'a, T>
100where
101    T: crate::traits::Owned,
102{
103    fn get_from_pointer(
104        reader: &PointerReader<'a>,
105        default: Option<&'a [crate::Word]>,
106    ) -> Result<Reader<'a, T>> {
107        Ok(Reader {
108            reader: reader.get_list(Pointer, default)?,
109            marker: ::core::marker::PhantomData,
110        })
111    }
112}
113
114impl<'a, T> Reader<'a, T>
115where
116    T: crate::traits::Owned,
117{
118    /// Gets the element at position `index`. Panics if `index` is greater than or
119    /// equal to `len()`.
120    pub fn get(self, index: u32) -> Result<T::Reader<'a>> {
121        assert!(index < self.len());
122        FromPointerReader::get_from_pointer(&self.reader.get_pointer_element(index), None)
123    }
124
125    /// Gets the element at position `index`. Returns `None` if `index`
126    /// is greater than or equal to `len()`.
127    pub fn try_get(self, index: u32) -> Option<Result<T::Reader<'a>>> {
128        if index < self.len() {
129            Some(FromPointerReader::get_from_pointer(
130                &self.reader.get_pointer_element(index),
131                None,
132            ))
133        } else {
134            None
135        }
136    }
137}
138
139impl<'a, T> crate::traits::IntoInternalListReader<'a> for Reader<'a, T>
140where
141    T: crate::traits::Owned,
142{
143    fn into_internal_list_reader(self) -> ListReader<'a> {
144        self.reader
145    }
146}
147
148pub struct Builder<'a, T>
149where
150    T: crate::traits::Owned,
151{
152    marker: ::core::marker::PhantomData<T>,
153    builder: ListBuilder<'a>,
154}
155
156impl<'a, T> Builder<'a, T>
157where
158    T: crate::traits::Owned,
159{
160    pub fn len(&self) -> u32 {
161        self.builder.len()
162    }
163
164    pub fn is_empty(&self) -> bool {
165        self.len() == 0
166    }
167
168    pub fn into_reader(self) -> Reader<'a, T> {
169        Reader {
170            reader: self.builder.into_reader(),
171            marker: ::core::marker::PhantomData,
172        }
173    }
174}
175
176impl<'a, T> Builder<'a, T>
177where
178    T: crate::traits::Owned,
179{
180    pub fn init(self, index: u32, size: u32) -> T::Builder<'a> {
181        FromPointerBuilder::init_pointer(self.builder.get_pointer_element(index), size)
182    }
183}
184
185impl<T> Builder<'_, T>
186where
187    T: crate::traits::Owned,
188{
189    pub fn reborrow(&mut self) -> Builder<'_, T> {
190        Builder {
191            builder: self.builder.reborrow(),
192            marker: ::core::marker::PhantomData,
193        }
194    }
195}
196
197impl<'a, T> FromPointerBuilder<'a> for Builder<'a, T>
198where
199    T: crate::traits::Owned,
200{
201    fn init_pointer(builder: PointerBuilder<'a>, size: u32) -> Builder<'a, T> {
202        Builder {
203            marker: ::core::marker::PhantomData,
204            builder: builder.init_list(Pointer, size),
205        }
206    }
207    fn get_from_pointer(
208        builder: PointerBuilder<'a>,
209        default: Option<&'a [crate::Word]>,
210    ) -> Result<Builder<'a, T>> {
211        Ok(Builder {
212            marker: ::core::marker::PhantomData,
213            builder: builder.get_list(Pointer, default)?,
214        })
215    }
216}
217
218impl<'a, T> Builder<'a, T>
219where
220    T: crate::traits::Owned,
221{
222    /// Gets the element at position `index`. Panics if `index` is greater than or
223    /// equal to `len()`.
224    pub fn get(self, index: u32) -> Result<T::Builder<'a>> {
225        assert!(index < self.len());
226        FromPointerBuilder::get_from_pointer(self.builder.get_pointer_element(index), None)
227    }
228
229    /// Gets the element at position `index`. Returns `None` if `index`
230    /// is greater than or equal to `len()`.
231    pub fn try_get(self, index: u32) -> Option<Result<T::Builder<'a>>> {
232        if index < self.len() {
233            Some(FromPointerBuilder::get_from_pointer(
234                self.builder.get_pointer_element(index),
235                None,
236            ))
237        } else {
238            None
239        }
240    }
241
242    pub fn set(&mut self, index: u32, value: impl SetterInput<T>) -> Result<()> {
243        assert!(index < self.len());
244        SetterInput::set_pointer_builder(
245            self.builder.reborrow().get_pointer_element(index),
246            value,
247            false,
248        )
249    }
250}
251
252impl<'a, T> crate::traits::SetterInput<Owned<T>> for Reader<'a, T>
253where
254    T: crate::traits::Owned,
255{
256    #[inline]
257    fn set_pointer_builder<'b>(
258        mut pointer: crate::private::layout::PointerBuilder<'b>,
259        value: Reader<'a, T>,
260        canonicalize: bool,
261    ) -> Result<()> {
262        pointer.set_list(&value.reader, canonicalize)
263    }
264}
265
266impl<'a, T> ::core::iter::IntoIterator for Reader<'a, T>
267where
268    T: crate::traits::Owned,
269{
270    type Item = Result<T::Reader<'a>>;
271    type IntoIter = ListIter<Reader<'a, T>, Self::Item>;
272
273    fn into_iter(self) -> Self::IntoIter {
274        self.iter()
275    }
276}
277
278impl<'a, T: crate::traits::Owned> From<Reader<'a, T>> for crate::dynamic_value::Reader<'a> {
279    fn from(t: Reader<'a, T>) -> crate::dynamic_value::Reader<'a> {
280        crate::dynamic_value::Reader::List(crate::dynamic_list::Reader::new(
281            t.reader,
282            T::introspect(),
283        ))
284    }
285}
286
287impl<'a, T: crate::traits::Owned> crate::dynamic_value::DowncastReader<'a> for Reader<'a, T> {
288    fn downcast_reader(v: crate::dynamic_value::Reader<'a>) -> Self {
289        let dl: crate::dynamic_list::Reader = v.downcast();
290        assert!(dl.element_type().loose_equals(T::introspect()));
291        Reader {
292            reader: dl.reader,
293            marker: core::marker::PhantomData,
294        }
295    }
296}
297
298impl<'a, T: crate::traits::Owned> From<Builder<'a, T>> for crate::dynamic_value::Builder<'a> {
299    fn from(t: Builder<'a, T>) -> crate::dynamic_value::Builder<'a> {
300        crate::dynamic_value::Builder::List(crate::dynamic_list::Builder::new(
301            t.builder,
302            T::introspect(),
303        ))
304    }
305}
306
307impl<'a, T: crate::traits::Owned> crate::dynamic_value::DowncastBuilder<'a> for Builder<'a, T> {
308    fn downcast_builder(v: crate::dynamic_value::Builder<'a>) -> Self {
309        let dl: crate::dynamic_list::Builder = v.downcast();
310        assert!(dl.element_type().loose_equals(T::introspect()));
311        Builder {
312            builder: dl.builder,
313            marker: core::marker::PhantomData,
314        }
315    }
316}
317
318impl<T: crate::traits::Owned> core::fmt::Debug for Reader<'_, T> {
319    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
320        core::fmt::Debug::fmt(
321            &::core::convert::Into::<crate::dynamic_value::Reader<'_>>::into(*self),
322            f,
323        )
324    }
325}