Skip to content

Commit 3e933b1

Browse files
committed
auto merge of #7194 : jensnockert/rust/endian, r=cmr
They simply byte-swap an integer to a specific endian, like the hton* functions in C. These intrinsics are synthesized, so maybe they should be in another file. But since they are just a single line of code each, based on the bswap intrinsics and aren't really intended for public consumption I thought they would fit in the intrinsics file. The next step working on this could be to expose a trait / generic function for byteswapping.
2 parents e9897cd + bc6848d commit 3e933b1

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/libstd/unstable/intrinsics.rs

+14
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,17 @@ pub extern "rust-intrinsic" {
417417
pub fn bswap32(x: i32) -> i32;
418418
pub fn bswap64(x: i64) -> i64;
419419
}
420+
421+
#[cfg(target_endian = "little")] pub fn to_le16(x: i16) -> i16 { x }
422+
#[cfg(target_endian = "big")] pub fn to_le16(x: i16) -> i16 { unsafe { bswap16(x) } }
423+
#[cfg(target_endian = "little")] pub fn to_le32(x: i32) -> i32 { x }
424+
#[cfg(target_endian = "big")] pub fn to_le32(x: i32) -> i32 { unsafe { bswap32(x) } }
425+
#[cfg(target_endian = "little")] pub fn to_le64(x: i64) -> i64 { x }
426+
#[cfg(target_endian = "big")] pub fn to_le64(x: i64) -> i64 { unsafe { bswap64(x) } }
427+
428+
#[cfg(target_endian = "little")] pub fn to_be16(x: i16) -> i16 { unsafe { bswap16(x) } }
429+
#[cfg(target_endian = "big")] pub fn to_be16(x: i16) -> i16 { x }
430+
#[cfg(target_endian = "little")] pub fn to_be32(x: i32) -> i32 { unsafe { bswap32(x) } }
431+
#[cfg(target_endian = "big")] pub fn to_be32(x: i32) -> i32 { x }
432+
#[cfg(target_endian = "little")] pub fn to_be64(x: i64) -> i64 { unsafe { bswap64(x) } }
433+
#[cfg(target_endian = "big")] pub fn to_be64(x: i64) -> i64 { x }

0 commit comments

Comments
 (0)