endian-type-0.1.2/.gitignore000064400017500001750000000000221255526631500141430ustar0000000000000000target Cargo.lock endian-type-0.1.2/Cargo.toml000064400017500001750000000015321255556123000141040ustar0000000000000000# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g. crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "endian-type" version = "0.1.2" authors = ["Lolirofle "] description = "Type safe wrappers for types with a defined byte order" homepage = "https://github.com/Lolirofle/endian-type" keywords = ["endian", "byteorder"] license = "MIT" repository = "https://github.com/Lolirofle/endian-type.git" endian-type-0.1.2/Cargo.toml.orig000064400017500001750000000005721255556123000150460ustar0000000000000000[package] name = "endian-type" version = "0.1.2" authors = ["Lolirofle "] description = "Type safe wrappers for types with a defined byte order" #documentation = "..." homepage = "https://github.com/Lolirofle/endian-type" repository = "https://github.com/Lolirofle/endian-type.git" #readme = "README.md" keywords = ["endian","byteorder"] license = "MIT" endian-type-0.1.2/LICENSE000064400017500001750000000020541255542007100131560ustar0000000000000000The MIT License (MIT) Copyright (c) 2015 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. endian-type-0.1.2/src/lib.rs000064400017500001750000000106431255556116000140640ustar0000000000000000use std::{mem,slice}; use std::convert::{From,Into}; use std::ops::{BitAnd,BitOr,BitXor}; ///Type with a specified byte order pub trait Endian{} macro_rules! impl_Endian{ ( for $e:ident) => { impl BitAnd for $e where T: BitAnd { type Output = $e<::Output>; #[inline] fn bitand(self,other: Self) -> Self::Output{ $e(self.0 & other.0) } } impl BitOr for $e where T: BitOr { type Output = $e<::Output>; #[inline] fn bitor(self,other: Self) -> Self::Output{ $e(self.0 | other.0) } } impl BitXor for $e where T: BitXor { type Output = $e<::Output>; #[inline] fn bitxor(self,other: Self) -> Self::Output{ $e(self.0 ^ other.0) } } impl $e where T: Sized + Copy { #[inline] pub fn from_bytes(bytes: &[u8]) -> Self{ debug_assert!(bytes.len() >= mem::size_of::()); $e(unsafe{*(bytes.as_ptr() as *const T)}) } #[inline] pub fn as_bytes(&self) -> &[u8]{ unsafe{slice::from_raw_parts( &self.0 as *const T as *const u8, mem::size_of::() )} } /*pub fn write_bytes(self,buffer: &mut [u8]){ debug_assert!(buffer.len() >= mem::size_of::()); let bytes = mem::transmute::<_,[u8; mem::size_of::()]>(); unsafe{ptr::copy_nonoverlapping(bytes.as_ptr(),buffer.as_mut_ptr(),mem::size_of::())}; }*/ } } } ///Big endian byte order /// ///Most significant byte first #[derive(Copy,Clone,Debug,Eq,PartialEq,Hash,Ord,PartialOrd)] pub struct BigEndian(T); impl Endian for BigEndian{} macro_rules! impl_for_BigEndian{ ( $t:ident ) => { impl Into<$t> for BigEndian<$t>{ #[inline] fn into(self) -> $t{ $t::from_be(self.0) } } impl From<$t> for BigEndian<$t>{ #[inline] fn from(data: $t) -> Self{ BigEndian(data.to_be()) } } impl From> for BigEndian<$t>{ #[inline] fn from(data: LittleEndian<$t>) -> Self{ BigEndian(data.0.swap_bytes()) } } } } impl_Endian!(for BigEndian); impl_for_BigEndian!(u16); impl_for_BigEndian!(u32); impl_for_BigEndian!(u64); impl_for_BigEndian!(usize); impl_for_BigEndian!(i16); impl_for_BigEndian!(i32); impl_for_BigEndian!(i64); impl_for_BigEndian!(isize); ///Little endian byte order /// ///Least significant byte first #[derive(Copy,Clone,Debug,Eq,PartialEq,Hash,Ord,PartialOrd)] pub struct LittleEndian(T); impl Endian for LittleEndian{} macro_rules! impl_for_LittleEndian{ ( $t:ident ) => { impl Into<$t> for LittleEndian<$t>{ #[inline] fn into(self) -> $t{ $t::from_le(self.0) } } impl From<$t> for LittleEndian<$t>{ #[inline] fn from(data: $t) -> Self{ LittleEndian(data.to_le()) } } impl From> for LittleEndian<$t>{ #[inline] fn from(data: BigEndian<$t>) -> Self{ LittleEndian(data.0.swap_bytes()) } } } } impl_Endian!(for LittleEndian); impl_for_LittleEndian!(u16); impl_for_LittleEndian!(u32); impl_for_LittleEndian!(u64); impl_for_LittleEndian!(usize); impl_for_LittleEndian!(i16); impl_for_LittleEndian!(i32); impl_for_LittleEndian!(i64); impl_for_LittleEndian!(isize); ///Network byte order as defined by IETF RFC1700 [http://tools.ietf.org/html/rfc1700] pub type NetworkOrder = BigEndian; ///Type aliases for primitive types pub mod types{ #![allow(non_camel_case_types)] use super::*; pub type i16_be = BigEndian; pub type i32_be = BigEndian; pub type i64_be = BigEndian; pub type isize_be = BigEndian; pub type u16_be = BigEndian; pub type u32_be = BigEndian; pub type u64_be = BigEndian; pub type usize_be = BigEndian; pub type i16_le = LittleEndian; pub type i32_le = LittleEndian; pub type i64_le = LittleEndian; pub type isize_le = LittleEndian; pub type u16_le = LittleEndian; pub type u32_le = LittleEndian; pub type u64_le = LittleEndian; pub type usize_le = LittleEndian; pub type i16_net = NetworkOrder; pub type i32_net = NetworkOrder; pub type i64_net = NetworkOrder; pub type isize_net = NetworkOrder; pub type u16_net = NetworkOrder; pub type u32_net = NetworkOrder; pub type u64_net = NetworkOrder; pub type usize_net = NetworkOrder; } /*#[cfg(test)] mod tests{ use super::*; use super::types::*; #[test] fn construct_big(){ //#[cfg(target_endian = "big")]{} } }*/