fidl_examples_keyvaluestore_addreaditem_common/
fidl_examples_keyvaluestore_addreaditem_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
11pub type Key = String;
12
13pub type Value = Vec<u8>;
14
15/// An enumeration of things that may go wrong when trying to read a value out of our store.
16#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
17pub enum ReadError {
18    Unknown,
19    NotFound,
20    #[doc(hidden)]
21    __SourceBreaking {
22        unknown_ordinal: u32,
23    },
24}
25
26/// Pattern that matches an unknown `ReadError` member.
27#[macro_export]
28macro_rules! ReadErrorUnknown {
29    () => {
30        _
31    };
32}
33
34impl ReadError {
35    #[inline]
36    pub fn from_primitive(prim: u32) -> Option<Self> {
37        match prim {
38            0 => Some(Self::Unknown),
39            1 => Some(Self::NotFound),
40            _ => None,
41        }
42    }
43
44    #[inline]
45    pub fn from_primitive_allow_unknown(prim: u32) -> Self {
46        match prim {
47            0 => Self::Unknown,
48            1 => Self::NotFound,
49            unknown_ordinal => Self::__SourceBreaking { unknown_ordinal },
50        }
51    }
52
53    #[inline]
54    pub fn unknown() -> Self {
55        Self::__SourceBreaking { unknown_ordinal: 0xffffffff }
56    }
57
58    #[inline]
59    pub const fn into_primitive(self) -> u32 {
60        match self {
61            Self::Unknown => 0,
62            Self::NotFound => 1,
63            Self::__SourceBreaking { unknown_ordinal } => unknown_ordinal,
64        }
65    }
66
67    #[inline]
68    pub fn is_unknown(&self) -> bool {
69        match self {
70            Self::__SourceBreaking { unknown_ordinal: _ } => true,
71            _ => false,
72        }
73    }
74}
75
76/// An enumeration of things that may go wrong when trying to write a value to our store.
77#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
78pub enum WriteError {
79    Unknown,
80    InvalidKey,
81    InvalidValue,
82    AlreadyExists,
83    #[doc(hidden)]
84    __SourceBreaking {
85        unknown_ordinal: u32,
86    },
87}
88
89/// Pattern that matches an unknown `WriteError` member.
90#[macro_export]
91macro_rules! WriteErrorUnknown {
92    () => {
93        _
94    };
95}
96
97impl WriteError {
98    #[inline]
99    pub fn from_primitive(prim: u32) -> Option<Self> {
100        match prim {
101            0 => Some(Self::Unknown),
102            1 => Some(Self::InvalidKey),
103            2 => Some(Self::InvalidValue),
104            3 => Some(Self::AlreadyExists),
105            _ => None,
106        }
107    }
108
109    #[inline]
110    pub fn from_primitive_allow_unknown(prim: u32) -> Self {
111        match prim {
112            0 => Self::Unknown,
113            1 => Self::InvalidKey,
114            2 => Self::InvalidValue,
115            3 => Self::AlreadyExists,
116            unknown_ordinal => Self::__SourceBreaking { unknown_ordinal },
117        }
118    }
119
120    #[inline]
121    pub fn unknown() -> Self {
122        Self::__SourceBreaking { unknown_ordinal: 0xffffffff }
123    }
124
125    #[inline]
126    pub const fn into_primitive(self) -> u32 {
127        match self {
128            Self::Unknown => 0,
129            Self::InvalidKey => 1,
130            Self::InvalidValue => 2,
131            Self::AlreadyExists => 3,
132            Self::__SourceBreaking { unknown_ordinal } => unknown_ordinal,
133        }
134    }
135
136    #[inline]
137    pub fn is_unknown(&self) -> bool {
138        match self {
139            Self::__SourceBreaking { unknown_ordinal: _ } => true,
140            _ => false,
141        }
142    }
143}
144
145/// An item in the store. The key must match the regex `^[A-z][A-z0-9_\.\/]{2,62}[A-z0-9]$`. That
146/// is, it must start with a letter, end with a letter or number, contain only letters, numbers,
147/// periods, and slashes, and be between 4 and 64 characters long.
148#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
149pub struct Item {
150    pub key: String,
151    pub value: Vec<u8>,
152}
153
154impl fidl::Persistable for Item {}
155
156#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
157pub struct StoreReadItemRequest {
158    pub key: String,
159}
160
161impl fidl::Persistable for StoreReadItemRequest {}
162
163#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
164pub struct StoreWriteItemRequest {
165    pub attempt: Item,
166}
167
168impl fidl::Persistable for StoreWriteItemRequest {}
169
170mod internal {
171    use super::*;
172    unsafe impl fidl::encoding::TypeMarker for ReadError {
173        type Owned = Self;
174
175        #[inline(always)]
176        fn inline_align(_context: fidl::encoding::Context) -> usize {
177            std::mem::align_of::<u32>()
178        }
179
180        #[inline(always)]
181        fn inline_size(_context: fidl::encoding::Context) -> usize {
182            std::mem::size_of::<u32>()
183        }
184
185        #[inline(always)]
186        fn encode_is_copy() -> bool {
187            false
188        }
189
190        #[inline(always)]
191        fn decode_is_copy() -> bool {
192            false
193        }
194    }
195
196    impl fidl::encoding::ValueTypeMarker for ReadError {
197        type Borrowed<'a> = Self;
198        #[inline(always)]
199        fn borrow(value: &<Self as fidl::encoding::TypeMarker>::Owned) -> Self::Borrowed<'_> {
200            *value
201        }
202    }
203
204    unsafe impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Encode<Self, D> for ReadError {
205        #[inline]
206        unsafe fn encode(
207            self,
208            encoder: &mut fidl::encoding::Encoder<'_, D>,
209            offset: usize,
210            _depth: fidl::encoding::Depth,
211        ) -> fidl::Result<()> {
212            encoder.debug_check_bounds::<Self>(offset);
213            encoder.write_num(self.into_primitive(), offset);
214            Ok(())
215        }
216    }
217
218    impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Decode<Self, D> for ReadError {
219        #[inline(always)]
220        fn new_empty() -> Self {
221            Self::unknown()
222        }
223
224        #[inline]
225        unsafe fn decode(
226            &mut self,
227            decoder: &mut fidl::encoding::Decoder<'_, D>,
228            offset: usize,
229            _depth: fidl::encoding::Depth,
230        ) -> fidl::Result<()> {
231            decoder.debug_check_bounds::<Self>(offset);
232            let prim = decoder.read_num::<u32>(offset);
233
234            *self = Self::from_primitive_allow_unknown(prim);
235            Ok(())
236        }
237    }
238    unsafe impl fidl::encoding::TypeMarker for WriteError {
239        type Owned = Self;
240
241        #[inline(always)]
242        fn inline_align(_context: fidl::encoding::Context) -> usize {
243            std::mem::align_of::<u32>()
244        }
245
246        #[inline(always)]
247        fn inline_size(_context: fidl::encoding::Context) -> usize {
248            std::mem::size_of::<u32>()
249        }
250
251        #[inline(always)]
252        fn encode_is_copy() -> bool {
253            false
254        }
255
256        #[inline(always)]
257        fn decode_is_copy() -> bool {
258            false
259        }
260    }
261
262    impl fidl::encoding::ValueTypeMarker for WriteError {
263        type Borrowed<'a> = Self;
264        #[inline(always)]
265        fn borrow(value: &<Self as fidl::encoding::TypeMarker>::Owned) -> Self::Borrowed<'_> {
266            *value
267        }
268    }
269
270    unsafe impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Encode<Self, D> for WriteError {
271        #[inline]
272        unsafe fn encode(
273            self,
274            encoder: &mut fidl::encoding::Encoder<'_, D>,
275            offset: usize,
276            _depth: fidl::encoding::Depth,
277        ) -> fidl::Result<()> {
278            encoder.debug_check_bounds::<Self>(offset);
279            encoder.write_num(self.into_primitive(), offset);
280            Ok(())
281        }
282    }
283
284    impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Decode<Self, D> for WriteError {
285        #[inline(always)]
286        fn new_empty() -> Self {
287            Self::unknown()
288        }
289
290        #[inline]
291        unsafe fn decode(
292            &mut self,
293            decoder: &mut fidl::encoding::Decoder<'_, D>,
294            offset: usize,
295            _depth: fidl::encoding::Depth,
296        ) -> fidl::Result<()> {
297            decoder.debug_check_bounds::<Self>(offset);
298            let prim = decoder.read_num::<u32>(offset);
299
300            *self = Self::from_primitive_allow_unknown(prim);
301            Ok(())
302        }
303    }
304
305    impl fidl::encoding::ValueTypeMarker for Item {
306        type Borrowed<'a> = &'a Self;
307        fn borrow(value: &<Self as fidl::encoding::TypeMarker>::Owned) -> Self::Borrowed<'_> {
308            value
309        }
310    }
311
312    unsafe impl fidl::encoding::TypeMarker for Item {
313        type Owned = Self;
314
315        #[inline(always)]
316        fn inline_align(_context: fidl::encoding::Context) -> usize {
317            8
318        }
319
320        #[inline(always)]
321        fn inline_size(_context: fidl::encoding::Context) -> usize {
322            32
323        }
324    }
325
326    unsafe impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Encode<Item, D> for &Item {
327        #[inline]
328        unsafe fn encode(
329            self,
330            encoder: &mut fidl::encoding::Encoder<'_, D>,
331            offset: usize,
332            _depth: fidl::encoding::Depth,
333        ) -> fidl::Result<()> {
334            encoder.debug_check_bounds::<Item>(offset);
335            // Delegate to tuple encoding.
336            fidl::encoding::Encode::<Item, D>::encode(
337                (
338                    <fidl::encoding::BoundedString<128> as fidl::encoding::ValueTypeMarker>::borrow(
339                        &self.key,
340                    ),
341                    <fidl::encoding::Vector<u8, 64000> as fidl::encoding::ValueTypeMarker>::borrow(
342                        &self.value,
343                    ),
344                ),
345                encoder,
346                offset,
347                _depth,
348            )
349        }
350    }
351    unsafe impl<
352            D: fidl::encoding::ResourceDialect,
353            T0: fidl::encoding::Encode<fidl::encoding::BoundedString<128>, D>,
354            T1: fidl::encoding::Encode<fidl::encoding::Vector<u8, 64000>, D>,
355        > fidl::encoding::Encode<Item, D> for (T0, T1)
356    {
357        #[inline]
358        unsafe fn encode(
359            self,
360            encoder: &mut fidl::encoding::Encoder<'_, D>,
361            offset: usize,
362            depth: fidl::encoding::Depth,
363        ) -> fidl::Result<()> {
364            encoder.debug_check_bounds::<Item>(offset);
365            // Zero out padding regions. There's no need to apply masks
366            // because the unmasked parts will be overwritten by fields.
367            // Write the fields.
368            self.0.encode(encoder, offset + 0, depth)?;
369            self.1.encode(encoder, offset + 16, depth)?;
370            Ok(())
371        }
372    }
373
374    impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Decode<Self, D> for Item {
375        #[inline(always)]
376        fn new_empty() -> Self {
377            Self {
378                key: fidl::new_empty!(fidl::encoding::BoundedString<128>, D),
379                value: fidl::new_empty!(fidl::encoding::Vector<u8, 64000>, D),
380            }
381        }
382
383        #[inline]
384        unsafe fn decode(
385            &mut self,
386            decoder: &mut fidl::encoding::Decoder<'_, D>,
387            offset: usize,
388            _depth: fidl::encoding::Depth,
389        ) -> fidl::Result<()> {
390            decoder.debug_check_bounds::<Self>(offset);
391            // Verify that padding bytes are zero.
392            fidl::decode!(
393                fidl::encoding::BoundedString<128>,
394                D,
395                &mut self.key,
396                decoder,
397                offset + 0,
398                _depth
399            )?;
400            fidl::decode!(fidl::encoding::Vector<u8, 64000>, D, &mut self.value, decoder, offset + 16, _depth)?;
401            Ok(())
402        }
403    }
404
405    impl fidl::encoding::ValueTypeMarker for StoreReadItemRequest {
406        type Borrowed<'a> = &'a Self;
407        fn borrow(value: &<Self as fidl::encoding::TypeMarker>::Owned) -> Self::Borrowed<'_> {
408            value
409        }
410    }
411
412    unsafe impl fidl::encoding::TypeMarker for StoreReadItemRequest {
413        type Owned = Self;
414
415        #[inline(always)]
416        fn inline_align(_context: fidl::encoding::Context) -> usize {
417            8
418        }
419
420        #[inline(always)]
421        fn inline_size(_context: fidl::encoding::Context) -> usize {
422            16
423        }
424    }
425
426    unsafe impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Encode<StoreReadItemRequest, D>
427        for &StoreReadItemRequest
428    {
429        #[inline]
430        unsafe fn encode(
431            self,
432            encoder: &mut fidl::encoding::Encoder<'_, D>,
433            offset: usize,
434            _depth: fidl::encoding::Depth,
435        ) -> fidl::Result<()> {
436            encoder.debug_check_bounds::<StoreReadItemRequest>(offset);
437            // Delegate to tuple encoding.
438            fidl::encoding::Encode::<StoreReadItemRequest, D>::encode(
439                (<fidl::encoding::BoundedString<128> as fidl::encoding::ValueTypeMarker>::borrow(
440                    &self.key,
441                ),),
442                encoder,
443                offset,
444                _depth,
445            )
446        }
447    }
448    unsafe impl<
449            D: fidl::encoding::ResourceDialect,
450            T0: fidl::encoding::Encode<fidl::encoding::BoundedString<128>, D>,
451        > fidl::encoding::Encode<StoreReadItemRequest, D> for (T0,)
452    {
453        #[inline]
454        unsafe fn encode(
455            self,
456            encoder: &mut fidl::encoding::Encoder<'_, D>,
457            offset: usize,
458            depth: fidl::encoding::Depth,
459        ) -> fidl::Result<()> {
460            encoder.debug_check_bounds::<StoreReadItemRequest>(offset);
461            // Zero out padding regions. There's no need to apply masks
462            // because the unmasked parts will be overwritten by fields.
463            // Write the fields.
464            self.0.encode(encoder, offset + 0, depth)?;
465            Ok(())
466        }
467    }
468
469    impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Decode<Self, D> for StoreReadItemRequest {
470        #[inline(always)]
471        fn new_empty() -> Self {
472            Self { key: fidl::new_empty!(fidl::encoding::BoundedString<128>, D) }
473        }
474
475        #[inline]
476        unsafe fn decode(
477            &mut self,
478            decoder: &mut fidl::encoding::Decoder<'_, D>,
479            offset: usize,
480            _depth: fidl::encoding::Depth,
481        ) -> fidl::Result<()> {
482            decoder.debug_check_bounds::<Self>(offset);
483            // Verify that padding bytes are zero.
484            fidl::decode!(
485                fidl::encoding::BoundedString<128>,
486                D,
487                &mut self.key,
488                decoder,
489                offset + 0,
490                _depth
491            )?;
492            Ok(())
493        }
494    }
495
496    impl fidl::encoding::ValueTypeMarker for StoreWriteItemRequest {
497        type Borrowed<'a> = &'a Self;
498        fn borrow(value: &<Self as fidl::encoding::TypeMarker>::Owned) -> Self::Borrowed<'_> {
499            value
500        }
501    }
502
503    unsafe impl fidl::encoding::TypeMarker for StoreWriteItemRequest {
504        type Owned = Self;
505
506        #[inline(always)]
507        fn inline_align(_context: fidl::encoding::Context) -> usize {
508            8
509        }
510
511        #[inline(always)]
512        fn inline_size(_context: fidl::encoding::Context) -> usize {
513            32
514        }
515    }
516
517    unsafe impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Encode<StoreWriteItemRequest, D>
518        for &StoreWriteItemRequest
519    {
520        #[inline]
521        unsafe fn encode(
522            self,
523            encoder: &mut fidl::encoding::Encoder<'_, D>,
524            offset: usize,
525            _depth: fidl::encoding::Depth,
526        ) -> fidl::Result<()> {
527            encoder.debug_check_bounds::<StoreWriteItemRequest>(offset);
528            // Delegate to tuple encoding.
529            fidl::encoding::Encode::<StoreWriteItemRequest, D>::encode(
530                (<Item as fidl::encoding::ValueTypeMarker>::borrow(&self.attempt),),
531                encoder,
532                offset,
533                _depth,
534            )
535        }
536    }
537    unsafe impl<D: fidl::encoding::ResourceDialect, T0: fidl::encoding::Encode<Item, D>>
538        fidl::encoding::Encode<StoreWriteItemRequest, D> for (T0,)
539    {
540        #[inline]
541        unsafe fn encode(
542            self,
543            encoder: &mut fidl::encoding::Encoder<'_, D>,
544            offset: usize,
545            depth: fidl::encoding::Depth,
546        ) -> fidl::Result<()> {
547            encoder.debug_check_bounds::<StoreWriteItemRequest>(offset);
548            // Zero out padding regions. There's no need to apply masks
549            // because the unmasked parts will be overwritten by fields.
550            // Write the fields.
551            self.0.encode(encoder, offset + 0, depth)?;
552            Ok(())
553        }
554    }
555
556    impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Decode<Self, D> for StoreWriteItemRequest {
557        #[inline(always)]
558        fn new_empty() -> Self {
559            Self { attempt: fidl::new_empty!(Item, D) }
560        }
561
562        #[inline]
563        unsafe fn decode(
564            &mut self,
565            decoder: &mut fidl::encoding::Decoder<'_, D>,
566            offset: usize,
567            _depth: fidl::encoding::Depth,
568        ) -> fidl::Result<()> {
569            decoder.debug_check_bounds::<Self>(offset);
570            // Verify that padding bytes are zero.
571            fidl::decode!(Item, D, &mut self.attempt, decoder, offset + 0, _depth)?;
572            Ok(())
573        }
574    }
575}