capnp/
traits.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
22use crate::private::layout::CapTable;
23use crate::private::layout::{
24    ListReader, PointerBuilder, PointerReader, StructBuilder, StructReader, StructSize,
25};
26use crate::Result;
27
28use core::marker::PhantomData;
29
30pub trait HasStructSize {
31    const STRUCT_SIZE: StructSize;
32}
33
34/// Trait for all types that can be converted to a low-level `StructReader`.
35pub trait IntoInternalStructReader<'a> {
36    fn into_internal_struct_reader(self) -> StructReader<'a>;
37}
38
39/// Trait for all types that can be converted to a low-level `ListReader`.
40pub trait IntoInternalListReader<'a> {
41    fn into_internal_list_reader(self) -> ListReader<'a>;
42}
43
44pub trait FromPointerReader<'a>: Sized {
45    fn get_from_pointer(
46        reader: &PointerReader<'a>,
47        default: Option<&'a [crate::Word]>,
48    ) -> Result<Self>;
49}
50
51/// A trait to encode relationships between readers and builders.
52///
53/// If `Foo` is a Cap'n Proto struct and `Bar` is a Rust-native struct, then
54/// `foo::Reader<'a>` is to `foo::Owned` as `&'a Bar` is to `Bar`, and
55/// `foo::Builder<'a>` is to `foo::Owned` as `&'a mut Bar` is to `Bar`.
56/// The relationship is formalized by an `impl capnp::traits::Owned for foo::Owned`.
57/// Because Cap'n Proto struct layout differs from Rust struct layout, a `foo::Owned` value
58/// cannot be used for anything interesting on its own; the `foo::Owned` type is useful
59/// nonetheless as a type parameter, e.g. for a generic container that owns a Cap'n Proto
60/// message of type `T: capnp::traits::Owned`.
61pub trait Owned: crate::introspect::Introspect {
62    type Reader<'a>: FromPointerReader<'a> + SetterInput<Self>;
63    type Builder<'a>: FromPointerBuilder<'a>;
64}
65
66pub trait OwnedStruct: crate::introspect::Introspect {
67    type Reader<'a>: From<StructReader<'a>> + SetterInput<Self> + IntoInternalStructReader<'a>;
68    type Builder<'a>: From<StructBuilder<'a>> + HasStructSize;
69}
70
71pub trait Pipelined {
72    type Pipeline;
73}
74
75pub trait FromPointerBuilder<'a>: Sized {
76    fn init_pointer(builder: PointerBuilder<'a>, length: u32) -> Self;
77    fn get_from_pointer(
78        builder: PointerBuilder<'a>,
79        default: Option<&'a [crate::Word]>,
80    ) -> Result<Self>;
81}
82
83/// A trait marking types that can be passed as inputs to setter methods.
84/// `Receiver` is intended to be an `Owned`, representing the destination type.
85///
86/// This trait allows setters to support multiple types of input. For example,
87/// a text field setter accepts values of type `&str` and of type `text::Reader`.
88pub trait SetterInput<Receiver: ?Sized> {
89    /// Copies the values from `input` into `builder`, where `builder`
90    /// represents the backing memory of a `<Receiver as Owned>::Builder`.
91    ///
92    /// End user code should never need to call this method directly.
93    fn set_pointer_builder(
94        builder: PointerBuilder<'_>,
95        input: Self,
96        canonicalize: bool,
97    ) -> Result<()>;
98}
99
100/// A trait for types that can be "imbued" with capabilities.
101///
102/// A newly-read message from the network might contain capability pointers
103/// but until the message has been imbued with the actual capabilities,
104/// those pointers will not be usable.
105pub trait Imbue<'a> {
106    fn imbue(&mut self, caps: &'a CapTable);
107}
108
109/// Like `Imbue`, but the capability table is mutable.
110pub trait ImbueMut<'a> {
111    fn imbue_mut(&mut self, caps: &'a mut CapTable);
112}
113
114/// User-defined Cap'n Proto structs and interfaces are statically assigned a
115/// 64-bit type ID. This trait allows the ID to be retrieved.
116pub trait HasTypeId {
117    const TYPE_ID: u64;
118}
119
120pub trait IndexMove<I, T> {
121    fn index_move(&self, index: I) -> T;
122}
123
124pub struct ListIter<T, U> {
125    marker: PhantomData<U>,
126    list: T,
127    index: u32,
128    size: u32,
129}
130
131impl<T, U> ListIter<T, U> {
132    pub fn new(list: T, size: u32) -> Self {
133        Self {
134            list,
135            index: 0,
136            size,
137            marker: PhantomData,
138        }
139    }
140}
141
142impl<U, T: IndexMove<u32, U>> ::core::iter::Iterator for ListIter<T, U> {
143    type Item = U;
144    fn next(&mut self) -> ::core::option::Option<U> {
145        if self.index < self.size {
146            let result = self.list.index_move(self.index);
147            self.index += 1;
148            Some(result)
149        } else {
150            None
151        }
152    }
153
154    fn size_hint(&self) -> (usize, Option<usize>) {
155        (self.size as usize, Some(self.size as usize))
156    }
157
158    fn nth(&mut self, p: usize) -> Option<U> {
159        if self.index + (p as u32) < self.size {
160            self.index += p as u32;
161            let result = self.list.index_move(self.index);
162            self.index += 1;
163            Some(result)
164        } else {
165            self.index = self.size;
166            None
167        }
168    }
169}
170
171impl<U, T: IndexMove<u32, U>> ::core::iter::ExactSizeIterator for ListIter<T, U> {
172    fn len(&self) -> usize {
173        self.size as usize - self.index as usize
174    }
175}
176
177impl<U, T: IndexMove<u32, U>> ::core::iter::DoubleEndedIterator for ListIter<T, U> {
178    fn next_back(&mut self) -> ::core::option::Option<U> {
179        if self.size > self.index {
180            self.size -= 1;
181            Some(self.list.index_move(self.size))
182        } else {
183            None
184        }
185    }
186}
187
188/// Iterator for a list whose indices are of type `u16`.
189pub struct ShortListIter<T, U> {
190    marker: PhantomData<U>,
191    list: T,
192    index: u16,
193    size: u16,
194}
195
196impl<T, U> ShortListIter<T, U> {
197    pub fn new(list: T, size: u16) -> Self {
198        Self {
199            list,
200            index: 0,
201            size,
202            marker: PhantomData,
203        }
204    }
205}
206
207impl<U, T: IndexMove<u16, U>> ::core::iter::Iterator for ShortListIter<T, U> {
208    type Item = U;
209    fn next(&mut self) -> ::core::option::Option<U> {
210        if self.index < self.size {
211            let result = self.list.index_move(self.index);
212            self.index += 1;
213            Some(result)
214        } else {
215            None
216        }
217    }
218
219    fn size_hint(&self) -> (usize, Option<usize>) {
220        (self.size as usize, Some(self.size as usize))
221    }
222
223    fn nth(&mut self, p: usize) -> Option<U> {
224        if self.index + (p as u16) < self.size {
225            self.index += p as u16;
226            let result = self.list.index_move(self.index);
227            self.index += 1;
228            Some(result)
229        } else {
230            self.index = self.size;
231            None
232        }
233    }
234}
235
236impl<U, T: IndexMove<u16, U>> ::core::iter::ExactSizeIterator for ShortListIter<T, U> {
237    fn len(&self) -> usize {
238        self.size as usize - self.index as usize
239    }
240}
241
242impl<U, T: IndexMove<u16, U>> ::core::iter::DoubleEndedIterator for ShortListIter<T, U> {
243    fn next_back(&mut self) -> ::core::option::Option<U> {
244        if self.size > self.index {
245            self.size -= 1;
246            Some(self.list.index_move(self.size))
247        } else {
248            None
249        }
250    }
251}