capnp/
enum_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 enums.
23
24use 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    /// Gets the `T` at position `index`. Panics if `index` is greater than or
97    /// equal to `len()`.
98    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    /// Gets the `T` at position `index`. Returns `None` if `index`
105    /// is greater than or equal to `len()`.
106    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>
117where
118    T: PrimitiveElement,
119{
120    fn into_internal_list_reader(self) -> ListReader<'a> {
121        self.reader
122    }
123}
124
125pub struct Builder<'a, T> {
126    marker: PhantomData<T>,
127    builder: ListBuilder<'a>,
128}
129
130impl<'a, T: Into<u16> + TryFrom<u16, Error = NotInSchema>> Builder<'a, T> {
131    pub fn len(&self) -> u32 {
132        self.builder.len()
133    }
134
135    pub fn is_empty(&self) -> bool {
136        self.len() == 0
137    }
138
139    pub fn into_reader(self) -> Reader<'a, T> {
140        Reader {
141            reader: self.builder.into_reader(),
142            marker: PhantomData,
143        }
144    }
145
146    pub fn set(&mut self, index: u32, value: T) {
147        assert!(index < self.len());
148        PrimitiveElement::set(&self.builder, index, value.into());
149    }
150}
151
152impl<'a, T: TryFrom<u16, Error = NotInSchema>> FromPointerBuilder<'a> for Builder<'a, T> {
153    fn init_pointer(builder: PointerBuilder<'a>, size: u32) -> Builder<'a, T> {
154        Builder {
155            builder: builder.init_list(TwoBytes, size),
156            marker: PhantomData,
157        }
158    }
159    fn get_from_pointer(
160        builder: PointerBuilder<'a>,
161        default: Option<&'a [crate::Word]>,
162    ) -> Result<Builder<'a, T>> {
163        Ok(Builder {
164            builder: builder.get_list(TwoBytes, default)?,
165            marker: PhantomData,
166        })
167    }
168}
169
170impl<T: Into<u16> + TryFrom<u16, Error = NotInSchema>> Builder<'_, T> {
171    /// Gets the `T` at position `index`. Panics if `index` is greater than or
172    /// equal to `len()`.
173    pub fn get(&self, index: u32) -> ::core::result::Result<T, NotInSchema> {
174        assert!(index < self.len());
175        let result: u16 = PrimitiveElement::get_from_builder(&self.builder, index);
176        result.try_into()
177    }
178
179    /// Gets the `T` at position `index`. Returns `None` if `index`
180    /// is greater than or equal to `len()`.
181    pub fn try_get(&self, index: u32) -> Option<::core::result::Result<T, NotInSchema>> {
182        if index < self.len() {
183            let result: u16 = PrimitiveElement::get_from_builder(&self.builder, index);
184            Some(result.try_into())
185        } else {
186            None
187        }
188    }
189
190    pub fn reborrow(&mut self) -> Builder<'_, T> {
191        Builder {
192            builder: self.builder.reborrow(),
193            marker: PhantomData,
194        }
195    }
196}
197
198impl<'a, T> crate::traits::SetterInput<Owned<T>> for Reader<'a, T> {
199    #[inline]
200    fn set_pointer_builder<'b>(
201        mut pointer: crate::private::layout::PointerBuilder<'b>,
202        value: Reader<'a, T>,
203        canonicalize: bool,
204    ) -> Result<()> {
205        pointer.set_list(&value.reader, canonicalize)
206    }
207}
208
209impl<'a, T: Copy + Into<u16>> crate::traits::SetterInput<Owned<T>> for &'a [T] {
210    #[inline]
211    fn set_pointer_builder<'b>(
212        pointer: crate::private::layout::PointerBuilder<'b>,
213        value: &'a [T],
214        _canonicalize: bool,
215    ) -> Result<()> {
216        let builder = pointer.init_list(
217            crate::private::layout::ElementSize::TwoBytes,
218            value.len() as u32,
219        );
220        for (idx, v) in value.iter().enumerate() {
221            <u16 as PrimitiveElement>::set(&builder, idx as u32, (*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().loose_equals(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().loose_equals(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(
300            &::core::convert::Into::<crate::dynamic_value::Reader<'_>>::into(*self),
301            f,
302        )
303    }
304}