| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 use bindings::mojom::MOJOM_NULL_POINTER; | 5 use bindings::mojom::MOJOM_NULL_POINTER; |
| 6 use bindings::util; | 6 use bindings::util; |
| 7 | 7 |
| 8 use std::mem; | 8 use std::mem; |
| 9 use std::ptr; | 9 use std::ptr; |
| 10 use std::ops::{Add, AddAssign, Sub, Mul, Div, Rem}; | 10 use std::ops::{Add, AddAssign, Sub, Mul, Div, Rem}; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 } | 34 } |
| 35 | 35 |
| 36 /// Return 1 left-shifted by the amount of bits stored here. | 36 /// Return 1 left-shifted by the amount of bits stored here. |
| 37 /// | 37 /// |
| 38 /// Only guaranteed to work for up to 8 bits. | 38 /// Only guaranteed to work for up to 8 bits. |
| 39 pub fn as_set_bit(self) -> u8 { | 39 pub fn as_set_bit(self) -> u8 { |
| 40 debug_assert!(self.0 < 8); | 40 debug_assert!(self.0 < 8); |
| 41 1 << (self.0 & 7) | 41 1 << (self.0 & 7) |
| 42 } | 42 } |
| 43 | 43 |
| 44 pub fn checked_mul(self, val: usize) -> Option<Bits> { |
| 45 match val.checked_mul(self.0) { |
| 46 Some(result) => Some(Bits(result)), |
| 47 None => None, |
| 48 } |
| 49 } |
| 50 |
| 44 /// Align the bits to some number of bytes. | 51 /// Align the bits to some number of bytes. |
| 45 pub fn align_to_bytes(&mut self, bytes: usize) { | 52 pub fn align_to_bytes(&mut self, bytes: usize) { |
| 46 self.0 = util::align_bytes(self.0, 8 * bytes); | 53 self.0 = util::align_bytes(self.0, 8 * bytes); |
| 47 } | 54 } |
| 48 } | 55 } |
| 49 | 56 |
| 50 impl Add for Bits { | 57 impl Add for Bits { |
| 51 type Output = Self; | 58 type Output = Self; |
| 52 fn add(self, rhs: Self) -> Self { | 59 fn add(self, rhs: Self) -> Self { |
| 53 Bits(self.0 + rhs.0) | 60 Bits(self.0 + rhs.0) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 65 fn mul(self, rhs: usize) -> Self { | 72 fn mul(self, rhs: usize) -> Self { |
| 66 Bits(self.0 * rhs) | 73 Bits(self.0 * rhs) |
| 67 } | 74 } |
| 68 } | 75 } |
| 69 | 76 |
| 70 /// This trait is intended to be used by Mojom primitive values | 77 /// This trait is intended to be used by Mojom primitive values |
| 71 /// in order to be identified in generic contexts. | 78 /// in order to be identified in generic contexts. |
| 72 pub trait MojomNumeric: Copy + Clone + Sized + Add<Self> + Sub<Self, Output=Self
> + Mul<Self> + | 79 pub trait MojomNumeric: Copy + Clone + Sized + Add<Self> + Sub<Self, Output=Self
> + Mul<Self> + |
| 73 Div<Self, Output=Self> + Rem<Self, Output=Self> + PartialEq<Self> + Default
{ | 80 Div<Self, Output=Self> + Rem<Self, Output=Self> + PartialEq<Self> + Default
{ |
| 74 | 81 |
| 75 /// Converts the primitive to a little-endian representation (the mojom endi
anness). | 82 /// Converts the primitive to a little-endian representation (the mojom endianne
ss). |
| 76 fn to_mojom_endian(self) -> Self; | 83 fn to_mojom_endian(self) -> Self; |
| 77 } | 84 } |
| 78 | 85 |
| 79 macro_rules! impl_mojom_numeric_for_prim { | 86 macro_rules! impl_mojom_numeric_for_prim { |
| 80 ($($t:ty),*) => { | 87 ($($t:ty),*) => { |
| 81 $( | 88 $( |
| 82 impl MojomNumeric for $t { | 89 impl MojomNumeric for $t { |
| 83 fn to_mojom_endian(self) -> $t { self.to_le() } | 90 fn to_mojom_endian(self) -> $t { self.to_le() } |
| 84 } | 91 } |
| 85 )* | 92 )* |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 | 221 |
| 215 /// The current bit offset within 'data'. | 222 /// The current bit offset within 'data'. |
| 216 bit_offset: Bits, | 223 bit_offset: Bits, |
| 217 } | 224 } |
| 218 | 225 |
| 219 impl<'slice> EncodingState<'slice> { | 226 impl<'slice> EncodingState<'slice> { |
| 220 /// Create a new encoding state. | 227 /// Create a new encoding state. |
| 221 /// | 228 /// |
| 222 /// Note: the encoder will not allocate a buffer for you, rather | 229 /// Note: the encoder will not allocate a buffer for you, rather |
| 223 /// a pre-allocated buffer must be passed in. | 230 /// a pre-allocated buffer must be passed in. |
| 224 pub fn new(buffer: &'slice mut [u8], header: &DataHeader, offset: usize) ->
EncodingState<'slice> { | 231 pub fn new(buffer: &'slice mut [u8], |
| 232 header: &DataHeader, |
| 233 offset: usize) |
| 234 -> EncodingState<'slice> { |
| 225 let mut state = EncodingState { | 235 let mut state = EncodingState { |
| 226 data: buffer, | 236 data: buffer, |
| 227 global_offset: offset, | 237 global_offset: offset, |
| 228 offset: 0, | 238 offset: 0, |
| 229 bit_offset: Bits(0), | 239 bit_offset: Bits(0), |
| 230 }; | 240 }; |
| 231 state.write(header.size()); | 241 state.write(header.size()); |
| 232 state.write(header.data()); | 242 state.write(header.data()); |
| 233 state | 243 state |
| 234 } | 244 } |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 } | 393 } |
| 384 | 394 |
| 385 /// Signal to finish encoding by destroying the Encoder and returning the fi
nal | 395 /// Signal to finish encoding by destroying the Encoder and returning the fi
nal |
| 386 /// handle vector. | 396 /// handle vector. |
| 387 /// | 397 /// |
| 388 /// Note: No byte buffer is returned as that is pre-allocated. | 398 /// Note: No byte buffer is returned as that is pre-allocated. |
| 389 pub fn unwrap(self) -> Vec<UntypedHandle> { | 399 pub fn unwrap(self) -> Vec<UntypedHandle> { |
| 390 self.handles | 400 self.handles |
| 391 } | 401 } |
| 392 } | 402 } |
| OLD | NEW |