/rust/registry/src/index.crates.io-1949cf8c6b5b557f/storekey-0.11.0/src/decode.rs
Line | Count | Source |
1 | | use std::borrow::Cow; |
2 | | use std::collections::{BTreeMap, BTreeSet}; |
3 | | use std::io::BufRead; |
4 | | use std::mem::MaybeUninit; |
5 | | use std::ops::Bound; |
6 | | use std::time::Duration; |
7 | | |
8 | | use super::reader::BorrowReader; |
9 | | use super::{BorrowDecode, Decode, Reader}; |
10 | | use crate::DecodeError; |
11 | | |
12 | | impl<F> Decode<F> for bool { |
13 | 0 | fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError> { |
14 | 0 | match r.read_u8()? { |
15 | 0 | 2 => Ok(false), |
16 | 0 | 3 => Ok(true), |
17 | 0 | _ => Err(DecodeError::InvalidFormat), |
18 | | } |
19 | 0 | } |
20 | | } |
21 | | |
22 | | impl<F> Decode<F> for char { |
23 | 0 | fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError> { |
24 | 0 | let c = r.read_u32()?; |
25 | 0 | char::from_u32(c).ok_or(DecodeError::InvalidFormat) |
26 | 0 | } |
27 | | } |
28 | | |
29 | | impl<F> Decode<F> for String { |
30 | 0 | fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError> { |
31 | 0 | r.read_string() |
32 | 0 | } |
33 | | } |
34 | | |
35 | | impl<F, D: Decode<F>> Decode<F> for Option<D> { |
36 | 0 | fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError> { |
37 | 0 | match r.read_u8()? { |
38 | | // Don't use 0 or 1 as those need to be escaped. |
39 | | // Todo: Maybe keep it backwards compatible. |
40 | 0 | 2 => Ok(None), |
41 | 0 | 3 => Ok(Some(Decode::decode(r)?)), |
42 | 0 | _ => Err(DecodeError::InvalidFormat), |
43 | | } |
44 | 0 | } |
45 | | } |
46 | | |
47 | | impl<F, D: Decode<F>> Decode<F> for Bound<D> { |
48 | 0 | fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError> { |
49 | 0 | match r.read_u8()? { |
50 | 0 | 2 => Ok(Bound::Unbounded), |
51 | 0 | 3 => Ok(Bound::Included(Decode::<F>::decode(r)?)), |
52 | 0 | 4 => Ok(Bound::Excluded(Decode::<F>::decode(r)?)), |
53 | 0 | _ => Err(DecodeError::InvalidFormat), |
54 | | } |
55 | 0 | } |
56 | | } |
57 | | |
58 | | impl<'a, F, O> Decode<F> for Cow<'a, O> |
59 | | where |
60 | | O: ToOwned + ?Sized, |
61 | | O::Owned: Decode<F>, |
62 | | { |
63 | 0 | fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError> { |
64 | 0 | Ok(Cow::Owned(O::Owned::decode(r)?)) |
65 | 0 | } |
66 | | } |
67 | | |
68 | | impl<F, O: Decode<F>, E: Decode<F>> Decode<F> for Result<O, E> { |
69 | 0 | fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError> { |
70 | 0 | match r.read_u8()? { |
71 | | // Don't use 0 or 1 as those need to be escaped. |
72 | | // Todo: Maybe keep it backwards compatible. |
73 | 0 | 2 => Ok(Ok(Decode::decode(r)?)), |
74 | 0 | 3 => Ok(Err(Decode::decode(r)?)), |
75 | 0 | _ => Err(DecodeError::InvalidFormat), |
76 | | } |
77 | 0 | } |
78 | | } |
79 | | |
80 | | impl<F, D: Decode<F>> Decode<F> for Vec<D> { |
81 | 0 | fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError> { |
82 | | // TODO: Castaway optimize Vec<u8>? |
83 | 0 | let mut buffer = Vec::new(); |
84 | | |
85 | 0 | while !r.read_terminal()? { |
86 | 0 | buffer.push(D::decode(r)?); |
87 | | } |
88 | | |
89 | 0 | Ok(buffer) |
90 | 0 | } |
91 | | } |
92 | | |
93 | | impl<F, D: Decode<F>> Decode<F> for Box<D> { |
94 | 0 | fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError> { |
95 | 0 | Ok(Box::new(D::decode(r)?)) |
96 | 0 | } |
97 | | } |
98 | | |
99 | | impl<F, K: Decode<F> + Ord, V: Decode<F>> Decode<F> for BTreeMap<K, V> { |
100 | 0 | fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError> { |
101 | 0 | let mut res = BTreeMap::default(); |
102 | | |
103 | 0 | while !r.read_terminal()? { |
104 | 0 | let k = K::decode(r)?; |
105 | 0 | let v = V::decode(r)?; |
106 | 0 | res.insert(k, v); |
107 | | } |
108 | | |
109 | 0 | Ok(res) |
110 | 0 | } |
111 | | } |
112 | | |
113 | | impl<F, T: Decode<F> + Ord> Decode<F> for BTreeSet<T> { |
114 | 0 | fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError> { |
115 | 0 | let mut res = BTreeSet::default(); |
116 | | |
117 | 0 | while !r.read_terminal()? { |
118 | 0 | let item = T::decode(r)?; |
119 | 0 | res.insert(item); |
120 | | } |
121 | | |
122 | 0 | Ok(res) |
123 | 0 | } |
124 | | } |
125 | | |
126 | | impl<F, T: Decode<F> + Sized, const SIZE: usize> Decode<F> for [T; SIZE] { |
127 | 0 | fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError> { |
128 | 0 | let mut res: MaybeUninit<[T; SIZE]> = MaybeUninit::uninit(); |
129 | | // dropper to properly clean up after a possible panics. |
130 | | // |
131 | | // Holds onto the mutable reference and the init count so it can drop all initialized |
132 | | // entries when if the function quits early. |
133 | | struct Dropper<'a, T, const SIZE: usize>(usize, &'a mut [MaybeUninit<T>; SIZE]); |
134 | | impl<T, const SIZE: usize> Drop for Dropper<'_, T, SIZE> { |
135 | 0 | fn drop(&mut self) { |
136 | 0 | for i in 0..self.0 { |
137 | 0 | unsafe { self.1[i].assume_init_drop() } |
138 | | } |
139 | 0 | } |
140 | | } |
141 | | |
142 | | // safety: Transmute is safe because the MaybeUninit<[T; S]> has the same representation as |
143 | | // [MaybeUninit<T>; S] |
144 | 0 | let mut dropper = Dropper::<T, SIZE>(0, unsafe { |
145 | 0 | std::mem::transmute::<&mut MaybeUninit<[T; SIZE]>, &mut [MaybeUninit<T>; SIZE]>( |
146 | 0 | &mut res, |
147 | 0 | ) |
148 | 0 | }); |
149 | | |
150 | 0 | while dropper.0 < SIZE { |
151 | 0 | dropper.1[dropper.0] = MaybeUninit::new(T::decode(r)?); |
152 | 0 | dropper.0 += 1; |
153 | | } |
154 | | |
155 | | // We have successfully initialized the array so new we forget the dropper so it won't |
156 | | // unitialize the fields. |
157 | 0 | std::mem::forget(dropper); |
158 | | |
159 | | // safety: All fields are now initialized. |
160 | 0 | unsafe { Ok(res.assume_init()) } |
161 | 0 | } |
162 | | } |
163 | | |
164 | | macro_rules! impl_decode_tuple { |
165 | | ($($t:ident),*$(,)?) => { |
166 | | impl<Format, $($t: Decode<Format>),*> Decode<Format> for ($($t,)*){ |
167 | | #[allow(non_snake_case)] |
168 | 0 | fn decode<R: BufRead>(_r: &mut Reader<R>) -> Result<Self, DecodeError> { |
169 | 0 | $(let $t = $t::decode(_r)?;)* |
170 | | |
171 | 0 | Ok(( |
172 | 0 | $($t,)* |
173 | 0 | )) |
174 | 0 | } Unexecuted instantiation: <() as storekey::Decode<_>>::decode::<_> Unexecuted instantiation: <(_,) as storekey::Decode<_>>::decode::<_> Unexecuted instantiation: <(_, _) as storekey::Decode<_>>::decode::<_> Unexecuted instantiation: <(_, _, _) as storekey::Decode<_>>::decode::<_> Unexecuted instantiation: <(_, _, _, _) as storekey::Decode<_>>::decode::<_> Unexecuted instantiation: <(_, _, _, _, _) as storekey::Decode<_>>::decode::<_> Unexecuted instantiation: <(_, _, _, _, _, _) as storekey::Decode<_>>::decode::<_> |
175 | | } |
176 | | |
177 | | }; |
178 | | } |
179 | | |
180 | | impl_decode_tuple!(); |
181 | | impl_decode_tuple!(A); |
182 | | impl_decode_tuple!(A, B); |
183 | | impl_decode_tuple!(A, B, C); |
184 | | impl_decode_tuple!(A, B, C, D); |
185 | | impl_decode_tuple!(A, B, C, D, E); |
186 | | impl_decode_tuple!(A, B, C, D, E, F); |
187 | | |
188 | | macro_rules! impl_decode_prim { |
189 | | ($ty:ident,$name:ident) => { |
190 | | impl<F> Decode<F> for $ty { |
191 | 0 | fn decode<R: BufRead>(r: &mut Reader<R>) -> Result<Self, DecodeError> { |
192 | 0 | r.$name() |
193 | 0 | } Unexecuted instantiation: <u32 as storekey::Decode<_>>::decode::<_> Unexecuted instantiation: <i32 as storekey::Decode<_>>::decode::<_> Unexecuted instantiation: <u64 as storekey::Decode<_>>::decode::<_> Unexecuted instantiation: <i64 as storekey::Decode<_>>::decode::<_> Unexecuted instantiation: <u128 as storekey::Decode<_>>::decode::<_> Unexecuted instantiation: <i128 as storekey::Decode<_>>::decode::<_> Unexecuted instantiation: <f32 as storekey::Decode<_>>::decode::<_> Unexecuted instantiation: <f64 as storekey::Decode<_>>::decode::<_> Unexecuted instantiation: <u8 as storekey::Decode<_>>::decode::<_> Unexecuted instantiation: <i8 as storekey::Decode<_>>::decode::<_> Unexecuted instantiation: <u16 as storekey::Decode<_>>::decode::<_> Unexecuted instantiation: <i16 as storekey::Decode<_>>::decode::<_> |
194 | | } |
195 | | }; |
196 | | } |
197 | | |
198 | | impl_decode_prim!(u8, read_u8); |
199 | | impl_decode_prim!(i8, read_i8); |
200 | | impl_decode_prim!(u16, read_u16); |
201 | | impl_decode_prim!(i16, read_i16); |
202 | | impl_decode_prim!(u32, read_u32); |
203 | | impl_decode_prim!(i32, read_i32); |
204 | | impl_decode_prim!(u64, read_u64); |
205 | | impl_decode_prim!(i64, read_i64); |
206 | | impl_decode_prim!(u128, read_u128); |
207 | | impl_decode_prim!(i128, read_i128); |
208 | | impl_decode_prim!(f32, read_f32); |
209 | | impl_decode_prim!(f64, read_f64); |
210 | | |
211 | | impl<'de, F> BorrowDecode<'de, F> for bool { |
212 | 0 | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { |
213 | 0 | match r.read_u8()? { |
214 | 0 | 2 => Ok(false), |
215 | 0 | 3 => Ok(true), |
216 | 0 | _ => Err(DecodeError::InvalidFormat), |
217 | | } |
218 | 0 | } Unexecuted instantiation: <bool as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode Unexecuted instantiation: <bool as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <bool as storekey::BorrowDecode<_>>::borrow_decode |
219 | | } |
220 | | |
221 | | impl<'de, F> BorrowDecode<'de, F> for char { |
222 | 0 | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { |
223 | 0 | char::from_u32(r.read_u32()?).ok_or(DecodeError::InvalidFormat) |
224 | 0 | } |
225 | | } |
226 | | |
227 | | impl<'de, F> BorrowDecode<'de, F> for String { |
228 | 0 | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { |
229 | 0 | r.read_string() |
230 | 0 | } Unexecuted instantiation: <alloc::string::String as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode Unexecuted instantiation: <alloc::string::String as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <alloc::string::String as storekey::BorrowDecode<_>>::borrow_decode |
231 | | } |
232 | | |
233 | | impl<'de, F, D: BorrowDecode<'de, F>> BorrowDecode<'de, F> for Option<D> { |
234 | 0 | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { |
235 | 0 | match r.read_u8()? { |
236 | 0 | 2 => Ok(None), |
237 | 0 | 3 => Ok(Some(D::borrow_decode(r)?)), |
238 | 0 | _ => Err(DecodeError::InvalidFormat), |
239 | | } |
240 | 0 | } Unexecuted instantiation: <core::option::Option<alloc::borrow::Cow<surrealdb_core::val::record_id::RecordIdKey>> as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode Unexecuted instantiation: <core::option::Option<(uuid::Uuid, uuid::Uuid)> as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <core::option::Option<_> as storekey::BorrowDecode<_>>::borrow_decode |
241 | | } |
242 | | |
243 | | impl<'de, F, D: BorrowDecode<'de, F>> BorrowDecode<'de, F> for Bound<D> { |
244 | 0 | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { |
245 | 0 | match r.read_u8()? { |
246 | 0 | 2 => Ok(Bound::Unbounded), |
247 | 0 | 3 => Ok(Bound::Included(BorrowDecode::<'de, F>::borrow_decode(r)?)), |
248 | 0 | 4 => Ok(Bound::Excluded(BorrowDecode::<'de, F>::borrow_decode(r)?)), |
249 | 0 | _ => Err(DecodeError::InvalidFormat), |
250 | | } |
251 | 0 | } Unexecuted instantiation: <core::ops::range::Bound<surrealdb_core::val::Value> as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode Unexecuted instantiation: <core::ops::range::Bound<surrealdb_core::val::Value> as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <core::ops::range::Bound<surrealdb_core::val::record_id::RecordIdKey> as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode Unexecuted instantiation: <core::ops::range::Bound<surrealdb_core::val::record_id::RecordIdKey> as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <core::ops::range::Bound<_> as storekey::BorrowDecode<_>>::borrow_decode |
252 | | } |
253 | | |
254 | | impl<'de, F, O: BorrowDecode<'de, F>, E: BorrowDecode<'de, F>> BorrowDecode<'de, F> |
255 | | for Result<O, E> |
256 | | { |
257 | 0 | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { |
258 | 0 | match r.read_u8()? { |
259 | 0 | 2 => Ok(Ok(O::borrow_decode(r)?)), |
260 | 0 | 3 => Ok(Err(E::borrow_decode(r)?)), |
261 | 0 | _ => Err(DecodeError::InvalidFormat), |
262 | | } |
263 | 0 | } |
264 | | } |
265 | | |
266 | | impl<'de, F> BorrowDecode<'de, F> for Cow<'de, [u8]> { |
267 | 0 | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { |
268 | 0 | r.read_cow() |
269 | 0 | } Unexecuted instantiation: <alloc::borrow::Cow<[u8]> as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <alloc::borrow::Cow<[u8]> as storekey::BorrowDecode<_>>::borrow_decode |
270 | | } |
271 | | |
272 | | impl<'de, F> BorrowDecode<'de, F> for Cow<'de, str> { |
273 | 0 | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { |
274 | 0 | r.read_str_cow() |
275 | 0 | } Unexecuted instantiation: <alloc::borrow::Cow<str> as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <alloc::borrow::Cow<str> as storekey::BorrowDecode<_>>::borrow_decode |
276 | | } |
277 | | |
278 | | impl<'de, F, D: BorrowDecode<'de, F>> BorrowDecode<'de, F> for Cow<'de, D> |
279 | | where |
280 | | D: ToOwned<Owned = D> + BorrowDecode<'de, F>, |
281 | | { |
282 | 2.93k | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { |
283 | 2.93k | Ok(Cow::Owned(D::borrow_decode(r)?)) |
284 | 2.93k | } Unexecuted instantiation: <alloc::borrow::Cow<surrealdb_core::val::array::Array> as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode Unexecuted instantiation: <alloc::borrow::Cow<surrealdb_core::val::table::TableName> as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode <alloc::borrow::Cow<surrealdb_core::val::table::TableName> as storekey::BorrowDecode>::borrow_decode Line | Count | Source | 282 | 2.93k | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { | 283 | 2.93k | Ok(Cow::Owned(D::borrow_decode(r)?)) | 284 | 2.93k | } |
Unexecuted instantiation: <alloc::borrow::Cow<surrealdb_core::val::record_id::RecordIdKey> as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode Unexecuted instantiation: <alloc::borrow::Cow<surrealdb_core::val::record_id::RecordIdKey> as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <alloc::borrow::Cow<surrealdb_core::idx::trees::vector::SerializedVector> as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <alloc::borrow::Cow<_> as storekey::BorrowDecode<_>>::borrow_decode |
285 | | } |
286 | | |
287 | | impl<'de, F> BorrowDecode<'de, F> for Duration { |
288 | 0 | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { |
289 | 0 | let secs = r.read_u64()?; |
290 | 0 | let subsec_nanos = r.read_u32()?; |
291 | 0 | if subsec_nanos > 999_999_999 { |
292 | 0 | return Err(DecodeError::message("Duration subsec nanoseconds was out of range")); |
293 | 0 | } |
294 | 0 | Ok(Duration::new(secs, subsec_nanos)) |
295 | 0 | } Unexecuted instantiation: <core::time::Duration as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode Unexecuted instantiation: <core::time::Duration as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <core::time::Duration as storekey::BorrowDecode<_>>::borrow_decode |
296 | | } |
297 | | |
298 | | impl<'de, F, D: BorrowDecode<'de, F>> BorrowDecode<'de, F> for Box<D> { |
299 | 0 | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { |
300 | 0 | Ok(Box::new(D::borrow_decode(r)?)) |
301 | 0 | } Unexecuted instantiation: <alloc::boxed::Box<surrealdb_core::val::range::Range> as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode Unexecuted instantiation: <alloc::boxed::Box<surrealdb_core::val::range::Range> as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <alloc::boxed::Box<surrealdb_core::val::closure::Closure> as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode Unexecuted instantiation: <alloc::boxed::Box<surrealdb_core::val::closure::Closure> as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <alloc::boxed::Box<surrealdb_core::val::record_id::RecordIdKeyRange> as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode Unexecuted instantiation: <alloc::boxed::Box<surrealdb_core::val::record_id::RecordIdKeyRange> as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <alloc::boxed::Box<_> as storekey::BorrowDecode<_>>::borrow_decode |
302 | | } |
303 | | |
304 | | impl<'de, F, D: BorrowDecode<'de, F>> BorrowDecode<'de, F> for Vec<D> { |
305 | 0 | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { |
306 | | // TODO: Castaway optimize Vec<u8>? |
307 | 0 | let mut buffer = Vec::new(); |
308 | | |
309 | 0 | while !r.read_terminal()? { |
310 | 0 | buffer.push(D::borrow_decode(r)?); |
311 | | } |
312 | | |
313 | 0 | Ok(buffer) |
314 | 0 | } Unexecuted instantiation: <alloc::vec::Vec<surrealdb_core::val::Value> as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode Unexecuted instantiation: <alloc::vec::Vec<surrealdb_core::val::Value> as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <alloc::vec::Vec<_> as storekey::BorrowDecode<_>>::borrow_decode |
315 | | } |
316 | | |
317 | | impl<'de, F, K: BorrowDecode<'de, F> + Ord, V: BorrowDecode<'de, F>> BorrowDecode<'de, F> |
318 | | for BTreeMap<K, V> |
319 | | { |
320 | 0 | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { |
321 | 0 | let mut res = BTreeMap::default(); |
322 | | |
323 | 0 | while !r.read_terminal()? { |
324 | 0 | let k = K::borrow_decode(r)?; |
325 | 0 | let v = V::borrow_decode(r)?; |
326 | 0 | res.insert(k, v); |
327 | | } |
328 | | |
329 | 0 | Ok(res) |
330 | 0 | } |
331 | | } |
332 | | |
333 | | impl<'de, F, T: BorrowDecode<'de, F> + Ord> BorrowDecode<'de, F> for BTreeSet<T> { |
334 | 0 | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { |
335 | 0 | let mut res = BTreeSet::default(); |
336 | | |
337 | 0 | while !r.read_terminal()? { |
338 | 0 | let item = T::borrow_decode(r)?; |
339 | 0 | res.insert(item); |
340 | | } |
341 | | |
342 | 0 | Ok(res) |
343 | 0 | } |
344 | | } |
345 | | |
346 | | impl<'de, F, T: BorrowDecode<'de, F> + Sized, const SIZE: usize> BorrowDecode<'de, F> |
347 | | for [T; SIZE] |
348 | | { |
349 | 0 | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { |
350 | | // TODO: Castaway optimize [T;SIZE]? |
351 | 0 | let mut res: MaybeUninit<[T; SIZE]> = MaybeUninit::uninit(); |
352 | | // dropper to properly clean up after a possible panics. |
353 | | // |
354 | | // Holds onto the mutable reference and the init count so it can drop all initialized |
355 | | // entries if the function quits early. |
356 | | struct Dropper<'a, T, const SIZE: usize>(usize, &'a mut [MaybeUninit<T>; SIZE]); |
357 | | impl<T, const SIZE: usize> Drop for Dropper<'_, T, SIZE> { |
358 | 0 | fn drop(&mut self) { |
359 | 0 | for i in 0..self.0 { |
360 | 0 | unsafe { self.1[i].assume_init_drop() } |
361 | | } |
362 | 0 | } Unexecuted instantiation: <<[_; _] as storekey::BorrowDecode<_>>::borrow_decode::Dropper<u8, 32> as core::ops::drop::Drop>::drop Unexecuted instantiation: <<[_; _] as storekey::BorrowDecode<_>>::borrow_decode::Dropper<_, _> as core::ops::drop::Drop>::drop |
363 | | } |
364 | | |
365 | | // safety: Transmute is safe because the MaybeUninit<[T; S]> has the same representation as |
366 | | // [MaybeUninit<T>; S] |
367 | 0 | let mut dropper = Dropper::<T, SIZE>(0, unsafe { |
368 | 0 | std::mem::transmute::<&mut MaybeUninit<[T; SIZE]>, &mut [MaybeUninit<T>; SIZE]>( |
369 | 0 | &mut res, |
370 | 0 | ) |
371 | 0 | }); |
372 | | |
373 | 0 | while dropper.0 < SIZE { |
374 | 0 | dropper.1[dropper.0] = MaybeUninit::new(T::borrow_decode(r)?); |
375 | 0 | dropper.0 += 1; |
376 | | } |
377 | | |
378 | | // We have successfully initialized the array so new we forget the dropper so it won't |
379 | | // unitialize the fields. |
380 | 0 | std::mem::forget(dropper); |
381 | | |
382 | | // safety: All fields are now initialized. |
383 | 0 | unsafe { Ok(res.assume_init()) } |
384 | 0 | } Unexecuted instantiation: <[u8; 32] as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <[_; _] as storekey::BorrowDecode<_>>::borrow_decode |
385 | | } |
386 | | |
387 | | macro_rules! impl_borrow_decode_tuple { |
388 | | ($($t:ident),*$(,)?) => { |
389 | | impl <'de,Format, $($t: BorrowDecode<'de,Format>),*> BorrowDecode<'de,Format> for ($($t,)*){ |
390 | | #[allow(non_snake_case)] |
391 | 0 | fn borrow_decode(_r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { |
392 | 0 | $(let $t = $t::borrow_decode(_r)?;)* |
393 | | |
394 | 0 | Ok(( |
395 | 0 | $($t,)* |
396 | 0 | )) |
397 | 0 | } Unexecuted instantiation: <(f64, f64) as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode Unexecuted instantiation: <(f64, f64) as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <(uuid::Uuid, uuid::Uuid) as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <() as storekey::BorrowDecode<_>>::borrow_decode Unexecuted instantiation: <(_,) as storekey::BorrowDecode<_>>::borrow_decode Unexecuted instantiation: <(_, _) as storekey::BorrowDecode<_>>::borrow_decode Unexecuted instantiation: <(_, _, _) as storekey::BorrowDecode<_>>::borrow_decode Unexecuted instantiation: <(_, _, _, _) as storekey::BorrowDecode<_>>::borrow_decode Unexecuted instantiation: <(_, _, _, _, _) as storekey::BorrowDecode<_>>::borrow_decode Unexecuted instantiation: <(_, _, _, _, _, _) as storekey::BorrowDecode<_>>::borrow_decode |
398 | | } |
399 | | |
400 | | }; |
401 | | } |
402 | | |
403 | | impl_borrow_decode_tuple!(); |
404 | | impl_borrow_decode_tuple!(A); |
405 | | impl_borrow_decode_tuple!(A, B); |
406 | | impl_borrow_decode_tuple!(A, B, C); |
407 | | impl_borrow_decode_tuple!(A, B, C, D); |
408 | | impl_borrow_decode_tuple!(A, B, C, D, E); |
409 | | impl_borrow_decode_tuple!(A, B, C, D, E, F); |
410 | | |
411 | | macro_rules! impl_borrow_decode_prim { |
412 | | ($ty:ident,$name:ident) => { |
413 | | impl<'de, F> BorrowDecode<'de, F> for $ty { |
414 | 23.7k | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { |
415 | 23.7k | r.$name() |
416 | 23.7k | } Unexecuted instantiation: <f64 as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode Unexecuted instantiation: <f64 as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <u8 as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode <u8 as storekey::BorrowDecode>::borrow_decode Line | Count | Source | 414 | 17.6k | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { | 415 | 17.6k | r.$name() | 416 | 17.6k | } |
Unexecuted instantiation: <u16 as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode Unexecuted instantiation: <u16 as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <u32 as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode <u32 as storekey::BorrowDecode>::borrow_decode Line | Count | Source | 414 | 5.87k | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { | 415 | 5.87k | r.$name() | 416 | 5.87k | } |
Unexecuted instantiation: <u64 as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode Unexecuted instantiation: <u64 as storekey::BorrowDecode>::borrow_decode Unexecuted instantiation: <i64 as storekey::BorrowDecode<surrealdb_core::val::IndexFormat>>::borrow_decode <i64 as storekey::BorrowDecode>::borrow_decode Line | Count | Source | 414 | 302 | fn borrow_decode(r: &mut BorrowReader<'de>) -> Result<Self, DecodeError> { | 415 | 302 | r.$name() | 416 | 302 | } |
Unexecuted instantiation: <f64 as storekey::BorrowDecode<_>>::borrow_decode Unexecuted instantiation: <u8 as storekey::BorrowDecode<_>>::borrow_decode Unexecuted instantiation: <i8 as storekey::BorrowDecode<_>>::borrow_decode Unexecuted instantiation: <u16 as storekey::BorrowDecode<_>>::borrow_decode Unexecuted instantiation: <i16 as storekey::BorrowDecode<_>>::borrow_decode Unexecuted instantiation: <u32 as storekey::BorrowDecode<_>>::borrow_decode Unexecuted instantiation: <i32 as storekey::BorrowDecode<_>>::borrow_decode Unexecuted instantiation: <u64 as storekey::BorrowDecode<_>>::borrow_decode Unexecuted instantiation: <i64 as storekey::BorrowDecode<_>>::borrow_decode Unexecuted instantiation: <u128 as storekey::BorrowDecode<_>>::borrow_decode Unexecuted instantiation: <i128 as storekey::BorrowDecode<_>>::borrow_decode Unexecuted instantiation: <f32 as storekey::BorrowDecode<_>>::borrow_decode |
417 | | } |
418 | | }; |
419 | | } |
420 | | |
421 | | impl_borrow_decode_prim!(u8, read_u8); |
422 | | impl_borrow_decode_prim!(i8, read_i8); |
423 | | impl_borrow_decode_prim!(u16, read_u16); |
424 | | impl_borrow_decode_prim!(i16, read_i16); |
425 | | impl_borrow_decode_prim!(u32, read_u32); |
426 | | impl_borrow_decode_prim!(i32, read_i32); |
427 | | impl_borrow_decode_prim!(u64, read_u64); |
428 | | impl_borrow_decode_prim!(i64, read_i64); |
429 | | impl_borrow_decode_prim!(u128, read_u128); |
430 | | impl_borrow_decode_prim!(i128, read_i128); |
431 | | impl_borrow_decode_prim!(f32, read_f32); |
432 | | impl_borrow_decode_prim!(f64, read_f64); |