fidl_test_tablevalidation_common/
fidl_test_tablevalidation_common.rs

1// WARNING: This file is machine generated by fidlgen.
2
3#![warn(clippy::all)]
4#![allow(unused_parens, unused_mut, unused_imports, nonstandard_style)]
5
6use bitflags::bitflags;
7use fidl::encoding::{MessageBufFor, ProxyChannelBox, ResourceDialect};
8use futures::future::{self, MaybeDone, TryFutureExt};
9use zx_status;
10
11#[derive(Clone, Debug, Default, PartialEq)]
12pub struct Example {
13    pub num: Option<u32>,
14    pub new_field_not_in_validated_type: Option<String>,
15    #[doc(hidden)]
16    pub __source_breaking: fidl::marker::SourceBreaking,
17}
18
19impl fidl::Persistable for Example {}
20
21#[derive(Clone, Debug, Default, PartialEq)]
22pub struct VecOfExample {
23    pub vec: Option<Vec<Example>>,
24    #[doc(hidden)]
25    pub __source_breaking: fidl::marker::SourceBreaking,
26}
27
28impl fidl::Persistable for VecOfExample {}
29
30#[derive(Clone, Debug, Default, PartialEq)]
31pub struct WrapExample {
32    pub inner: Option<Example>,
33    #[doc(hidden)]
34    pub __source_breaking: fidl::marker::SourceBreaking,
35}
36
37impl fidl::Persistable for WrapExample {}
38
39mod internal {
40    use super::*;
41
42    impl Example {
43        #[inline(always)]
44        fn max_ordinal_present(&self) -> u64 {
45            if let Some(_) = self.new_field_not_in_validated_type {
46                return 2;
47            }
48            if let Some(_) = self.num {
49                return 1;
50            }
51            0
52        }
53    }
54
55    impl fidl::encoding::ValueTypeMarker for Example {
56        type Borrowed<'a> = &'a Self;
57        fn borrow(value: &<Self as fidl::encoding::TypeMarker>::Owned) -> Self::Borrowed<'_> {
58            value
59        }
60    }
61
62    unsafe impl fidl::encoding::TypeMarker for Example {
63        type Owned = Self;
64
65        #[inline(always)]
66        fn inline_align(_context: fidl::encoding::Context) -> usize {
67            8
68        }
69
70        #[inline(always)]
71        fn inline_size(_context: fidl::encoding::Context) -> usize {
72            16
73        }
74    }
75
76    unsafe impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Encode<Example, D> for &Example {
77        unsafe fn encode(
78            self,
79            encoder: &mut fidl::encoding::Encoder<'_, D>,
80            offset: usize,
81            mut depth: fidl::encoding::Depth,
82        ) -> fidl::Result<()> {
83            encoder.debug_check_bounds::<Example>(offset);
84            // Vector header
85            let max_ordinal: u64 = self.max_ordinal_present();
86            encoder.write_num(max_ordinal, offset);
87            encoder.write_num(fidl::encoding::ALLOC_PRESENT_U64, offset + 8);
88            // Calling encoder.out_of_line_offset(0) is not allowed.
89            if max_ordinal == 0 {
90                return Ok(());
91            }
92            depth.increment()?;
93            let envelope_size = 8;
94            let bytes_len = max_ordinal as usize * envelope_size;
95            #[allow(unused_variables)]
96            let offset = encoder.out_of_line_offset(bytes_len);
97            let mut _prev_end_offset: usize = 0;
98            if 1 > max_ordinal {
99                return Ok(());
100            }
101
102            // Write at offset+(ordinal-1)*envelope_size, since ordinals are one-based and envelopes
103            // are envelope_size bytes.
104            let cur_offset: usize = (1 - 1) * envelope_size;
105
106            // Zero reserved fields.
107            encoder.padding(offset + _prev_end_offset, cur_offset - _prev_end_offset);
108
109            // Safety:
110            // - bytes_len is calculated to fit envelope_size*max(member.ordinal).
111            // - Since cur_offset is envelope_size*(member.ordinal - 1) and the envelope takes
112            //   envelope_size bytes, there is always sufficient room.
113            fidl::encoding::encode_in_envelope_optional::<u32, D>(
114                self.num.as_ref().map(<u32 as fidl::encoding::ValueTypeMarker>::borrow),
115                encoder,
116                offset + cur_offset,
117                depth,
118            )?;
119
120            _prev_end_offset = cur_offset + envelope_size;
121            if 2 > max_ordinal {
122                return Ok(());
123            }
124
125            // Write at offset+(ordinal-1)*envelope_size, since ordinals are one-based and envelopes
126            // are envelope_size bytes.
127            let cur_offset: usize = (2 - 1) * envelope_size;
128
129            // Zero reserved fields.
130            encoder.padding(offset + _prev_end_offset, cur_offset - _prev_end_offset);
131
132            // Safety:
133            // - bytes_len is calculated to fit envelope_size*max(member.ordinal).
134            // - Since cur_offset is envelope_size*(member.ordinal - 1) and the envelope takes
135            //   envelope_size bytes, there is always sufficient room.
136            fidl::encoding::encode_in_envelope_optional::<fidl::encoding::UnboundedString, D>(
137                self.new_field_not_in_validated_type.as_ref().map(
138                    <fidl::encoding::UnboundedString as fidl::encoding::ValueTypeMarker>::borrow,
139                ),
140                encoder,
141                offset + cur_offset,
142                depth,
143            )?;
144
145            _prev_end_offset = cur_offset + envelope_size;
146
147            Ok(())
148        }
149    }
150
151    impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Decode<Self, D> for Example {
152        #[inline(always)]
153        fn new_empty() -> Self {
154            Self::default()
155        }
156
157        unsafe fn decode(
158            &mut self,
159            decoder: &mut fidl::encoding::Decoder<'_, D>,
160            offset: usize,
161            mut depth: fidl::encoding::Depth,
162        ) -> fidl::Result<()> {
163            decoder.debug_check_bounds::<Self>(offset);
164            let len = match fidl::encoding::decode_vector_header(decoder, offset)? {
165                None => return Err(fidl::Error::NotNullable),
166                Some(len) => len,
167            };
168            // Calling decoder.out_of_line_offset(0) is not allowed.
169            if len == 0 {
170                return Ok(());
171            };
172            depth.increment()?;
173            let envelope_size = 8;
174            let bytes_len = len * envelope_size;
175            let offset = decoder.out_of_line_offset(bytes_len)?;
176            // Decode the envelope for each type.
177            let mut _next_ordinal_to_read = 0;
178            let mut next_offset = offset;
179            let end_offset = offset + bytes_len;
180            _next_ordinal_to_read += 1;
181            if next_offset >= end_offset {
182                return Ok(());
183            }
184
185            // Decode unknown envelopes for gaps in ordinals.
186            while _next_ordinal_to_read < 1 {
187                fidl::encoding::decode_unknown_envelope(decoder, next_offset, depth)?;
188                _next_ordinal_to_read += 1;
189                next_offset += envelope_size;
190            }
191
192            let next_out_of_line = decoder.next_out_of_line();
193            let handles_before = decoder.remaining_handles();
194            if let Some((inlined, num_bytes, num_handles)) =
195                fidl::encoding::decode_envelope_header(decoder, next_offset)?
196            {
197                let member_inline_size =
198                    <u32 as fidl::encoding::TypeMarker>::inline_size(decoder.context);
199                if inlined != (member_inline_size <= 4) {
200                    return Err(fidl::Error::InvalidInlineBitInEnvelope);
201                }
202                let inner_offset;
203                let mut inner_depth = depth.clone();
204                if inlined {
205                    decoder.check_inline_envelope_padding(next_offset, member_inline_size)?;
206                    inner_offset = next_offset;
207                } else {
208                    inner_offset = decoder.out_of_line_offset(member_inline_size)?;
209                    inner_depth.increment()?;
210                }
211                let val_ref = self.num.get_or_insert_with(|| fidl::new_empty!(u32, D));
212                fidl::decode!(u32, D, val_ref, decoder, inner_offset, inner_depth)?;
213                if !inlined && decoder.next_out_of_line() != next_out_of_line + (num_bytes as usize)
214                {
215                    return Err(fidl::Error::InvalidNumBytesInEnvelope);
216                }
217                if handles_before != decoder.remaining_handles() + (num_handles as usize) {
218                    return Err(fidl::Error::InvalidNumHandlesInEnvelope);
219                }
220            }
221
222            next_offset += envelope_size;
223            _next_ordinal_to_read += 1;
224            if next_offset >= end_offset {
225                return Ok(());
226            }
227
228            // Decode unknown envelopes for gaps in ordinals.
229            while _next_ordinal_to_read < 2 {
230                fidl::encoding::decode_unknown_envelope(decoder, next_offset, depth)?;
231                _next_ordinal_to_read += 1;
232                next_offset += envelope_size;
233            }
234
235            let next_out_of_line = decoder.next_out_of_line();
236            let handles_before = decoder.remaining_handles();
237            if let Some((inlined, num_bytes, num_handles)) =
238                fidl::encoding::decode_envelope_header(decoder, next_offset)?
239            {
240                let member_inline_size =
241                    <fidl::encoding::UnboundedString as fidl::encoding::TypeMarker>::inline_size(
242                        decoder.context,
243                    );
244                if inlined != (member_inline_size <= 4) {
245                    return Err(fidl::Error::InvalidInlineBitInEnvelope);
246                }
247                let inner_offset;
248                let mut inner_depth = depth.clone();
249                if inlined {
250                    decoder.check_inline_envelope_padding(next_offset, member_inline_size)?;
251                    inner_offset = next_offset;
252                } else {
253                    inner_offset = decoder.out_of_line_offset(member_inline_size)?;
254                    inner_depth.increment()?;
255                }
256                let val_ref = self
257                    .new_field_not_in_validated_type
258                    .get_or_insert_with(|| fidl::new_empty!(fidl::encoding::UnboundedString, D));
259                fidl::decode!(
260                    fidl::encoding::UnboundedString,
261                    D,
262                    val_ref,
263                    decoder,
264                    inner_offset,
265                    inner_depth
266                )?;
267                if !inlined && decoder.next_out_of_line() != next_out_of_line + (num_bytes as usize)
268                {
269                    return Err(fidl::Error::InvalidNumBytesInEnvelope);
270                }
271                if handles_before != decoder.remaining_handles() + (num_handles as usize) {
272                    return Err(fidl::Error::InvalidNumHandlesInEnvelope);
273                }
274            }
275
276            next_offset += envelope_size;
277
278            // Decode the remaining unknown envelopes.
279            while next_offset < end_offset {
280                _next_ordinal_to_read += 1;
281                fidl::encoding::decode_unknown_envelope(decoder, next_offset, depth)?;
282                next_offset += envelope_size;
283            }
284
285            Ok(())
286        }
287    }
288
289    impl VecOfExample {
290        #[inline(always)]
291        fn max_ordinal_present(&self) -> u64 {
292            if let Some(_) = self.vec {
293                return 1;
294            }
295            0
296        }
297    }
298
299    impl fidl::encoding::ValueTypeMarker for VecOfExample {
300        type Borrowed<'a> = &'a Self;
301        fn borrow(value: &<Self as fidl::encoding::TypeMarker>::Owned) -> Self::Borrowed<'_> {
302            value
303        }
304    }
305
306    unsafe impl fidl::encoding::TypeMarker for VecOfExample {
307        type Owned = Self;
308
309        #[inline(always)]
310        fn inline_align(_context: fidl::encoding::Context) -> usize {
311            8
312        }
313
314        #[inline(always)]
315        fn inline_size(_context: fidl::encoding::Context) -> usize {
316            16
317        }
318    }
319
320    unsafe impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Encode<VecOfExample, D>
321        for &VecOfExample
322    {
323        unsafe fn encode(
324            self,
325            encoder: &mut fidl::encoding::Encoder<'_, D>,
326            offset: usize,
327            mut depth: fidl::encoding::Depth,
328        ) -> fidl::Result<()> {
329            encoder.debug_check_bounds::<VecOfExample>(offset);
330            // Vector header
331            let max_ordinal: u64 = self.max_ordinal_present();
332            encoder.write_num(max_ordinal, offset);
333            encoder.write_num(fidl::encoding::ALLOC_PRESENT_U64, offset + 8);
334            // Calling encoder.out_of_line_offset(0) is not allowed.
335            if max_ordinal == 0 {
336                return Ok(());
337            }
338            depth.increment()?;
339            let envelope_size = 8;
340            let bytes_len = max_ordinal as usize * envelope_size;
341            #[allow(unused_variables)]
342            let offset = encoder.out_of_line_offset(bytes_len);
343            let mut _prev_end_offset: usize = 0;
344            if 1 > max_ordinal {
345                return Ok(());
346            }
347
348            // Write at offset+(ordinal-1)*envelope_size, since ordinals are one-based and envelopes
349            // are envelope_size bytes.
350            let cur_offset: usize = (1 - 1) * envelope_size;
351
352            // Zero reserved fields.
353            encoder.padding(offset + _prev_end_offset, cur_offset - _prev_end_offset);
354
355            // Safety:
356            // - bytes_len is calculated to fit envelope_size*max(member.ordinal).
357            // - Since cur_offset is envelope_size*(member.ordinal - 1) and the envelope takes
358            //   envelope_size bytes, there is always sufficient room.
359            fidl::encoding::encode_in_envelope_optional::<fidl::encoding::UnboundedVector<Example>, D>(
360            self.vec.as_ref().map(<fidl::encoding::UnboundedVector<Example> as fidl::encoding::ValueTypeMarker>::borrow),
361            encoder, offset + cur_offset, depth
362        )?;
363
364            _prev_end_offset = cur_offset + envelope_size;
365
366            Ok(())
367        }
368    }
369
370    impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Decode<Self, D> for VecOfExample {
371        #[inline(always)]
372        fn new_empty() -> Self {
373            Self::default()
374        }
375
376        unsafe fn decode(
377            &mut self,
378            decoder: &mut fidl::encoding::Decoder<'_, D>,
379            offset: usize,
380            mut depth: fidl::encoding::Depth,
381        ) -> fidl::Result<()> {
382            decoder.debug_check_bounds::<Self>(offset);
383            let len = match fidl::encoding::decode_vector_header(decoder, offset)? {
384                None => return Err(fidl::Error::NotNullable),
385                Some(len) => len,
386            };
387            // Calling decoder.out_of_line_offset(0) is not allowed.
388            if len == 0 {
389                return Ok(());
390            };
391            depth.increment()?;
392            let envelope_size = 8;
393            let bytes_len = len * envelope_size;
394            let offset = decoder.out_of_line_offset(bytes_len)?;
395            // Decode the envelope for each type.
396            let mut _next_ordinal_to_read = 0;
397            let mut next_offset = offset;
398            let end_offset = offset + bytes_len;
399            _next_ordinal_to_read += 1;
400            if next_offset >= end_offset {
401                return Ok(());
402            }
403
404            // Decode unknown envelopes for gaps in ordinals.
405            while _next_ordinal_to_read < 1 {
406                fidl::encoding::decode_unknown_envelope(decoder, next_offset, depth)?;
407                _next_ordinal_to_read += 1;
408                next_offset += envelope_size;
409            }
410
411            let next_out_of_line = decoder.next_out_of_line();
412            let handles_before = decoder.remaining_handles();
413            if let Some((inlined, num_bytes, num_handles)) =
414                fidl::encoding::decode_envelope_header(decoder, next_offset)?
415            {
416                let member_inline_size = <fidl::encoding::UnboundedVector<Example> as fidl::encoding::TypeMarker>::inline_size(decoder.context);
417                if inlined != (member_inline_size <= 4) {
418                    return Err(fidl::Error::InvalidInlineBitInEnvelope);
419                }
420                let inner_offset;
421                let mut inner_depth = depth.clone();
422                if inlined {
423                    decoder.check_inline_envelope_padding(next_offset, member_inline_size)?;
424                    inner_offset = next_offset;
425                } else {
426                    inner_offset = decoder.out_of_line_offset(member_inline_size)?;
427                    inner_depth.increment()?;
428                }
429                let val_ref = self.vec.get_or_insert_with(|| {
430                    fidl::new_empty!(fidl::encoding::UnboundedVector<Example>, D)
431                });
432                fidl::decode!(
433                    fidl::encoding::UnboundedVector<Example>,
434                    D,
435                    val_ref,
436                    decoder,
437                    inner_offset,
438                    inner_depth
439                )?;
440                if !inlined && decoder.next_out_of_line() != next_out_of_line + (num_bytes as usize)
441                {
442                    return Err(fidl::Error::InvalidNumBytesInEnvelope);
443                }
444                if handles_before != decoder.remaining_handles() + (num_handles as usize) {
445                    return Err(fidl::Error::InvalidNumHandlesInEnvelope);
446                }
447            }
448
449            next_offset += envelope_size;
450
451            // Decode the remaining unknown envelopes.
452            while next_offset < end_offset {
453                _next_ordinal_to_read += 1;
454                fidl::encoding::decode_unknown_envelope(decoder, next_offset, depth)?;
455                next_offset += envelope_size;
456            }
457
458            Ok(())
459        }
460    }
461
462    impl WrapExample {
463        #[inline(always)]
464        fn max_ordinal_present(&self) -> u64 {
465            if let Some(_) = self.inner {
466                return 1;
467            }
468            0
469        }
470    }
471
472    impl fidl::encoding::ValueTypeMarker for WrapExample {
473        type Borrowed<'a> = &'a Self;
474        fn borrow(value: &<Self as fidl::encoding::TypeMarker>::Owned) -> Self::Borrowed<'_> {
475            value
476        }
477    }
478
479    unsafe impl fidl::encoding::TypeMarker for WrapExample {
480        type Owned = Self;
481
482        #[inline(always)]
483        fn inline_align(_context: fidl::encoding::Context) -> usize {
484            8
485        }
486
487        #[inline(always)]
488        fn inline_size(_context: fidl::encoding::Context) -> usize {
489            16
490        }
491    }
492
493    unsafe impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Encode<WrapExample, D>
494        for &WrapExample
495    {
496        unsafe fn encode(
497            self,
498            encoder: &mut fidl::encoding::Encoder<'_, D>,
499            offset: usize,
500            mut depth: fidl::encoding::Depth,
501        ) -> fidl::Result<()> {
502            encoder.debug_check_bounds::<WrapExample>(offset);
503            // Vector header
504            let max_ordinal: u64 = self.max_ordinal_present();
505            encoder.write_num(max_ordinal, offset);
506            encoder.write_num(fidl::encoding::ALLOC_PRESENT_U64, offset + 8);
507            // Calling encoder.out_of_line_offset(0) is not allowed.
508            if max_ordinal == 0 {
509                return Ok(());
510            }
511            depth.increment()?;
512            let envelope_size = 8;
513            let bytes_len = max_ordinal as usize * envelope_size;
514            #[allow(unused_variables)]
515            let offset = encoder.out_of_line_offset(bytes_len);
516            let mut _prev_end_offset: usize = 0;
517            if 1 > max_ordinal {
518                return Ok(());
519            }
520
521            // Write at offset+(ordinal-1)*envelope_size, since ordinals are one-based and envelopes
522            // are envelope_size bytes.
523            let cur_offset: usize = (1 - 1) * envelope_size;
524
525            // Zero reserved fields.
526            encoder.padding(offset + _prev_end_offset, cur_offset - _prev_end_offset);
527
528            // Safety:
529            // - bytes_len is calculated to fit envelope_size*max(member.ordinal).
530            // - Since cur_offset is envelope_size*(member.ordinal - 1) and the envelope takes
531            //   envelope_size bytes, there is always sufficient room.
532            fidl::encoding::encode_in_envelope_optional::<Example, D>(
533                self.inner.as_ref().map(<Example as fidl::encoding::ValueTypeMarker>::borrow),
534                encoder,
535                offset + cur_offset,
536                depth,
537            )?;
538
539            _prev_end_offset = cur_offset + envelope_size;
540
541            Ok(())
542        }
543    }
544
545    impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Decode<Self, D> for WrapExample {
546        #[inline(always)]
547        fn new_empty() -> Self {
548            Self::default()
549        }
550
551        unsafe fn decode(
552            &mut self,
553            decoder: &mut fidl::encoding::Decoder<'_, D>,
554            offset: usize,
555            mut depth: fidl::encoding::Depth,
556        ) -> fidl::Result<()> {
557            decoder.debug_check_bounds::<Self>(offset);
558            let len = match fidl::encoding::decode_vector_header(decoder, offset)? {
559                None => return Err(fidl::Error::NotNullable),
560                Some(len) => len,
561            };
562            // Calling decoder.out_of_line_offset(0) is not allowed.
563            if len == 0 {
564                return Ok(());
565            };
566            depth.increment()?;
567            let envelope_size = 8;
568            let bytes_len = len * envelope_size;
569            let offset = decoder.out_of_line_offset(bytes_len)?;
570            // Decode the envelope for each type.
571            let mut _next_ordinal_to_read = 0;
572            let mut next_offset = offset;
573            let end_offset = offset + bytes_len;
574            _next_ordinal_to_read += 1;
575            if next_offset >= end_offset {
576                return Ok(());
577            }
578
579            // Decode unknown envelopes for gaps in ordinals.
580            while _next_ordinal_to_read < 1 {
581                fidl::encoding::decode_unknown_envelope(decoder, next_offset, depth)?;
582                _next_ordinal_to_read += 1;
583                next_offset += envelope_size;
584            }
585
586            let next_out_of_line = decoder.next_out_of_line();
587            let handles_before = decoder.remaining_handles();
588            if let Some((inlined, num_bytes, num_handles)) =
589                fidl::encoding::decode_envelope_header(decoder, next_offset)?
590            {
591                let member_inline_size =
592                    <Example as fidl::encoding::TypeMarker>::inline_size(decoder.context);
593                if inlined != (member_inline_size <= 4) {
594                    return Err(fidl::Error::InvalidInlineBitInEnvelope);
595                }
596                let inner_offset;
597                let mut inner_depth = depth.clone();
598                if inlined {
599                    decoder.check_inline_envelope_padding(next_offset, member_inline_size)?;
600                    inner_offset = next_offset;
601                } else {
602                    inner_offset = decoder.out_of_line_offset(member_inline_size)?;
603                    inner_depth.increment()?;
604                }
605                let val_ref = self.inner.get_or_insert_with(|| fidl::new_empty!(Example, D));
606                fidl::decode!(Example, D, val_ref, decoder, inner_offset, inner_depth)?;
607                if !inlined && decoder.next_out_of_line() != next_out_of_line + (num_bytes as usize)
608                {
609                    return Err(fidl::Error::InvalidNumBytesInEnvelope);
610                }
611                if handles_before != decoder.remaining_handles() + (num_handles as usize) {
612                    return Err(fidl::Error::InvalidNumHandlesInEnvelope);
613                }
614            }
615
616            next_offset += envelope_size;
617
618            // Decode the remaining unknown envelopes.
619            while next_offset < end_offset {
620                _next_ordinal_to_read += 1;
621                fidl::encoding::decode_unknown_envelope(decoder, next_offset, depth)?;
622                next_offset += envelope_size;
623            }
624
625            Ok(())
626        }
627    }
628}