| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /// This macro provides a common implementation of MojomEncodable | |
| 6 /// for MojomPointer types. | |
| 7 /// | |
| 8 /// Note: it does not implement compute_size(); | |
| 9 /// | |
| 10 /// The Rust type system currently lacks the facilities to do this | |
| 11 /// generically (need mutually excludable traits) but this macro | |
| 12 /// should be replaced as soon as this is possible. | |
| 13 #[macro_export] | |
| 14 macro_rules! impl_encodable_for_pointer { | |
| 15 () => { | |
| 16 fn mojom_alignment() -> usize { | |
| 17 8 // All mojom pointers are 8 bytes in length, and thus are 8-byte a
ligned | |
| 18 } | |
| 19 fn mojom_type() -> $crate::bindings::mojom::MojomType { | |
| 20 $crate::bindings::mojom::MojomType::Pointer | |
| 21 } | |
| 22 fn embed_size(_context: &$crate::bindings::encoding::Context) -> $crate:
:bindings::encoding::Bits { | |
| 23 $crate::bindings::mojom::POINTER_BIT_SIZE | |
| 24 } | |
| 25 fn encode(self, encoder: &mut $crate::bindings::encoding::Encoder, conte
xt: $crate::bindings::encoding::Context) { | |
| 26 let loc = encoder.size() as u64; | |
| 27 { | |
| 28 let state = encoder.get_mut(&context); | |
| 29 state.encode_pointer(loc); | |
| 30 } | |
| 31 self.encode_new(encoder, context); | |
| 32 } | |
| 33 fn decode(decoder: &mut $crate::bindings::decoding::Decoder, context: $c
rate::bindings::encoding::Context) -> Result<Self, ValidationError> { | |
| 34 let ptr = { | |
| 35 let state = decoder.get_mut(&context); | |
| 36 match state.decode_pointer() { | |
| 37 Some(ptr) => ptr, | |
| 38 None => return Err(ValidationError::IllegalPointer), | |
| 39 } | |
| 40 }; | |
| 41 if ptr == $crate::bindings::mojom::MOJOM_NULL_POINTER { | |
| 42 Err(ValidationError::UnexpectedNullPointer) | |
| 43 } else { | |
| 44 Self::decode_new(decoder, context, ptr) | |
| 45 } | |
| 46 } | |
| 47 }; | |
| 48 } | |
| 49 | |
| 50 /// This macro provides a common implementation of MojomEncodable | |
| 51 /// for MojomUnion types. | |
| 52 /// | |
| 53 /// Note: it does not implement compute_size(); | |
| 54 /// | |
| 55 /// The Rust type system currently lacks the facilities to do this | |
| 56 /// generically (need mutually excludable traits) but this macro | |
| 57 /// should be replaced as soon as this is possible. | |
| 58 #[macro_export] | |
| 59 macro_rules! impl_encodable_for_union { | |
| 60 () => { | |
| 61 fn mojom_alignment() -> usize { | |
| 62 8 | |
| 63 } | |
| 64 fn mojom_type() -> $crate::bindings::mojom::MojomType { | |
| 65 $crate::bindings::mojom::MojomType::Union | |
| 66 } | |
| 67 fn embed_size(context: &$crate::bindings::encoding::Context) -> $crate::
bindings::encoding::Bits { | |
| 68 if context.is_union() { | |
| 69 Self::nested_embed_size() | |
| 70 } else { | |
| 71 Self::inline_embed_size() | |
| 72 } | |
| 73 } | |
| 74 fn encode(self, encoder: &mut $crate::bindings::encoding::Encoder, conte
xt: $crate::bindings::encoding::Context) { | |
| 75 if context.is_union() { | |
| 76 self.nested_encode(encoder, context); | |
| 77 } else { | |
| 78 self.inline_encode(encoder, context.set_is_union(true)); | |
| 79 } | |
| 80 } | |
| 81 fn decode(decoder: &mut $crate::bindings::decoding::Decoder, context: $c
rate::bindings::encoding::Context) -> Result<Self, ValidationError> { | |
| 82 if context.is_union() { | |
| 83 Self::nested_decode(decoder, context) | |
| 84 } else { | |
| 85 Self::inline_decode(decoder, context.set_is_union(true)) | |
| 86 } | |
| 87 } | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 /// This macro provides a common implementation of MojomEncodable | |
| 92 /// for MojomInterface types. | |
| 93 /// | |
| 94 /// Note: it does not implement compute_size(); | |
| 95 /// | |
| 96 /// The Rust type system currently lacks the facilities to do this | |
| 97 /// generically (need mutually excludable traits) but this macro | |
| 98 /// should be replaced as soon as this is possible. | |
| 99 #[macro_export] | |
| 100 macro_rules! impl_encodable_for_interface { | |
| 101 () => { | |
| 102 fn mojom_alignment() -> usize { | |
| 103 4 | |
| 104 } | |
| 105 fn mojom_type() -> $crate::bindings::mojom::MojomType { | |
| 106 $crate::bindings::mojom::MojomType::Interface | |
| 107 } | |
| 108 fn embed_size(_context: &$crate::bindings::encoding::Context) -> $crate:
:bindings::encoding::Bits { | |
| 109 use std::mem; | |
| 110 $crate::bindings::encoding::Bits(2 * 8 * mem::size_of::<u32>()) | |
| 111 } | |
| 112 fn compute_size(&self, _context: $crate::bindings::encoding::Context) ->
usize { | |
| 113 0 // Indicates that this type is inlined and it adds nothing externa
l to the size | |
| 114 } | |
| 115 fn encode(self, encoder: &mut $crate::bindings::encoding::Encoder, conte
xt: $crate::bindings::encoding::Context) { | |
| 116 let version = self.version(); | |
| 117 let pos = encoder.add_handle(self.as_untyped()); | |
| 118 let mut state = encoder.get_mut(&context); | |
| 119 state.encode(pos as i32); | |
| 120 state.encode(version as u32); | |
| 121 } | |
| 122 fn decode(decoder: &mut $crate::bindings::decoding::Decoder, context: $c
rate::bindings::encoding::Context) -> Result<Self, ValidationError> { | |
| 123 let (handle_index, version) = { | |
| 124 let mut state = decoder.get_mut(&context); | |
| 125 (state.decode::<i32>(), state.decode::<u32>()) | |
| 126 }; | |
| 127 let handle = try!(decoder.claim_handle::<$crate::system::message_pip
e::MessageEndpoint>(handle_index)); | |
| 128 Ok(Self::with_version(handle, version)) | |
| 129 } | |
| 130 } | |
| 131 } | |
| OLD | NEW |