1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
// WARNING: This file is machine generated by fidlgen.

// fidl_experiment = transitional_allow_list

#![warn(clippy::all)]
#![allow(unused_parens, unused_mut, unused_imports, nonstandard_style)]

use {
    bitflags::bitflags,
    fidl::{
        client::QueryResponseFut,
        endpoints::{ControlHandle as _, Responder as _},
    },
    fuchsia_zircon_status as zx_status,
    futures::future::{self, MaybeDone, TryFutureExt},
};

#[cfg(target_os = "fuchsia")]
use fuchsia_zircon as zx;

/// A buffer for data whose size is not necessarily a multiple of the page
/// size.
///
/// DEPRECATED: This type is superfluous and deprecated. Instead of using this type,
/// use a zx.Handle:VMO object and store the size of the contents in the ZX_PROP_VMO_CONTENT_SIZE
/// property.
#[derive(Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Buffer {
    /// The vmo that contains the buffer.
    pub vmo: fidl::Vmo,
    /// The number of bytes in the buffer.
    ///
    /// The content of the buffer begin at the start of the VMO and continue
    /// for `size` bytes. To specify a range of bytes that do not start at
    /// the beginning of the VMO, use `Range` rather than buffer.
    ///
    /// This size must not be greater than the physical size of the VMO.
    pub size: u64,
}

impl fidl::Standalone for Buffer {}

/// A range of bytes within a VMO.
#[derive(Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Range {
    /// The vmo that contains the bytes.
    pub vmo: fidl::Vmo,
    /// The offset of the first byte within the range relative to the start of
    /// the VMO.
    ///
    /// For example, if `offset` is zero, then the first byte in the range is
    /// the first byte in the VMO.
    pub offset: u64,
    /// The number of bytes in the range.
    ///
    /// For example, if the offset is 3 and the size is 2, and the VMO starts
    /// with "abcdefg...", then the range contains "de".
    ///
    /// The sum of the offset and the size must not be greater than the
    /// physical size of the VMO.
    pub size: u64,
}

impl fidl::Standalone for Range {}

/// Binary data that might be stored inline or in a VMO.
///
/// Useful for performance-sensitive protocols that sometimes receive small
/// amounts of binary data (i.e., which is more efficient to provide using
/// `bytes`) but also need to support arbitrary amounts of data (i.e., which
/// need to be provided out-of-line in a `Buffer`).
#[derive(Debug)]
pub enum Data {
    /// The binary data provided inline in the message.
    Bytes(Vec<u8>),
    /// The binary data provided out-of-line in a `Buffer`.
    Buffer(Buffer),
    #[doc(hidden)]
    __SourceBreaking { unknown_ordinal: u64 },
}

/// Pattern that matches an unknown `Data` member.
#[macro_export]
macro_rules! DataUnknown {
    () => {
        _
    };
}

// Custom PartialEq so that unknown variants are not equal to themselves.
impl PartialEq for Data {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Bytes(x), Self::Bytes(y)) => *x == *y,
            (Self::Buffer(x), Self::Buffer(y)) => *x == *y,
            _ => false,
        }
    }
}

impl Data {
    #[inline]
    pub fn ordinal(&self) -> u64 {
        match *self {
            Self::Bytes(_) => 1,
            Self::Buffer(_) => 2,
            Self::__SourceBreaking { unknown_ordinal } => unknown_ordinal,
        }
    }

    #[inline]
    pub fn unknown_variant_for_testing() -> Self {
        Self::__SourceBreaking { unknown_ordinal: 0 }
    }

    #[inline]
    pub fn is_unknown(&self) -> bool {
        match self {
            Self::__SourceBreaking { .. } => true,
            _ => false,
        }
    }
}

impl fidl::Standalone for Data {}

mod internal {
    use super::*;

    unsafe impl fidl::encoding::TypeMarker for Buffer {
        type Owned = Self;

        #[inline(always)]
        fn inline_align(_context: fidl::encoding::Context) -> usize {
            8
        }

