Shorten endian conversion method names
The consensus on #14917 was that the proposed names were too long.
This commit is contained in:
parent
779ca97525
commit
cb8ca2dafd
9 changed files with 84 additions and 84 deletions
|
@ -104,7 +104,7 @@ macro_rules! impl_hash {
|
|||
#[inline]
|
||||
fn hash(&self, state: &mut S) {
|
||||
let a: [u8, ..::core::$ty::BYTES] = unsafe {
|
||||
mem::transmute((*self as $uty).to_little_endian() as $ty)
|
||||
mem::transmute((*self as $uty).to_le() as $ty)
|
||||
};
|
||||
state.write(a.as_slice())
|
||||
}
|
||||
|
|
|
@ -173,85 +173,85 @@ pub unsafe fn move_val_init<T>(dst: &mut T, src: T) {
|
|||
///
|
||||
/// On little endian, this is a no-op. On big endian, the bytes are swapped.
|
||||
#[inline]
|
||||
#[deprecated = "use `Int::to_little_endian` instead"]
|
||||
pub fn to_le16(x: u16) -> u16 { x.to_little_endian() }
|
||||
#[deprecated = "use `Int::to_le` instead"]
|
||||
pub fn to_le16(x: u16) -> u16 { x.to_le() }
|
||||
|
||||
/// Convert an u32 to little endian from the target's endianness.
|
||||
///
|
||||
/// On little endian, this is a no-op. On big endian, the bytes are swapped.
|
||||
#[inline]
|
||||
#[deprecated = "use `Int::to_little_endian` instead"]
|
||||
pub fn to_le32(x: u32) -> u32 { x.to_little_endian() }
|
||||
#[deprecated = "use `Int::to_le` instead"]
|
||||
pub fn to_le32(x: u32) -> u32 { x.to_le() }
|
||||
|
||||
/// Convert an u64 to little endian from the target's endianness.
|
||||
///
|
||||
/// On little endian, this is a no-op. On big endian, the bytes are swapped.
|
||||
#[inline]
|
||||
#[deprecated = "use `Int::to_little_endian` instead"]
|
||||
pub fn to_le64(x: u64) -> u64 { x.to_little_endian() }
|
||||
#[deprecated = "use `Int::to_le` instead"]
|
||||
pub fn to_le64(x: u64) -> u64 { x.to_le() }
|
||||
|
||||
/// Convert an u16 to big endian from the target's endianness.
|
||||
///
|
||||
/// On big endian, this is a no-op. On little endian, the bytes are swapped.
|
||||
#[inline]
|
||||
#[deprecated = "use `Int::to_big_endian` instead"]
|
||||
pub fn to_be16(x: u16) -> u16 { x.to_big_endian() }
|
||||
#[deprecated = "use `Int::to_be` instead"]
|
||||
pub fn to_be16(x: u16) -> u16 { x.to_be() }
|
||||
|
||||
/// Convert an u32 to big endian from the target's endianness.
|
||||
///
|
||||
/// On big endian, this is a no-op. On little endian, the bytes are swapped.
|
||||
#[inline]
|
||||
#[deprecated = "use `Int::to_big_endian` instead"]
|
||||
pub fn to_be32(x: u32) -> u32 { x.to_big_endian() }
|
||||
#[deprecated = "use `Int::to_be` instead"]
|
||||
pub fn to_be32(x: u32) -> u32 { x.to_be() }
|
||||
|
||||
/// Convert an u64 to big endian from the target's endianness.
|
||||
///
|
||||
/// On big endian, this is a no-op. On little endian, the bytes are swapped.
|
||||
#[inline]
|
||||
#[deprecated = "use `Int::to_big_endian` instead"]
|
||||
pub fn to_be64(x: u64) -> u64 { x.to_big_endian() }
|
||||
#[deprecated = "use `Int::to_be` instead"]
|
||||
pub fn to_be64(x: u64) -> u64 { x.to_be() }
|
||||
|
||||
/// Convert an u16 from little endian to the target's endianness.
|
||||
///
|
||||
/// On little endian, this is a no-op. On big endian, the bytes are swapped.
|
||||
#[inline]
|
||||
#[deprecated = "use `Int::from_little_endian` instead"]
|
||||
pub fn from_le16(x: u16) -> u16 { Int::from_little_endian(x) }
|
||||
#[deprecated = "use `Int::from_le` instead"]
|
||||
pub fn from_le16(x: u16) -> u16 { Int::from_le(x) }
|
||||
|
||||
/// Convert an u32 from little endian to the target's endianness.
|
||||
///
|
||||
/// On little endian, this is a no-op. On big endian, the bytes are swapped.
|
||||
#[inline]
|
||||
#[deprecated = "use `Int::from_little_endian` instead"]
|
||||
pub fn from_le32(x: u32) -> u32 { Int::from_little_endian(x) }
|
||||
#[deprecated = "use `Int::from_le` instead"]
|
||||
pub fn from_le32(x: u32) -> u32 { Int::from_le(x) }
|
||||
|
||||
/// Convert an u64 from little endian to the target's endianness.
|
||||
///
|
||||
/// On little endian, this is a no-op. On big endian, the bytes are swapped.
|
||||
#[inline]
|
||||
#[deprecated = "use `Int::from_little_endian` instead"]
|
||||
pub fn from_le64(x: u64) -> u64 { Int::from_little_endian(x) }
|
||||
#[deprecated = "use `Int::from_le` instead"]
|
||||
pub fn from_le64(x: u64) -> u64 { Int::from_le(x) }
|
||||
|
||||
/// Convert an u16 from big endian to the target's endianness.
|
||||
///
|
||||
/// On big endian, this is a no-op. On little endian, the bytes are swapped.
|
||||
#[inline]
|
||||
#[deprecated = "use `Int::from_big_endian` instead"]
|
||||
pub fn from_be16(x: u16) -> u16 { Int::from_big_endian(x) }
|
||||
#[deprecated = "use `Int::from_be` instead"]
|
||||
pub fn from_be16(x: u16) -> u16 { Int::from_be(x) }
|
||||
|
||||
/// Convert an u32 from big endian to the target's endianness.
|
||||
///
|
||||
/// On big endian, this is a no-op. On little endian, the bytes are swapped.
|
||||
#[inline]
|
||||
#[deprecated = "use `Int::from_big_endian` instead"]
|
||||
pub fn from_be32(x: u32) -> u32 { Int::from_big_endian(x) }
|
||||
#[deprecated = "use `Int::from_be` instead"]
|
||||
pub fn from_be32(x: u32) -> u32 { Int::from_be(x) }
|
||||
|
||||
/// Convert an u64 from big endian to the target's endianness.
|
||||
///
|
||||
/// On big endian, this is a no-op. On little endian, the bytes are swapped.
|
||||
#[inline]
|
||||
#[deprecated = "use `Int::from_big_endian` instead"]
|
||||
pub fn from_be64(x: u64) -> u64 { Int::from_big_endian(x) }
|
||||
#[deprecated = "use `Int::from_be` instead"]
|
||||
pub fn from_be64(x: u64) -> u64 { Int::from_be(x) }
|
||||
|
||||
/// Swap the values at two mutable locations of the same type, without
|
||||
/// deinitialising or copying either one.
|
||||
|
|
|
@ -147,25 +147,25 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_little_endian() {
|
||||
assert_eq!(Int::from_little_endian(A.to_little_endian()), A);
|
||||
assert_eq!(Int::from_little_endian(B.to_little_endian()), B);
|
||||
assert_eq!(Int::from_little_endian(C.to_little_endian()), C);
|
||||
assert_eq!(Int::from_little_endian(_0), _0);
|
||||
assert_eq!(Int::from_little_endian(_1), _1);
|
||||
assert_eq!(_0.to_little_endian(), _0);
|
||||
assert_eq!(_1.to_little_endian(), _1);
|
||||
fn test_le() {
|
||||
assert_eq!(Int::from_le(A.to_le()), A);
|
||||
assert_eq!(Int::from_le(B.to_le()), B);
|
||||
assert_eq!(Int::from_le(C.to_le()), C);
|
||||
assert_eq!(Int::from_le(_0), _0);
|
||||
assert_eq!(Int::from_le(_1), _1);
|
||||
assert_eq!(_0.to_le(), _0);
|
||||
assert_eq!(_1.to_le(), _1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_big_endian() {
|
||||
assert_eq!(Int::from_big_endian(A.to_big_endian()), A);
|
||||
assert_eq!(Int::from_big_endian(B.to_big_endian()), B);
|
||||
assert_eq!(Int::from_big_endian(C.to_big_endian()), C);
|
||||
assert_eq!(Int::from_big_endian(_0), _0);
|
||||
assert_eq!(Int::from_big_endian(_1), _1);
|
||||
assert_eq!(_0.to_big_endian(), _0);
|
||||
assert_eq!(_1.to_big_endian(), _1);
|
||||
fn test_be() {
|
||||
assert_eq!(Int::from_be(A.to_be()), A);
|
||||
assert_eq!(Int::from_be(B.to_be()), B);
|
||||
assert_eq!(Int::from_be(C.to_be()), C);
|
||||
assert_eq!(Int::from_be(_0), _0);
|
||||
assert_eq!(Int::from_be(_1), _1);
|
||||
assert_eq!(_0.to_be(), _0);
|
||||
assert_eq!(_1.to_be(), _1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -498,13 +498,13 @@ pub trait Int: Primitive
|
|||
/// let n = 0x0123456789ABCDEFu64;
|
||||
///
|
||||
/// if cfg!(target_endian = "big") {
|
||||
/// assert_eq!(Int::from_big_endian(n), n)
|
||||
/// assert_eq!(Int::from_be(n), n)
|
||||
/// } else {
|
||||
/// assert_eq!(Int::from_big_endian(n), n.swap_bytes())
|
||||
/// assert_eq!(Int::from_be(n), n.swap_bytes())
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
fn from_big_endian(x: Self) -> Self {
|
||||
fn from_be(x: Self) -> Self {
|
||||
if cfg!(target_endian = "big") { x } else { x.swap_bytes() }
|
||||
}
|
||||
|
||||
|
@ -518,13 +518,13 @@ pub trait Int: Primitive
|
|||
/// let n = 0x0123456789ABCDEFu64;
|
||||
///
|
||||
/// if cfg!(target_endian = "little") {
|
||||
/// assert_eq!(Int::from_little_endian(n), n)
|
||||
/// assert_eq!(Int::from_le(n), n)
|
||||
/// } else {
|
||||
/// assert_eq!(Int::from_little_endian(n), n.swap_bytes())
|
||||
/// assert_eq!(Int::from_le(n), n.swap_bytes())
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
fn from_little_endian(x: Self) -> Self {
|
||||
fn from_le(x: Self) -> Self {
|
||||
if cfg!(target_endian = "little") { x } else { x.swap_bytes() }
|
||||
}
|
||||
|
||||
|
@ -538,13 +538,13 @@ pub trait Int: Primitive
|
|||
/// let n = 0x0123456789ABCDEFu64;
|
||||
///
|
||||
/// if cfg!(target_endian = "big") {
|
||||
/// assert_eq!(n.to_big_endian(), n)
|
||||
/// assert_eq!(n.to_be(), n)
|
||||
/// } else {
|
||||
/// assert_eq!(n.to_big_endian(), n.swap_bytes())
|
||||
/// assert_eq!(n.to_be(), n.swap_bytes())
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
fn to_big_endian(self) -> Self {
|
||||
fn to_be(self) -> Self { // or not to be?
|
||||
if cfg!(target_endian = "big") { self } else { self.swap_bytes() }
|
||||
}
|
||||
|
||||
|
@ -558,13 +558,13 @@ pub trait Int: Primitive
|
|||
/// let n = 0x0123456789ABCDEFu64;
|
||||
///
|
||||
/// if cfg!(target_endian = "little") {
|
||||
/// assert_eq!(n.to_little_endian(), n)
|
||||
/// assert_eq!(n.to_le(), n)
|
||||
/// } else {
|
||||
/// assert_eq!(n.to_little_endian(), n.swap_bytes())
|
||||
/// assert_eq!(n.to_le(), n.swap_bytes())
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
fn to_little_endian(self) -> Self {
|
||||
fn to_le(self) -> Self {
|
||||
if cfg!(target_endian = "little") { self } else { self.swap_bytes() }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -98,25 +98,25 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_little_endian() {
|
||||
assert_eq!(Int::from_little_endian(A.to_little_endian()), A);
|
||||
assert_eq!(Int::from_little_endian(B.to_little_endian()), B);
|
||||
assert_eq!(Int::from_little_endian(C.to_little_endian()), C);
|
||||
assert_eq!(Int::from_little_endian(_0), _0);
|
||||
assert_eq!(Int::from_little_endian(_1), _1);
|
||||
assert_eq!(_0.to_little_endian(), _0);
|
||||
assert_eq!(_1.to_little_endian(), _1);
|
||||
fn test_le() {
|
||||
assert_eq!(Int::from_le(A.to_le()), A);
|
||||
assert_eq!(Int::from_le(B.to_le()), B);
|
||||
assert_eq!(Int::from_le(C.to_le()), C);
|
||||
assert_eq!(Int::from_le(_0), _0);
|
||||
assert_eq!(Int::from_le(_1), _1);
|
||||
assert_eq!(_0.to_le(), _0);
|
||||
assert_eq!(_1.to_le(), _1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_big_endian() {
|
||||
assert_eq!(Int::from_big_endian(A.to_big_endian()), A);
|
||||
assert_eq!(Int::from_big_endian(B.to_big_endian()), B);
|
||||
assert_eq!(Int::from_big_endian(C.to_big_endian()), C);
|
||||
assert_eq!(Int::from_big_endian(_0), _0);
|
||||
assert_eq!(Int::from_big_endian(_1), _1);
|
||||
assert_eq!(_0.to_big_endian(), _0);
|
||||
assert_eq!(_1.to_big_endian(), _1);
|
||||
fn test_be() {
|
||||
assert_eq!(Int::from_be(A.to_be()), A);
|
||||
assert_eq!(Int::from_be(B.to_be()), B);
|
||||
assert_eq!(Int::from_be(C.to_be()), C);
|
||||
assert_eq!(Int::from_be(_0), _0);
|
||||
assert_eq!(Int::from_be(_1), _1);
|
||||
assert_eq!(_0.to_be(), _0);
|
||||
assert_eq!(_1.to_be(), _1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -27,10 +27,10 @@ use super::util;
|
|||
#[cfg(unix)] pub type sock_t = super::file::fd_t;
|
||||
|
||||
pub fn htons(u: u16) -> u16 {
|
||||
u.to_big_endian()
|
||||
u.to_be()
|
||||
}
|
||||
pub fn ntohs(u: u16) -> u16 {
|
||||
Int::from_big_endian(u)
|
||||
Int::from_be(u)
|
||||
}
|
||||
|
||||
enum InAddr {
|
||||
|
@ -46,7 +46,7 @@ fn ip_to_inaddr(ip: rtio::IpAddr) -> InAddr {
|
|||
(c as u32 << 8) |
|
||||
(d as u32 << 0);
|
||||
InAddr(libc::in_addr {
|
||||
s_addr: Int::from_big_endian(ip)
|
||||
s_addr: Int::from_be(ip)
|
||||
})
|
||||
}
|
||||
rtio::Ipv6Addr(a, b, c, d, e, f, g, h) => {
|
||||
|
@ -180,7 +180,7 @@ pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
|
|||
let storage: &libc::sockaddr_in = unsafe {
|
||||
mem::transmute(storage)
|
||||
};
|
||||
let ip = (storage.sin_addr.s_addr as u32).to_big_endian();
|
||||
let ip = (storage.sin_addr.s_addr as u32).to_be();
|
||||
let a = (ip >> 24) as u8;
|
||||
let b = (ip >> 16) as u8;
|
||||
let c = (ip >> 8) as u8;
|
||||
|
|
|
@ -30,8 +30,8 @@ use uvll;
|
|||
/// Generic functions related to dealing with sockaddr things
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub fn htons(u: u16) -> u16 { u.to_big_endian() }
|
||||
pub fn ntohs(u: u16) -> u16 { Int::from_big_endian(u) }
|
||||
pub fn htons(u: u16) -> u16 { u.to_be() }
|
||||
pub fn ntohs(u: u16) -> u16 { Int::from_be(u) }
|
||||
|
||||
pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
|
||||
len: uint) -> rtio::SocketAddr {
|
||||
|
@ -41,7 +41,7 @@ pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
|
|||
let storage: &libc::sockaddr_in = unsafe {
|
||||
mem::transmute(storage)
|
||||
};
|
||||
let ip = (storage.sin_addr.s_addr as u32).to_big_endian();
|
||||
let ip = (storage.sin_addr.s_addr as u32).to_be();
|
||||
let a = (ip >> 24) as u8;
|
||||
let b = (ip >> 16) as u8;
|
||||
let c = (ip >> 8) as u8;
|
||||
|
@ -89,7 +89,7 @@ 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: Int::from_big_endian(ip),
|
||||
s_addr: Int::from_be(ip),
|
||||
|
||||
};
|
||||
mem::size_of::<libc::sockaddr_in>()
|
||||
|
|
|
@ -183,7 +183,7 @@ pub mod reader {
|
|||
|
||||
unsafe {
|
||||
let ptr = data.as_ptr().offset(start as int) as *u32;
|
||||
let val = Int::from_big_endian(*ptr);
|
||||
let val = Int::from_be(*ptr);
|
||||
|
||||
let i = (val >> 28u) as uint;
|
||||
let (shift, mask) = SHIFT_MASK_TABLE[i];
|
||||
|
|
|
@ -217,9 +217,9 @@ impl Uuid {
|
|||
data4: [0, ..8]
|
||||
};
|
||||
|
||||
fields.data1 = d1.to_big_endian();
|
||||
fields.data2 = d2.to_big_endian();
|
||||
fields.data3 = d3.to_big_endian();
|
||||
fields.data1 = d1.to_be();
|
||||
fields.data2 = d2.to_be();
|
||||
fields.data3 = d3.to_be();
|
||||
slice::bytes::copy_memory(fields.data4, d4);
|
||||
|
||||
unsafe {
|
||||
|
@ -339,9 +339,9 @@ impl Uuid {
|
|||
unsafe {
|
||||
uf = transmute_copy(&self.bytes);
|
||||
}
|
||||
uf.data1 = uf.data1.to_big_endian();
|
||||
uf.data2 = uf.data2.to_big_endian();
|
||||
uf.data3 = uf.data3.to_big_endian();
|
||||
uf.data1 = uf.data1.to_be();
|
||||
uf.data2 = uf.data2.to_be();
|
||||
uf.data3 = uf.data3.to_be();
|
||||
let s = format!("{:08x}-{:04x}-{:04x}-{:02x}{:02x}-\
|
||||
{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
|
||||
uf.data1,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue