capnp/
data_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 sequences of bytes.
23
24use crate::private::layout::*;
25use crate::traits::{FromPointerBuilder, FromPointerReader, IndexMove, ListIter};
26use crate::Result;
27
28#[derive(Copy, Clone)]
29pub struct Owned;
30
31impl crate::traits::Owned for Owned {
32    type Reader<'a> = Reader<'a>;
33    type Builder<'a> = Builder<'a>;
34}
35
36impl crate::introspect::Introspect for Owned {
37    fn introspect() -> crate::introspect::Type {
38        crate::introspect::Type::list_of(crate::introspect::TypeVariant::Data.into())
39    }
40}
41
42#[derive(Clone, Copy)]
43pub struct Reader<'a> {
44    pub reader: ListReader<'a>,
45}
46
47impl<'a> Reader<'a> {
48    pub fn new(reader: ListReader<'_>) -> Reader<'_> {
49        Reader { reader }
50    }
51
52    pub fn len(&self) -> u32 {
53        self.reader.len()
54    }
55
56    pub fn is_empty(&self) -> bool {
57        self.len() == 0
58    }
59
60    pub fn iter(self) -> ListIter<Reader<'a>, Result<crate::data::Reader<'a>>> {
61        let l = self.len();
62        ListIter::new(self, l)
63    }
64
65    pub fn reborrow(&self) -> Reader {
66        Reader {
67            reader: self.reader,
68        }
69    }
70}
71
72impl<'a> FromPointerReader<'a> for Reader<'a> {
73    fn get_from_pointer(
74        reader: &PointerReader<'a>,
75        default: Option<&'a [crate::Word]>,
76    ) -> Result<Reader<'a>> {
77        Ok(Reader {
78            reader: reader.get_list(Pointer, default)?,
79        })
80    }
81}
82
83impl<'a> IndexMove<u32, Result<crate::data::Reader<'a>>> for Reader<'a> {
84    fn index_move(&self, index: u32) -> Result<crate::data::Reader<'a>> {
85        self.get(index)
86    }
87}
88
89impl<'a> Reader<'a> {
90    /// Gets the `data::Reader` at position `index`. Panics if `index` is
91    /// greater than or equal to `len()`.
92    pub fn get(self, index: u32) -> Result<crate::data::Reader<'a>> {
93        assert!(index < self.len());
94        self.reader.get_pointer_element(index).get_data(None)
95    }
96
97    /// Gets the `data::Reader` at position `index`. Returns `None` if `index`
98    /// is greater than or equal to `len()`.
99    pub fn try_get(self, index: u32) -> Option<Result<crate::data::Reader<'a>>> {
100        if index < self.len() {
101            Some(self.reader.get_pointer_element(index).get_data(None))
102        } else {
103            None
104        }
105    }
106}
107
108impl<'a> crate::traits::IntoInternalListReader<'a> for Reader<'a> {
109    fn into_internal_list_reader(self) -> ListReader<'a> {
110        self.reader
111    }
112}
113
114pub struct Builder<'a> {
115    builder: ListBuilder<'a>,
116}
117
118impl<'a> Builder<'a> {
119    pub fn new(builder: ListBuilder<'a>) -> Builder<'a> {
120        Builder { builder }
121    }
122
123    pub fn len(&self) -> u32 {
124        self.builder.len()
125    }
126
127    pub fn is_empty(&self) -> bool {
128        self.len() == 0
129    }
130
131    pub fn into_reader(self) -> Reader<'a> {
132        Reader {
133            reader: self.builder.into_reader(),
134        }
135    }
136
137    pub fn set(&mut self, index: u32, value: crate::data::Reader) {
138        assert!(index < self.len());
139        self.builder
140            .reborrow()
141            .get_pointer_element(index)
142            .set_data(value);
143    }
144
145    pub fn reborrow(&mut self) -> Builder<'_> {
146        Builder {
147            builder: self.builder.reborrow(),
148        }
149    }
150}
151
152impl<'a> FromPointerBuilder<'a> for Builder<'a> {
153    fn init_pointer(builder: PointerBuilder<'a>, size: u32) -> Builder<'a> {
154        Builder {
155            builder: builder.init_list(Pointer, size),
156        }
157    }
158
159    fn get_from_pointer(
160        builder: PointerBuilder<'a>,
161        default: Option<&'a [crate::Word]>,
162    ) -> Result<Builder<'a>> {
163        Ok(Builder {
164            builder: builder.get_list(Pointer, default)?,
165        })
166    }
167}
168
169impl<'a> Builder<'a> {
170    /// Gets the `data::Builder` at position `index`. Panics if `index` is
171    /// greater than or equal to `len()`.
172    pub fn get(self, index: u32) -> Result<crate::data::Builder<'a>> {
173        assert!(index < self.len());
174        self.builder.get_pointer_element(index).get_data(None)
175    }
176
177    /// Gets the `data::Builder` at position `index`. Returns `None` if `index`
178    /// is greater than or equal to `len()`.
179    pub fn try_get(self, index: u32) -> Option<Result<crate::data::Builder<'a>>> {
180        if index < self.len() {
181            Some(self.builder.get_pointer_element(index).get_data(None))
182        } else {
183            None
184        }
185    }
186}
187
188impl<'a> crate::traits::SetterInput<Owned> for Reader<'a> {
189    #[inline]
190    fn set_pointer_builder<'b>(
191        mut pointer: crate::private::layout::PointerBuilder<'b>,
192        value: Reader<'a>,
193        canonicalize: bool,
194    ) -> Result<()> {
195        pointer.set_list(&value.reader, canonicalize)?;
196        Ok(())
197    }
198}
199
200impl<'a> ::core::iter::IntoIterator for Reader<'a> {
201    type Item = Result<crate::data::Reader<'a>>;
202    type IntoIter = ListIter<Reader<'a>, Self::Item>;
203
204    fn into_iter(self) -> Self::IntoIter {
205        self.iter()
206    }
207}
208
209impl<'a> From<Reader<'a>> for crate::dynamic_value::Reader<'a> {
210    fn from(t: Reader<'a>) -> crate::dynamic_value::Reader<'a> {
211        crate::dynamic_value::Reader::List(crate::dynamic_list::Reader {
212            reader: t.reader,
213            element_type: crate::introspect::TypeVariant::Data.into(),
214        })
215    }
216}
217
218impl<'a> crate::dynamic_value::DowncastReader<'a> for Reader<'a> {
219    fn downcast_reader(v: crate::dynamic_value::Reader<'a>) -> Self {
220        let dl: crate::dynamic_list::Reader = v.downcast();
221        assert!(dl
222            .element_type()
223            .loose_equals(crate::introspect::TypeVariant::Data.into()));
224        Reader { reader: dl.reader }
225    }
226}
227
228impl<'a> From<Builder<'a>> for crate::dynamic_value::Builder<'a> {
229    fn from(t: Builder<'a>) -> crate::dynamic_value::Builder<'a> {
230        crate::dynamic_value::Builder::List(crate::dynamic_list::Builder {
231            builder: t.builder,
232            element_type: crate::introspect::TypeVariant::Data.into(),
233        })
234    }
235}
236
237impl<'a> crate::dynamic_value::DowncastBuilder<'a> for Builder<'a> {
238    fn downcast_builder(v: crate::dynamic_value::Builder<'a>) -> Self {
239        let dl: crate::dynamic_list::Builder = v.downcast();
240        assert!(dl
241            .element_type()
242            .loose_equals(crate::introspect::TypeVariant::Data.into()));
243        Builder {
244            builder: dl.builder,
245        }
246    }
247}
248
249impl core::fmt::Debug for Reader<'_> {
250    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
251        core::fmt::Debug::fmt(
252            &::core::convert::Into::<crate::dynamic_value::Reader<'_>>::into(*self),
253            f,
254        )
255    }
256}