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
// Copyright (c) 2013-2015 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. /// A value casted directly from a little-endian byte buffer. On big-endian /// processors, the bytes of the value need to be swapped upon reading and writing. #[repr(C)] pub struct WireValue<T> { value: T, } impl<T> WireValue<T> where T: Endian { /// Reads the value, swapping bytes on big-endian processors. #[inline] pub fn get(&self) -> T { self.value.get() } /// Writes the value, swapping bytes on big-endian processors. #[inline] pub fn set(&mut self, value: T) { self.value.set(value) } } /// Something that can appear in a `WireValue`. pub trait Endian : Sized { /// Reads the value, swapping bytes on big-endian processors. fn get(&self) -> Self; /// Writes the value, swapping bytes on big-endian processors. fn set(&mut self, value: Self); } macro_rules! endian_impl( ($typ:ty) => ( impl Endian for $typ { #[inline] fn get(&self) -> $typ { *self } #[inline] fn set(&mut self, value: $typ) {*self = value;} } ); ($typ:ty, $swapper:ident) => ( impl Endian for $typ { #[inline] fn get(&self) -> $typ { self.$swapper() } #[inline] fn set(&mut self, value: $typ) { *self = value.$swapper(); } } ); ); // No swapping necessary for primitives of size less than one byte. endian_impl!(()); endian_impl!(bool); endian_impl!(u8); endian_impl!(i8); // Need to swap bytes for primitives larger than a byte. endian_impl!(u16, to_le); endian_impl!(i16, to_le); endian_impl!(u32, to_le); endian_impl!(i32, to_le); endian_impl!(u64, to_le); endian_impl!(i64, to_le); impl Endian for f32 { fn get(&self) -> f32 { f32::from_bits(self.to_bits().to_le()) } fn set(&mut self, value : f32) { *self = f32::from_bits(value.to_bits().to_le()); } } impl Endian for f64 { fn get(&self) -> f64 { f64::from_bits(self.to_bits().to_le()) } fn set(&mut self, value : f64) { *self = f64::from_bits(value.to_bits().to_le()); } }