std: Move byteswap functions to mem
This commit is contained in:
parent
c7710cdf45
commit
073b655187
@ -15,7 +15,6 @@ use std::libc;
|
||||
use std::mem;
|
||||
use std::rt::rtio;
|
||||
use std::sync::arc::UnsafeArc;
|
||||
use std::unstable::intrinsics;
|
||||
|
||||
use super::{IoResult, retry};
|
||||
use super::file::keep_going;
|
||||
@ -28,10 +27,10 @@ use super::file::keep_going;
|
||||
#[cfg(unix)] pub type sock_t = super::file::fd_t;
|
||||
|
||||
pub fn htons(u: u16) -> u16 {
|
||||
intrinsics::to_be16(u as i16) as u16
|
||||
mem::to_be16(u as i16) as u16
|
||||
}
|
||||
pub fn ntohs(u: u16) -> u16 {
|
||||
intrinsics::from_be16(u as i16) as u16
|
||||
mem::from_be16(u as i16) as u16
|
||||
}
|
||||
|
||||
enum InAddr {
|
||||
|
@ -22,7 +22,7 @@ use extra::hex::ToHex;
|
||||
/// format.
|
||||
fn write_u32_be(dst: &mut[u8], input: u32) {
|
||||
use std::cast::transmute;
|
||||
use std::unstable::intrinsics::to_be32;
|
||||
use std::mem::to_be32;
|
||||
assert!(dst.len() == 4);
|
||||
unsafe {
|
||||
let x: *mut i32 = transmute(dst.unsafe_mut_ref(0));
|
||||
@ -33,7 +33,7 @@ fn write_u32_be(dst: &mut[u8], input: u32) {
|
||||
/// Read a vector of bytes into a vector of u32s. The values are read in big-endian format.
|
||||
fn read_u32v_be(dst: &mut[u32], input: &[u8]) {
|
||||
use std::cast::transmute;
|
||||
use std::unstable::intrinsics::to_be32;
|
||||
use std::mem::to_be32;
|
||||
assert!(dst.len() * 4 == input.len());
|
||||
unsafe {
|
||||
let mut x: *mut i32 = transmute(dst.unsafe_mut_ref(0));
|
||||
|
@ -17,7 +17,6 @@ use std::mem;
|
||||
use std::ptr;
|
||||
use std::rt::rtio;
|
||||
use std::rt::task::BlockedTask;
|
||||
use std::unstable::intrinsics;
|
||||
|
||||
use access::Access;
|
||||
use homing::{HomingIO, HomeHandle};
|
||||
@ -33,8 +32,8 @@ use uvll;
|
||||
/// Generic functions related to dealing with sockaddr things
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub fn htons(u: u16) -> u16 { intrinsics::to_be16(u as i16) as u16 }
|
||||
pub fn ntohs(u: u16) -> u16 { intrinsics::from_be16(u as i16) as u16 }
|
||||
pub fn htons(u: u16) -> u16 { mem::to_be16(u as i16) as u16 }
|
||||
pub fn ntohs(u: u16) -> u16 { mem::from_be16(u as i16) as u16 }
|
||||
|
||||
pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
|
||||
len: uint) -> ip::SocketAddr {
|
||||
|
@ -132,7 +132,7 @@ pub mod reader {
|
||||
|
||||
pub fn vuint_at(data: &[u8], start: uint) -> Res {
|
||||
use std::ptr::offset;
|
||||
use std::unstable::intrinsics::from_be32;
|
||||
use std::mem::from_be32;
|
||||
|
||||
if data.len() - start < 4 {
|
||||
return vuint_at_slow(data, start);
|
||||
|
@ -51,7 +51,7 @@ impl<'r, R: Reader> Iterator<u8> for Bytes<'r, R> {
|
||||
}
|
||||
|
||||
pub fn u64_to_le_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
|
||||
use unstable::intrinsics::{to_le16, to_le32, to_le64};
|
||||
use mem::{to_le16, to_le32, to_le64};
|
||||
use cast::transmute;
|
||||
|
||||
// LLVM fails to properly optimize this when using shifts instead of the to_le* intrinsics
|
||||
@ -77,7 +77,7 @@ pub fn u64_to_le_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
|
||||
}
|
||||
|
||||
pub fn u64_to_be_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
|
||||
use unstable::intrinsics::{to_be16, to_be32, to_be64};
|
||||
use mem::{to_be16, to_be32, to_be64};
|
||||
use cast::transmute;
|
||||
|
||||
// LLVM fails to properly optimize this when using shifts instead of the to_be* intrinsics
|
||||
@ -105,7 +105,7 @@ pub fn u64_from_be_bytes(data: &[u8],
|
||||
size: uint)
|
||||
-> u64 {
|
||||
use ptr::{copy_nonoverlapping_memory, offset, mut_offset};
|
||||
use unstable::intrinsics::from_be64;
|
||||
use mem::from_be64;
|
||||
use vec::MutableVector;
|
||||
|
||||
assert!(size <= 8u);
|
||||
|
@ -10,7 +10,10 @@
|
||||
|
||||
//! Functions relating to memory layout
|
||||
|
||||
#[allow(missing_doc)]; // FIXME
|
||||
|
||||
use unstable::intrinsics;
|
||||
use unstable::intrinsics::{bswap16, bswap32, bswap64};
|
||||
|
||||
/// Returns the size of a type
|
||||
#[inline]
|
||||
@ -91,6 +94,34 @@ pub unsafe fn move_val_init<T>(dst: &mut T, src: T) {
|
||||
intrinsics::move_val_init(dst, src)
|
||||
}
|
||||
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn to_le16(x: i16) -> i16 { x }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn to_le16(x: i16) -> i16 { unsafe { bswap16(x) } }
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn to_le32(x: i32) -> i32 { x }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn to_le32(x: i32) -> i32 { unsafe { bswap32(x) } }
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn to_le64(x: i64) -> i64 { x }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn to_le64(x: i64) -> i64 { unsafe { bswap64(x) } }
|
||||
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn to_be16(x: i16) -> i16 { unsafe { bswap16(x) } }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn to_be16(x: i16) -> i16 { x }
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn to_be32(x: i32) -> i32 { unsafe { bswap32(x) } }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn to_be32(x: i32) -> i32 { x }
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn to_be64(x: i64) -> i64 { unsafe { bswap64(x) } }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn to_be64(x: i64) -> i64 { x }
|
||||
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn from_le16(x: i16) -> i16 { x }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn from_le16(x: i16) -> i16 { unsafe { bswap16(x) } }
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn from_le32(x: i32) -> i32 { x }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn from_le32(x: i32) -> i32 { unsafe { bswap32(x) } }
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn from_le64(x: i64) -> i64 { x }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn from_le64(x: i64) -> i64 { unsafe { bswap64(x) } }
|
||||
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn from_be16(x: i16) -> i16 { unsafe { bswap16(x) } }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn from_be16(x: i16) -> i16 { x }
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn from_be32(x: i32) -> i32 { unsafe { bswap32(x) } }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn from_be32(x: i32) -> i32 { x }
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn from_be64(x: i64) -> i64 { unsafe { bswap64(x) } }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn from_be64(x: i64) -> i64 { x }
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
@ -439,33 +439,6 @@ extern "rust-intrinsic" {
|
||||
pub fn u64_mul_with_overflow(x: u64, y: u64) -> (u64, bool);
|
||||
}
|
||||
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn to_le16(x: i16) -> i16 { x }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn to_le16(x: i16) -> i16 { unsafe { bswap16(x) } }
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn to_le32(x: i32) -> i32 { x }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn to_le32(x: i32) -> i32 { unsafe { bswap32(x) } }
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn to_le64(x: i64) -> i64 { x }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn to_le64(x: i64) -> i64 { unsafe { bswap64(x) } }
|
||||
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn to_be16(x: i16) -> i16 { unsafe { bswap16(x) } }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn to_be16(x: i16) -> i16 { x }
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn to_be32(x: i32) -> i32 { unsafe { bswap32(x) } }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn to_be32(x: i32) -> i32 { x }
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn to_be64(x: i64) -> i64 { unsafe { bswap64(x) } }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn to_be64(x: i64) -> i64 { x }
|
||||
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn from_le16(x: i16) -> i16 { x }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn from_le16(x: i16) -> i16 { unsafe { bswap16(x) } }
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn from_le32(x: i32) -> i32 { x }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn from_le32(x: i32) -> i32 { unsafe { bswap32(x) } }
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn from_le64(x: i64) -> i64 { x }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn from_le64(x: i64) -> i64 { unsafe { bswap64(x) } }
|
||||
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn from_be16(x: i16) -> i16 { unsafe { bswap16(x) } }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn from_be16(x: i16) -> i16 { x }
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn from_be32(x: i32) -> i32 { unsafe { bswap32(x) } }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn from_be32(x: i32) -> i32 { x }
|
||||
#[cfg(target_endian = "little")] #[inline] pub fn from_be64(x: i64) -> i64 { unsafe { bswap64(x) } }
|
||||
#[cfg(target_endian = "big")] #[inline] pub fn from_be64(x: i64) -> i64 { x }
|
||||
|
||||
/// `TypeId` represents a globally unique identifier for a type
|
||||
#[lang="type_id"] // This needs to be kept in lockstep with the code in trans/intrinsic.rs and
|
||||
|
@ -203,7 +203,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::unstable::intrinsics::{to_be16, to_be32};
|
||||
use std::mem::{to_be16, to_be32};
|
||||
|
||||
// First construct a temporary field-based struct
|
||||
let mut fields = UuidFields {
|
||||
@ -329,7 +329,7 @@ impl Uuid {
|
||||
///
|
||||
/// Example: `550e8400-e29b-41d4-a716-446655440000`
|
||||
pub fn to_hyphenated_str(&self) -> ~str {
|
||||
use std::unstable::intrinsics::{to_be16, to_be32};
|
||||
use std::mem::{to_be16, to_be32};
|
||||
// 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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user