Use ByteOrder methods instead of free-standing functions
This commit is contained in:
parent
87c529c43a
commit
b84d17d4d7
@ -98,46 +98,31 @@ pub trait Writer {
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
fn id<T>(t: T) -> T { t }
|
||||
|
||||
macro_rules! impl_hash(
|
||||
( $($ty:ident, $uty:ident, $f:path;)* ) => (
|
||||
$(
|
||||
impl<S: Writer> Hash<S> for $ty {
|
||||
#[inline]
|
||||
fn hash(&self, state: &mut S) {
|
||||
let a: [u8, ..::core::$ty::BYTES] = unsafe {
|
||||
mem::transmute($f(*self as $uty) as $ty)
|
||||
};
|
||||
state.write(a.as_slice())
|
||||
}
|
||||
macro_rules! impl_hash {
|
||||
($ty:ident, $uty:ident) => {
|
||||
impl<S: Writer> Hash<S> for $ty {
|
||||
#[inline]
|
||||
fn hash(&self, state: &mut S) {
|
||||
use core::mem::ByteOrder;
|
||||
let a: [u8, ..::core::$ty::BYTES] = unsafe {
|
||||
mem::transmute((*self as $uty).to_little_endian() as $ty)
|
||||
};
|
||||
state.write(a.as_slice())
|
||||
}
|
||||
)*
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_hash!(
|
||||
u8, u8, id;
|
||||
u16, u16, mem::to_le16;
|
||||
u32, u32, mem::to_le32;
|
||||
u64, u64, mem::to_le64;
|
||||
i8, u8, id;
|
||||
i16, u16, mem::to_le16;
|
||||
i32, u32, mem::to_le32;
|
||||
i64, u64, mem::to_le64;
|
||||
)
|
||||
|
||||
#[cfg(target_word_size = "32")]
|
||||
impl_hash!(
|
||||
uint, u32, mem::to_le32;
|
||||
int, u32, mem::to_le32;
|
||||
)
|
||||
|
||||
#[cfg(target_word_size = "64")]
|
||||
impl_hash!(
|
||||
uint, u64, mem::to_le64;
|
||||
int, u64, mem::to_le64;
|
||||
)
|
||||
impl_hash!(u8, u8)
|
||||
impl_hash!(u16, u16)
|
||||
impl_hash!(u32, u32)
|
||||
impl_hash!(u64, u64)
|
||||
impl_hash!(uint, uint)
|
||||
impl_hash!(i8, u8)
|
||||
impl_hash!(i16, u16)
|
||||
impl_hash!(i32, u32)
|
||||
impl_hash!(i64, u64)
|
||||
impl_hash!(int, uint)
|
||||
|
||||
impl<S: Writer> Hash<S> for bool {
|
||||
#[inline]
|
||||
|
@ -11,6 +11,7 @@
|
||||
use alloc::arc::Arc;
|
||||
use libc;
|
||||
use std::mem;
|
||||
use std::mem::ByteOrder;
|
||||
use std::rt::mutex;
|
||||
use std::rt::rtio;
|
||||
use std::rt::rtio::{IoResult, IoError};
|
||||
@ -27,10 +28,10 @@ use super::util;
|
||||
#[cfg(unix)] pub type sock_t = super::file::fd_t;
|
||||
|
||||
pub fn htons(u: u16) -> u16 {
|
||||
mem::to_be16(u)
|
||||
u.to_big_endian()
|
||||
}
|
||||
pub fn ntohs(u: u16) -> u16 {
|
||||
mem::from_be16(u)
|
||||
ByteOrder::from_big_endian(u)
|
||||
}
|
||||
|
||||
enum InAddr {
|
||||
@ -46,7 +47,7 @@ fn ip_to_inaddr(ip: rtio::IpAddr) -> InAddr {
|
||||
(c as u32 << 8) |
|
||||
(d as u32 << 0);
|
||||
InAddr(libc::in_addr {
|
||||
s_addr: mem::from_be32(ip)
|
||||
s_addr: ByteOrder::from_big_endian(ip)
|
||||
})
|
||||
}
|
||||
rtio::Ipv6Addr(a, b, c, d, e, f, g, h) => {
|
||||
@ -180,7 +181,7 @@ pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
|
||||
let storage: &libc::sockaddr_in = unsafe {
|
||||
mem::transmute(storage)
|
||||
};
|
||||
let ip = mem::to_be32(storage.sin_addr.s_addr as u32);
|
||||
let ip = (storage.sin_addr.s_addr as u32).to_big_endian();
|
||||
let a = (ip >> 24) as u8;
|
||||
let b = (ip >> 16) as u8;
|
||||
let c = (ip >> 8) as u8;
|
||||
|
@ -11,6 +11,7 @@
|
||||
use libc::{size_t, ssize_t, c_int, c_void, c_uint};
|
||||
use libc;
|
||||
use std::mem;
|
||||
use std::mem::ByteOrder;
|
||||
use std::ptr;
|
||||
use std::rt::rtio;
|
||||
use std::rt::rtio::IoError;
|
||||
@ -30,8 +31,8 @@ use uvll;
|
||||
/// Generic functions related to dealing with sockaddr things
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub fn htons(u: u16) -> u16 { mem::to_be16(u) }
|
||||
pub fn ntohs(u: u16) -> u16 { mem::from_be16(u) }
|
||||
pub fn htons(u: u16) -> u16 { u.to_big_endian() }
|
||||
pub fn ntohs(u: u16) -> u16 { ByteOrder::from_big_endian(u) }
|
||||
|
||||
pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
|
||||
len: uint) -> rtio::SocketAddr {
|
||||
@ -41,7 +42,7 @@ pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
|
||||
let storage: &libc::sockaddr_in = unsafe {
|
||||
mem::transmute(storage)
|
||||
};
|
||||
let ip = mem::to_be32(storage.sin_addr.s_addr as u32);
|
||||
let ip = (storage.sin_addr.s_addr as u32).to_big_endian();
|
||||
let a = (ip >> 24) as u8;
|
||||
let b = (ip >> 16) as u8;
|
||||
let c = (ip >> 8) as u8;
|
||||
@ -89,7 +90,8 @@ fn addr_to_sockaddr(addr: rtio::SocketAddr) -> (libc::sockaddr_storage, uint) {
|
||||
(*storage).sin_family = libc::AF_INET as libc::sa_family_t;
|
||||
(*storage).sin_port = htons(addr.port);
|
||||
(*storage).sin_addr = libc::in_addr {
|
||||
s_addr: mem::from_be32(ip)
|
||||
s_addr: ByteOrder::from_big_endian(ip),
|
||||
|
||||
};
|
||||
mem::size_of::<libc::sockaddr_in>()
|
||||
}
|
||||
|
@ -154,7 +154,7 @@ pub mod reader {
|
||||
}
|
||||
|
||||
pub fn vuint_at(data: &[u8], start: uint) -> DecodeResult<Res> {
|
||||
use std::mem::from_be32;
|
||||
use std::mem::ByteOrder;
|
||||
|
||||
if data.len() - start < 4 {
|
||||
return vuint_at_slow(data, start);
|
||||
@ -185,7 +185,7 @@ pub mod reader {
|
||||
|
||||
unsafe {
|
||||
let ptr = data.as_ptr().offset(start as int) as *u32;
|
||||
let val = from_be32(*ptr);
|
||||
let val = ByteOrder::from_big_endian(*ptr);
|
||||
|
||||
let i = (val >> 28u) as uint;
|
||||
let (shift, mask) = SHIFT_MASK_TABLE[i];
|
||||
|
@ -209,7 +209,7 @@ impl Uuid {
|
||||
/// * `d3` A 16-bit word
|
||||
/// * `d4` Array of 8 octets
|
||||
pub fn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8]) -> Uuid {
|
||||
use std::mem::{to_be16, to_be32};
|
||||
use std::mem::ByteOrder;
|
||||
|
||||
// First construct a temporary field-based struct
|
||||
let mut fields = UuidFields {
|
||||
@ -219,9 +219,9 @@ impl Uuid {
|
||||
data4: [0, ..8]
|
||||
};
|
||||
|
||||
fields.data1 = to_be32(d1);
|
||||
fields.data2 = to_be16(d2);
|
||||
fields.data3 = to_be16(d3);
|
||||
fields.data1 = d1.to_big_endian();
|
||||
fields.data2 = d2.to_big_endian();
|
||||
fields.data3 = d3.to_big_endian();
|
||||
slice::bytes::copy_memory(fields.data4, d4);
|
||||
|
||||
unsafe {
|
||||
@ -335,16 +335,16 @@ impl Uuid {
|
||||
///
|
||||
/// Example: `550e8400-e29b-41d4-a716-446655440000`
|
||||
pub fn to_hyphenated_str(&self) -> String {
|
||||
use std::mem::{to_be16, to_be32};
|
||||
use std::mem::ByteOrder;
|
||||
// Convert to field-based struct as it matches groups in output.
|
||||
// Ensure fields are in network byte order, as per RFC.
|
||||
let mut uf: UuidFields;
|
||||
unsafe {
|
||||
uf = transmute_copy(&self.bytes);
|
||||
}
|
||||
uf.data1 = to_be32(uf.data1);
|
||||
uf.data2 = to_be16(uf.data2);
|
||||
uf.data3 = to_be16(uf.data3);
|
||||
uf.data1 = uf.data1.to_big_endian();
|
||||
uf.data2 = uf.data2.to_big_endian();
|
||||
uf.data3 = uf.data3.to_big_endian();
|
||||
let s = format!("{:08x}-{:04x}-{:04x}-{:02x}{:02x}-\
|
||||
{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
|
||||
uf.data1,
|
||||
|
Loading…
x
Reference in New Issue
Block a user