/rust/registry/src/index.crates.io-1949cf8c6b5b557f/flatbuffers-25.12.19/src/vector.rs
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2018 Google Inc. All rights reserved. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | use core::cmp::Ordering; |
18 | | use core::fmt::{Debug, Formatter, Result}; |
19 | | use core::iter::{DoubleEndedIterator, ExactSizeIterator, FusedIterator}; |
20 | | #[cfg(nightly)] |
21 | | use core::iter::TrustedLen; |
22 | | use core::marker::PhantomData; |
23 | | use core::mem::{align_of, size_of}; |
24 | | use core::str::from_utf8_unchecked; |
25 | | |
26 | | use crate::endian_scalar::read_scalar_at; |
27 | | use crate::follow::Follow; |
28 | | use crate::primitives::*; |
29 | | |
30 | | pub struct Vector<'a, T: 'a>(&'a [u8], usize, PhantomData<T>); |
31 | | |
32 | | impl<'a, T: 'a> Default for Vector<'a, T> { |
33 | 0 | fn default() -> Self { |
34 | | // Static, length 0 vector. |
35 | | // Note that derived default causes UB due to issues in read_scalar_at /facepalm. |
36 | 0 | Self(&[0; core::mem::size_of::<UOffsetT>()], 0, Default::default()) |
37 | 0 | } |
38 | | } |
39 | | |
40 | | impl<'a, T> Debug for Vector<'a, T> |
41 | | where |
42 | | T: 'a + Follow<'a>, |
43 | | <T as Follow<'a>>::Inner: Debug, |
44 | | { |
45 | 0 | fn fmt(&self, f: &mut Formatter) -> Result { |
46 | 0 | f.debug_list().entries(self.iter()).finish() |
47 | 0 | } Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::kind_generated::Kind>> as core::fmt::Debug>::fmt Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::point_generated::Point>> as core::fmt::Debug>::fmt Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::value_generated::Value>> as core::fmt::Debug>::fmt Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::polygon_generated::Polygon>> as core::fmt::Debug>::fmt Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_generated::Geometry>> as core::fmt::Debug>::fmt Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::key_value_generated::KeyValue>> as core::fmt::Debug>::fmt Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::line_string_generated::LineString>> as core::fmt::Debug>::fmt Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::object_field_generated::ObjectField>> as core::fmt::Debug>::fmt Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::literal_object_generated::LiteralObject>> as core::fmt::Debug>::fmt Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<&str>> as core::fmt::Debug>::fmt Unexecuted instantiation: <flatbuffers::vector::Vector<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_kind_type_generated::GeometryKindType> as core::fmt::Debug>::fmt Unexecuted instantiation: <flatbuffers::vector::Vector<u8> as core::fmt::Debug>::fmt Unexecuted instantiation: <flatbuffers::vector::Vector<_> as core::fmt::Debug>::fmt |
48 | | } |
49 | | |
50 | | // We cannot use derive for these two impls, as it would only implement Copy |
51 | | // and Clone for `T: Copy` and `T: Clone` respectively. However `Vector<'a, T>` |
52 | | // can always be copied, no matter that `T` you have. |
53 | | impl<'a, T> Copy for Vector<'a, T> {} |
54 | | |
55 | | impl<'a, T> Clone for Vector<'a, T> { |
56 | 0 | fn clone(&self) -> Self { |
57 | 0 | *self |
58 | 0 | } |
59 | | } |
60 | | |
61 | | impl<'a, T: 'a> Vector<'a, T> { |
62 | | /// # Safety |
63 | | /// |
64 | | /// `buf` contains a valid vector at `loc` consisting of |
65 | | /// |
66 | | /// - UOffsetT element count |
67 | | /// - Consecutive list of `T` elements |
68 | | #[inline(always)] |
69 | 0 | pub unsafe fn new(buf: &'a [u8], loc: usize) -> Self { |
70 | 0 | Vector(buf, loc, PhantomData) |
71 | 0 | } Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::kind_generated::Kind>>>::new Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::point_generated::Point>>>::new Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::value_generated::Value>>>::new Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::polygon_generated::Polygon>>>::new Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_generated::Geometry>>>::new Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::key_value_generated::KeyValue>>>::new Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::line_string_generated::LineString>>>::new Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::object_field_generated::ObjectField>>>::new Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::literal_object_generated::LiteralObject>>>::new Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<&str>>>::new Unexecuted instantiation: <flatbuffers::vector::Vector<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_kind_type_generated::GeometryKindType>>::new Unexecuted instantiation: <flatbuffers::vector::Vector<u8>>::new Unexecuted instantiation: <flatbuffers::vector::Vector<_>>::new |
72 | | |
73 | | #[inline(always)] |
74 | 0 | pub fn len(&self) -> usize { |
75 | | // Safety: |
76 | | // Valid vector at time of construction starting with UOffsetT element count |
77 | 0 | unsafe { read_scalar_at::<UOffsetT>(self.0, self.1) as usize } |
78 | 0 | } Unexecuted instantiation: <flatbuffers::vector::Vector<u8>>::len Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::key_value_generated::KeyValue>>>::len Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_generated::Geometry>>>::len Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::kind_generated::Kind>>>::len Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::point_generated::Point>>>::len Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::value_generated::Value>>>::len Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::polygon_generated::Polygon>>>::len Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_generated::Geometry>>>::len Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::key_value_generated::KeyValue>>>::len Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::line_string_generated::LineString>>>::len Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::object_field_generated::ObjectField>>>::len Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::literal_object_generated::LiteralObject>>>::len Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<&str>>>::len Unexecuted instantiation: <flatbuffers::vector::Vector<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_kind_type_generated::GeometryKindType>>::len Unexecuted instantiation: <flatbuffers::vector::Vector<u8>>::len Unexecuted instantiation: <flatbuffers::vector::Vector<_>>::len |
79 | | |
80 | | #[inline(always)] |
81 | 0 | pub fn is_empty(&self) -> bool { |
82 | 0 | self.len() == 0 |
83 | 0 | } Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::key_value_generated::KeyValue>>>::is_empty Unexecuted instantiation: <flatbuffers::vector::Vector<_>>::is_empty |
84 | | |
85 | | #[inline(always)] |
86 | 0 | pub fn bytes(&self) -> &'a [u8] { |
87 | 0 | let sz = size_of::<T>(); |
88 | 0 | let len = self.len(); |
89 | 0 | &self.0[self.1 + SIZE_UOFFSET..self.1 + SIZE_UOFFSET + sz * len] |
90 | 0 | } Unexecuted instantiation: <flatbuffers::vector::Vector<u8>>::bytes Unexecuted instantiation: <flatbuffers::vector::Vector<_>>::bytes |
91 | | } |
92 | | |
93 | | impl<'a, T: Follow<'a> + 'a> Vector<'a, T> { |
94 | | #[inline(always)] |
95 | 0 | pub fn get(&self, idx: usize) -> T::Inner { |
96 | 0 | assert!(idx < self.len()); |
97 | 0 | let sz = size_of::<T>(); |
98 | 0 | debug_assert!(sz > 0); |
99 | | // Safety: |
100 | | // Valid vector at time of construction, verified that idx < element count |
101 | 0 | unsafe { T::follow(self.0, self.1 as usize + SIZE_UOFFSET + sz * idx) } |
102 | 0 | } |
103 | | |
104 | | #[inline(always)] |
105 | 0 | pub fn lookup_by_key<K: Ord>( |
106 | 0 | &self, |
107 | 0 | key: K, |
108 | 0 | f: fn(&<T as Follow<'a>>::Inner, &K) -> Ordering, |
109 | 0 | ) -> Option<T::Inner> { |
110 | 0 | if self.is_empty() { |
111 | 0 | return None; |
112 | 0 | } |
113 | | |
114 | 0 | let mut left: usize = 0; |
115 | 0 | let mut right = self.len() - 1; |
116 | | |
117 | 0 | while left <= right { |
118 | 0 | let mid = (left + right) / 2; |
119 | 0 | let value = self.get(mid); |
120 | 0 | match f(&value, &key) { |
121 | 0 | Ordering::Equal => return Some(value), |
122 | 0 | Ordering::Less => left = mid + 1, |
123 | | Ordering::Greater => { |
124 | 0 | if mid == 0 { |
125 | 0 | return None; |
126 | 0 | } |
127 | 0 | right = mid - 1; |
128 | | } |
129 | | } |
130 | | } |
131 | | |
132 | 0 | None |
133 | 0 | } |
134 | | |
135 | | #[inline(always)] |
136 | 0 | pub fn iter(&self) -> VectorIter<'a, T> { |
137 | 0 | VectorIter::from_vector(*self) |
138 | 0 | } Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::point_generated::Point>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::value_generated::Value>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::polygon_generated::Polygon>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_generated::Geometry>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::key_value_generated::KeyValue>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::line_string_generated::LineString>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::kind_generated::Kind>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<&str>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_kind_type_generated::GeometryKindType>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::object_field_generated::ObjectField>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::kind_generated::Kind>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::point_generated::Point>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::value_generated::Value>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::polygon_generated::Polygon>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_generated::Geometry>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::key_value_generated::KeyValue>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::line_string_generated::LineString>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::object_field_generated::ObjectField>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::literal_object_generated::LiteralObject>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<&str>>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_kind_type_generated::GeometryKindType>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<u8>>::iter Unexecuted instantiation: <flatbuffers::vector::Vector<_>>::iter |
139 | | } |
140 | | |
141 | | /// # Safety |
142 | | /// |
143 | | /// `buf` must contain a value of T at `loc` and have alignment of 1 |
144 | 0 | pub unsafe fn follow_cast_ref<'a, T: Sized + 'a>(buf: &'a [u8], loc: usize) -> &'a T { |
145 | 0 | assert_eq!(align_of::<T>(), 1); |
146 | 0 | let sz = size_of::<T>(); |
147 | 0 | let buf = &buf[loc..loc + sz]; |
148 | 0 | let ptr = buf.as_ptr() as *const T; |
149 | | // SAFETY |
150 | | // buf contains a value at loc of type T and T has no alignment requirements |
151 | 0 | &*ptr |
152 | 0 | } |
153 | | |
154 | | impl<'a> Follow<'a> for &'a str { |
155 | | type Inner = &'a str; |
156 | 0 | unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { |
157 | 0 | let len = read_scalar_at::<UOffsetT>(buf, loc) as usize; |
158 | 0 | let slice = &buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len]; |
159 | 0 | from_utf8_unchecked(slice) |
160 | 0 | } |
161 | | } |
162 | | |
163 | | impl<'a> Follow<'a> for &'a [u8] { |
164 | | type Inner = &'a [u8]; |
165 | 0 | unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { |
166 | 0 | let len = read_scalar_at::<UOffsetT>(buf, loc) as usize; |
167 | 0 | &buf[loc + SIZE_UOFFSET..loc + SIZE_UOFFSET + len] |
168 | 0 | } |
169 | | } |
170 | | |
171 | | /// Implement Follow for all possible Vectors that have Follow-able elements. |
172 | | impl<'a, T: Follow<'a> + 'a> Follow<'a> for Vector<'a, T> { |
173 | | type Inner = Vector<'a, T>; |
174 | 0 | unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { |
175 | 0 | Vector::new(buf, loc) |
176 | 0 | } Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::kind_generated::Kind>> as flatbuffers::follow::Follow>::follow Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::point_generated::Point>> as flatbuffers::follow::Follow>::follow Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::value_generated::Value>> as flatbuffers::follow::Follow>::follow Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::polygon_generated::Polygon>> as flatbuffers::follow::Follow>::follow Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_generated::Geometry>> as flatbuffers::follow::Follow>::follow Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::key_value_generated::KeyValue>> as flatbuffers::follow::Follow>::follow Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::line_string_generated::LineString>> as flatbuffers::follow::Follow>::follow Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::object_field_generated::ObjectField>> as flatbuffers::follow::Follow>::follow Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::literal_object_generated::LiteralObject>> as flatbuffers::follow::Follow>::follow Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<&str>> as flatbuffers::follow::Follow>::follow Unexecuted instantiation: <flatbuffers::vector::Vector<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_kind_type_generated::GeometryKindType> as flatbuffers::follow::Follow>::follow Unexecuted instantiation: <flatbuffers::vector::Vector<u8> as flatbuffers::follow::Follow>::follow Unexecuted instantiation: <flatbuffers::vector::Vector<_> as flatbuffers::follow::Follow>::follow |
177 | | } |
178 | | |
179 | | /// An iterator over a `Vector`. |
180 | | #[derive(Debug)] |
181 | | pub struct VectorIter<'a, T: 'a> { |
182 | | buf: &'a [u8], |
183 | | loc: usize, |
184 | | remaining: usize, |
185 | | phantom: PhantomData<T>, |
186 | | } |
187 | | |
188 | | impl<'a, T: 'a> VectorIter<'a, T> { |
189 | | #[inline] |
190 | 0 | pub fn from_vector(inner: Vector<'a, T>) -> Self { |
191 | 0 | VectorIter { |
192 | 0 | buf: inner.0, |
193 | 0 | // inner.1 is the location of the data for the vector. |
194 | 0 | // The first SIZE_UOFFSET bytes is the length. We skip |
195 | 0 | // that to get to the actual vector content. |
196 | 0 | loc: inner.1 + SIZE_UOFFSET, |
197 | 0 | remaining: inner.len(), |
198 | 0 | phantom: PhantomData, |
199 | 0 | } |
200 | 0 | } Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::kind_generated::Kind>>>::from_vector Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::point_generated::Point>>>::from_vector Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::value_generated::Value>>>::from_vector Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::polygon_generated::Polygon>>>::from_vector Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_generated::Geometry>>>::from_vector Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::key_value_generated::KeyValue>>>::from_vector Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::line_string_generated::LineString>>>::from_vector Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::object_field_generated::ObjectField>>>::from_vector Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::literal_object_generated::LiteralObject>>>::from_vector Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<&str>>>::from_vector Unexecuted instantiation: <flatbuffers::vector::VectorIter<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_kind_type_generated::GeometryKindType>>::from_vector Unexecuted instantiation: <flatbuffers::vector::VectorIter<u8>>::from_vector Unexecuted instantiation: <flatbuffers::vector::VectorIter<_>>::from_vector |
201 | | |
202 | | /// Creates a new `VectorIter` from the provided slice |
203 | | /// |
204 | | /// # Safety |
205 | | /// |
206 | | /// buf must contain a contiguous sequence of `items_num` values of `T` |
207 | | /// |
208 | | #[inline] |
209 | 0 | pub unsafe fn from_slice(buf: &'a [u8], items_num: usize) -> Self { |
210 | 0 | VectorIter { buf, loc: 0, remaining: items_num, phantom: PhantomData } |
211 | 0 | } |
212 | | } |
213 | | |
214 | | impl<'a, T: Follow<'a> + 'a> Clone for VectorIter<'a, T> { |
215 | | #[inline] |
216 | 0 | fn clone(&self) -> Self { |
217 | 0 | VectorIter { |
218 | 0 | buf: self.buf, |
219 | 0 | loc: self.loc, |
220 | 0 | remaining: self.remaining, |
221 | 0 | phantom: self.phantom, |
222 | 0 | } |
223 | 0 | } |
224 | | } |
225 | | |
226 | | impl<'a, T: Follow<'a> + 'a> Iterator for VectorIter<'a, T> { |
227 | | type Item = T::Inner; |
228 | | |
229 | | #[inline] |
230 | 0 | fn next(&mut self) -> Option<T::Inner> { |
231 | 0 | let sz = size_of::<T>(); |
232 | 0 | debug_assert!(sz > 0); |
233 | | |
234 | 0 | if self.remaining == 0 { |
235 | 0 | None |
236 | | } else { |
237 | | // Safety: |
238 | | // VectorIter can only be created from a contiguous sequence of `items_num` |
239 | | // And remaining is initialized to `items_num` |
240 | 0 | let result = unsafe { T::follow(self.buf, self.loc) }; |
241 | 0 | self.loc += sz; |
242 | 0 | self.remaining -= 1; |
243 | 0 | Some(result) |
244 | | } |
245 | 0 | } Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::kind_generated::Kind>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::point_generated::Point>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::value_generated::Value>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::polygon_generated::Polygon>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_generated::Geometry>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::key_value_generated::KeyValue>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::line_string_generated::LineString>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::object_field_generated::ObjectField>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::literal_object_generated::LiteralObject>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<&str>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <flatbuffers::vector::VectorIter<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_kind_type_generated::GeometryKindType> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <flatbuffers::vector::VectorIter<u8> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <flatbuffers::vector::VectorIter<_> as core::iter::traits::iterator::Iterator>::next |
246 | | |
247 | | #[inline] |
248 | 0 | fn nth(&mut self, n: usize) -> Option<T::Inner> { |
249 | 0 | let sz = size_of::<T>(); |
250 | 0 | debug_assert!(sz > 0); |
251 | | |
252 | 0 | self.remaining = self.remaining.saturating_sub(n); |
253 | | |
254 | | // Note that this might overflow, but that is okay because |
255 | | // in that case self.remaining will have been set to zero. |
256 | 0 | self.loc = self.loc.wrapping_add(sz * n); |
257 | | |
258 | 0 | self.next() |
259 | 0 | } |
260 | | |
261 | | #[inline] |
262 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
263 | 0 | (self.remaining, Some(self.remaining)) |
264 | 0 | } Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::kind_generated::Kind>> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <flatbuffers::vector::VectorIter<flatbuffers::primitives::ForwardsUOffset<&str>> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <flatbuffers::vector::VectorIter<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_kind_type_generated::GeometryKindType> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <flatbuffers::vector::VectorIter<_> as core::iter::traits::iterator::Iterator>::size_hint |
265 | | } |
266 | | |
267 | | impl<'a, T: Follow<'a> + 'a> DoubleEndedIterator for VectorIter<'a, T> { |
268 | | #[inline] |
269 | 0 | fn next_back(&mut self) -> Option<T::Inner> { |
270 | 0 | let sz = size_of::<T>(); |
271 | 0 | debug_assert!(sz > 0); |
272 | | |
273 | 0 | if self.remaining == 0 { |
274 | 0 | None |
275 | | } else { |
276 | 0 | self.remaining -= 1; |
277 | | // Safety: |
278 | | // VectorIter can only be created from a contiguous sequence of `items_num` |
279 | | // And remaining is initialized to `items_num` |
280 | 0 | Some(unsafe { T::follow(self.buf, self.loc + sz * self.remaining) }) |
281 | | } |
282 | 0 | } |
283 | | |
284 | | #[inline] |
285 | 0 | fn nth_back(&mut self, n: usize) -> Option<T::Inner> { |
286 | 0 | self.remaining = self.remaining.saturating_sub(n); |
287 | 0 | self.next_back() |
288 | 0 | } |
289 | | } |
290 | | |
291 | | impl<'a, T: 'a + Follow<'a>> ExactSizeIterator for VectorIter<'a, T> { |
292 | | #[inline] |
293 | 0 | fn len(&self) -> usize { |
294 | 0 | self.remaining |
295 | 0 | } |
296 | | } |
297 | | |
298 | | #[cfg(nightly)] |
299 | | unsafe impl<'a, T: Follow<'a> + 'a> TrustedLen for VectorIter<'a, T> {} |
300 | | |
301 | | impl<'a, T: 'a + Follow<'a>> FusedIterator for VectorIter<'a, T> {} |
302 | | |
303 | | impl<'a, T: Follow<'a> + 'a> IntoIterator for Vector<'a, T> { |
304 | | type Item = T::Inner; |
305 | | type IntoIter = VectorIter<'a, T>; |
306 | | #[inline] |
307 | 0 | fn into_iter(self) -> Self::IntoIter { |
308 | 0 | self.iter() |
309 | 0 | } Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::point_generated::Point>> as core::iter::traits::collect::IntoIterator>::into_iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::value_generated::Value>> as core::iter::traits::collect::IntoIterator>::into_iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::polygon_generated::Polygon>> as core::iter::traits::collect::IntoIterator>::into_iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::geometry_generated::Geometry>> as core::iter::traits::collect::IntoIterator>::into_iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::key_value_generated::KeyValue>> as core::iter::traits::collect::IntoIterator>::into_iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::line_string_generated::LineString>> as core::iter::traits::collect::IntoIterator>::into_iter Unexecuted instantiation: <flatbuffers::vector::Vector<flatbuffers::primitives::ForwardsUOffset<surrealdb_protocol::fb::generated::surrealdb::protocol::v_1::object_field_generated::ObjectField>> as core::iter::traits::collect::IntoIterator>::into_iter Unexecuted instantiation: <flatbuffers::vector::Vector<_> as core::iter::traits::collect::IntoIterator>::into_iter |
310 | | } |
311 | | |
312 | | impl<'a, 'b, T: Follow<'a> + 'a> IntoIterator for &'b Vector<'a, T> { |
313 | | type Item = T::Inner; |
314 | | type IntoIter = VectorIter<'a, T>; |
315 | 0 | fn into_iter(self) -> Self::IntoIter { |
316 | 0 | self.iter() |
317 | 0 | } |
318 | | } |
319 | | |
320 | | #[cfg(feature = "serialize")] |
321 | | impl<'a, T> serde::ser::Serialize for Vector<'a, T> |
322 | | where |
323 | | T: 'a + Follow<'a>, |
324 | | <T as Follow<'a>>::Inner: serde::ser::Serialize, |
325 | | { |
326 | | fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> |
327 | | where |
328 | | S: serde::ser::Serializer, |
329 | | { |
330 | | use serde::ser::SerializeSeq; |
331 | | let mut seq = serializer.serialize_seq(Some(self.len()))?; |
332 | | for element in self { |
333 | | seq.serialize_element(&element)?; |
334 | | } |
335 | | seq.end() |
336 | | } |
337 | | } |