        #[inline(always)]
        fn inline_size(_context: fidl::encoding::Context) -> usize {
            16
        }
    }
    impl fidl::encoding::ResourceTypeMarker for Buffer {
        type Borrowed<'a> = &'a mut Self;
        fn take_or_borrow<'a>(
            value: &'a mut <Self as fidl::encoding::TypeMarker>::Owned,
        ) -> Self::Borrowed<'a> {
            value
        }
    }

    unsafe impl fidl::encoding::Encode<Buffer> for &mut Buffer {
        #[inline]
        unsafe fn encode(
            self,
            encoder: &mut fidl::encoding::Encoder<'_>,
            offset: usize,
            _depth: fidl::encoding::Depth,
        ) -> fidl::Result<()> {
            encoder.debug_check_bounds::<Buffer>(offset);
            // Delegate to tuple encoding.
            fidl::encoding::Encode::<Buffer>::encode(
                (
                    <fidl::encoding::HandleType<
                        fidl::Vmo,
                        { fidl::ObjectType::VMO.into_raw() },
                        2147483648,
                    > as fidl::encoding::ResourceTypeMarker>::take_or_borrow(
                        &mut self.vmo
                    ),
                    <u64 as fidl::encoding::ValueTypeMarker>::borrow(&self.size),
                ),
                encoder,
                offset,
                _depth,
            )
        }
    }
    unsafe impl<
            T0: fidl::encoding::Encode<
                fidl::encoding::HandleType<
                    fidl::Vmo,
                    { fidl::ObjectType::VMO.into_raw() },
                    2147483648,
                >,
            >,
            T1: fidl::encoding::Encode<u64>,
        > fidl::encoding::Encode<Buffer> for (T0, T1)
    {
        #[inline]
        unsafe fn encode(
            self,
            encoder: &mut fidl::encoding::Encoder<'_>,
            offset: usize,
            depth: fidl::encoding::Depth,
        ) -> fidl::Result<()> {
            encoder.debug_check_bounds::<Buffer>(offset);
            // Zero out padding regions. There's no need to apply masks
            // because the unmasked parts will be overwritten by fields.
            unsafe {
                let ptr = encoder.buf.as_mut_ptr().add(offset).offset(0);
                (ptr as *mut u64).write_unaligned(0);
            }
            // Write the fields.
            self.0.encode(encoder, offset + 0, depth)?;
            self.1.encode(encoder, offset + 8, depth)?;
            Ok(())
        }
    }

    impl fidl::encoding::Decode<Self> for Buffer {
        #[inline(always)]
        fn new_empty() -> Self {
            Self {
                vmo: fidl::new_empty!(fidl::encoding::HandleType<fidl::Vmo, { fidl::ObjectType::VMO.into_raw() }, 2147483648>),
                size: fidl::new_empty!(u64),
            }
        }

        #[inline]
        unsafe fn decode(
            &mut self,
            decoder: &mut fidl::encoding::Decoder<'_>,
            offset: usize,
            _depth: fidl::encoding::Depth,
        ) -> fidl::Result<()> {
            decoder.debug_check_bounds::<Self>(offset);
            // Verify that padding bytes are zero.
            let ptr = unsafe { decoder.buf.as_ptr().add(offset).offset(0) };
            let padval = unsafe { (ptr as *const u64).read_unaligned() };
            let mask = 0xffffffff00000000u64;
            let maskedval = padval & mask;
            if maskedval != 0 {
                return Err(fidl::Error::NonZeroPadding {
                    padding_start: offset + 0 + ((mask as u64).trailing_zeros() / 8) as usize,
                });
            }
            fidl::decode!(fidl::encoding::HandleType<fidl::Vmo, { fidl::ObjectType::VMO.into_raw() }, 2147483648>, &mut self.vmo, decoder, offset + 0, _depth)?;
            fidl::decode!(u64, &mut self.size, decoder, offset + 8, _depth)?;
            Ok(())
        }
    }

    unsafe impl fidl::encoding::TypeMarker for Range {
        type Owned = Self;

        #[inline(always)]
        fn inline_align(_context: fidl::encoding::Context) -> usize {
            8
        }

        #[inline(always)]
        fn inline_size(_context: fidl::encoding::Context) -> usize {
            24
        }
    }
    impl fidl::encoding::ResourceTypeMarker for Range {
        type Borrowed<'a> = &'a mut Self;
        fn take_or_borrow<'a>(
            value: &'a mut <Self as fidl::encoding::TypeMarker>::Owned,
        ) -> Self::Borrowed<'a> {
            value
        }
    }

    unsafe impl fidl::encoding::Encode<Range> for &mut Range {
        #[inline]
        unsafe fn encode(
            self,
            encoder: &mut fidl::encoding::Encoder<'_>,
            offset: usize,
            _depth: fidl::encoding::Depth,
        ) -> fidl::Result<()> {
            encoder.debug_check_bounds::<Range>(offset);
            // Delegate to tuple encoding.
            fidl::encoding::Encode::<Range>::encode(
                (
                    <fidl::encoding::HandleType<
                        fidl::Vmo,
                        { fidl::ObjectType::VMO.into_raw() },
                        2147483648,
                    > as fidl::encoding::ResourceTypeMarker>::take_or_borrow(
                        &mut self.vmo
                    ),
                    <u64 as fidl::encoding::ValueTypeMarker>::borrow(&self.offset),
                    <u64 as fidl::encoding::ValueTypeMarker>::borrow(&self.size),
                ),
                encoder,
                offset,
                _depth,
            )
        }
    }
    unsafe impl<
            T0: fidl::encoding::Encode<
                fidl::encoding::HandleType<
                    fidl::Vmo,
                    { fidl::ObjectType::VMO.into_raw() },
                    2147483648,
                >,
            >,
            T1: fidl::encoding::Encode<u64>,
            T2: fidl::encoding::Encode<u64>,
        > fidl::encoding::Encode<Range> for (T0, T1, T2)
    {
        #[inline]
        unsafe fn encode(
            self,
            encoder: &mut fidl::encoding::Encoder<'_>,
            offset: usize,
            depth: fidl::encoding::Depth,
        ) -> fidl::Result<()> {
            encoder.debug_check_bounds::<Range>(offset);
            // Zero out padding regions. There's no need to apply masks
            // because the unmasked parts will be overwritten by fields.
            unsafe {
                let ptr = encoder.buf.as_mut_ptr().add(offset).offset(0);
                (ptr as *mut u64).write_unaligned(0);
            }
            // Write the fields.
            self.0.encode(encoder, offset + 0, depth)?;
            self.1.encode(encoder, offset + 8, depth)?;
            self.2.encode(encoder, offset + 16, depth)?;
            Ok(())
        }
    }

    impl fidl::encoding::Decode<Self> for Range {
        #[inline(always)]
        fn new_empty() -> Self {
            Self {
                vmo: fidl::new_empty!(fidl::encoding::HandleType<fidl::Vmo, { fidl::ObjectType::VMO.into_raw() }, 2147483648>),
                offset: fidl::new_empty!(u64),
                size: fidl::new_empty!(u64),
            }
        }

        #[inline]
        unsafe fn decode(
            &mut self,
            decoder: &mut fidl::encoding::Decoder<'_>,
            offset: usize,
            _depth: fidl::encoding::Depth,
        ) -> fidl::Result<()> {
            decoder.debug_check_bounds::<Self>(offset);
            // Verify that padding bytes are zero.
            let ptr = unsafe { decoder.buf.as_ptr().add(offset).offset(0) };
            let padval = unsafe { (ptr as *const u64).read_unaligned() };
            let mask = 0xffffffff00000000u64;
            let maskedval = padval & mask;
            if maskedval != 0 {
                return Err(fidl::Error::NonZeroPadding {
                    padding_start: offset + 0 + ((mask as u64).trailing_zeros() / 8) as usize,
                });
            }
            fidl::decode!(fidl::encoding::HandleType<fidl::Vmo, { fidl::ObjectType::VMO.into_raw() }, 2147483648>, &mut self.vmo, decoder, offset + 0, _depth)?;
            fidl::decode!(u64, &mut self.offset, decoder, offset + 8, _depth)?;
            fidl::decode!(u64, &mut self.size, decoder, offset + 16, _depth)?;
            Ok(())
        }
    }

    unsafe impl fidl::encoding::TypeMarker for Data {
        type Owned = Self;

        #[inline(always)]
        fn inline_align(_context: fidl::encoding::Context) -> usize {
            8
        }

        #[inline(always)]
        fn inline_size(_context: fidl::encoding::Context) -> usize {
            16
        }
    }
    impl fidl::encoding::ResourceTypeMarker for Data {
        type Borrowed<'a> = &'a mut Self;
        fn take_or_borrow<'a>(
            value: &'a mut <Self as fidl::encoding::TypeMarker>::Owned,
        ) -> Self::Borrowed<'a> {
            value
        }
    }

    unsafe impl fidl::encoding::Encode<Data> for &mut Data {
        #[inline]
        unsafe fn encode(
            self,
            encoder: &mut fidl::encoding::Encoder<'_>,
            offset: usize,
            _depth: fidl::encoding::Depth,
        ) -> fidl::Result<()> {
            encoder.debug_check_bounds::<Data>(offset);
            encoder.write_num::<u64>(self.ordinal(), offset);
            match self {
            Data::Bytes(ref val) => {
                fidl::encoding::encode_in_envelope::<fidl::encoding::UnboundedVector<u8>>(
                    <fidl::encoding::UnboundedVector<u8> as fidl::encoding::ValueTypeMarker>::borrow(val),
                    encoder, offset + 8, _depth
                )
            }
            Data::Buffer(ref mut val) => {
                fidl::encoding::encode_in_envelope::<Buffer>(
                    <Buffer as fidl::encoding::ResourceTypeMarker>::take_or_borrow(val),
                    encoder, offset + 8, _depth
                )
            }
            Data::__SourceBreaking { .. } => Err(fidl::Error::UnknownUnionTag),
        }
        }
    }

    impl fidl::encoding::Decode<Self> for Data {
        #[inline(always)]
        fn new_empty() -> Self {
            Self::__SourceBreaking { unknown_ordinal: 0 }
        }

        #[inline]
        unsafe fn decode(
            &mut self,
            decoder: &mut fidl::encoding::Decoder<'_>,
            offset: usize,
            mut depth: fidl::encoding::Depth,
        ) -> fidl::Result<()> {
            decoder.debug_check_bounds::<Self>(offset);
            #[allow(unused_variables)]
            let next_out_of_line = decoder.next_out_of_line();
            let handles_before = decoder.remaining_handles();
            let (ordinal, inlined, num_bytes, num_handles) =
                fidl::encoding::decode_union_inline_portion(decoder, offset)?;

            let member_inline_size = match ordinal {
                1 => {
                    <fidl::encoding::UnboundedVector<u8> as fidl::encoding::TypeMarker>::inline_size(
                        decoder.context,
                    )
                }
                2 => <Buffer as fidl::encoding::TypeMarker>::inline_size(decoder.context),
                0 => return Err(fidl::Error::UnknownUnionTag),
                _ => num_bytes as usize,
            };

            if inlined != (member_inline_size <= 4) {
                return Err(fidl::Error::InvalidInlineBitInEnvelope);
            }
            let _inner_offset;
            if inlined {
                decoder.check_inline_envelope_padding(offset + 8, member_inline_size)?;
                _inner_offset = offset + 8;
            } else {
                depth.increment()?;
                _inner_offset = decoder.out_of_line_offset(member_inline_size)?;
            }
            match ordinal {
                1 => {
                    #[allow(irrefutable_let_patterns)]
                    if let Data::Bytes(_) = self {
                        // Do nothing, read the value into the object
                    } else {
                        // Initialize `self` to the right variant
                        *self = Data::Bytes(fidl::new_empty!(fidl::encoding::UnboundedVector<u8>));
                    }
                    #[allow(irrefutable_let_patterns)]
                    if let Data::Bytes(ref mut val) = self {
                        fidl::decode!(
                            fidl::encoding::UnboundedVector<u8>,
                            val,
                            decoder,
                            _inner_offset,
                            depth
                        )?;
                    } else {
                        unreachable!()
                    }
                }
                2 => {
                    #[allow(irrefutable_let_patterns)]
                    if let Data::Buffer(_) = self {
                        // Do nothing, read the value into the object
                    } else {
                        // Initialize `self` to the right variant
                        *self = Data::Buffer(fidl::new_empty!(Buffer));
                    }
                    #[allow(irrefutable_let_patterns)]
                    if let Data::Buffer(ref mut val) = self {
                        fidl::decode!(Buffer, val, decoder, _inner_offset, depth)?;
                    } else {
                        unreachable!()
                    }
                }
                #[allow(deprecated)]
                ordinal => {
                    for _ in 0..num_handles {
                        decoder.drop_next_handle()?;
                    }
                    *self = Data::__SourceBreaking { unknown_ordinal: ordinal };
                }
            }
            if !inlined && decoder.next_out_of_line() != next_out_of_line + (num_bytes as usize) {
                return Err(fidl::Error::InvalidNumBytesInEnvelope);
            }
            if handles_before != decoder.remaining_handles() + (num_handles as usize) {
                return Err(fidl::Error::InvalidNumHandlesInEnvelope);
            }
            Ok(())
        }
    }
}