Skip to content

Commit

Permalink
Use ByteOrder methods instead of free-standing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab authored and alexcrichton committed Jun 19, 2014
1 parent 87c529c commit b84d17d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 56 deletions.
61 changes: 23 additions & 38 deletions src/libcollections/hash/mod.rs
Expand Up @@ -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]
Expand Down
9 changes: 5 additions & 4 deletions src/libnative/io/net.rs
Expand Up @@ -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};
Expand All @@ -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 {
Expand All @@ -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) => {
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions src/librustuv/net.rs
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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>()
}
Expand Down
4 changes: 2 additions & 2 deletions src/libserialize/ebml.rs
Expand Up @@ -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);
Expand Down Expand Up @@ -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];
Expand Down
16 changes: 8 additions & 8 deletions src/libuuid/lib.rs
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b84d17d

Please sign in to comment.