Skip to content

Commit 08b0206

Browse files
committed
Replace endianness check with from/to_ne_bytes
1 parent 8990b9e commit 08b0206

File tree

2 files changed

+6
-42
lines changed

2 files changed

+6
-42
lines changed

tests/fail-dep/libc/libc_eventfd_write_block.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,15 @@ fn main() {
66
// This will pass when blocking is implemented.
77
let flags = libc::EFD_CLOEXEC;
88
let fd = unsafe { libc::eventfd(0, flags) };
9-
let mut sized_8_data: [u8; 8];
10-
119
// Write u64 - 1.
12-
if cfg!(target_endian = "big") {
13-
// Adjust the data based on the endianness of host system.
14-
sized_8_data = [255, 255, 255, 255, 255, 255, 255, 254];
15-
} else {
16-
sized_8_data = [254, 255, 255, 255, 255, 255, 255, 255];
17-
}
10+
let mut sized_8_data: [u8; 8] = (u64::MAX - 1).to_ne_bytes();
1811
let res: i64 = unsafe {
1912
libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap()
2013
};
2114
assert_eq!(res, 8);
2215

2316
// Write 1.
24-
if cfg!(target_endian = "big") {
25-
// Adjust the data based on the endianess of host system.
26-
sized_8_data = [0, 0, 0, 0, 0, 0, 0, 1];
27-
} else {
28-
sized_8_data = [1, 0, 0, 0, 0, 0, 0, 0];
29-
}
17+
sized_8_data = 1_u64.to_ne_bytes();
3018
// Write 1 to the counter.
3119
let _res: i64 = unsafe {
3220
libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap() //~ERROR: blocking is unsupported

tests/pass-dep/libc/libc-eventfd.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@ fn main() {
1313
fn test_read_write() {
1414
let flags = libc::EFD_NONBLOCK | libc::EFD_CLOEXEC;
1515
let fd = unsafe { libc::eventfd(0, flags) };
16-
let sized_8_data: [u8; 8];
17-
if cfg!(target_endian = "big") {
18-
// Adjust the data based on the endianness of host system.
19-
sized_8_data = 1_i64.to_be_bytes();
20-
} else {
21-
sized_8_data = 1_i64.to_le_bytes();
22-
}
16+
let sized_8_data: [u8; 8] = 1_u64.to_ne_bytes();
2317
// Write 1 to the counter.
2418
let res: i64 = unsafe {
2519
libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap()
@@ -34,13 +28,7 @@ fn test_read_write() {
3428
// Read returns number of bytes has been read, which is always 8.
3529
assert_eq!(res, 8);
3630
// Check the value of counter read.
37-
let counter: u64;
38-
if cfg!(target_endian = "big") {
39-
// Read will store the bytes based on the endianess of the host system.
40-
counter = u64::from_be_bytes(buf);
41-
} else {
42-
counter = u64::from_le_bytes(buf);
43-
}
31+
let counter = u64::from_ne_bytes(buf);
4432
assert_eq!(counter, 1);
4533

4634
// After read, the counter is currently 0, read counter 0 should fail with return
@@ -105,24 +93,12 @@ fn test_race() {
10593
};
10694
// read returns number of bytes has been read, which is always 8.
10795
assert_eq!(res, 8);
108-
let counter: u64;
109-
if cfg!(target_endian = "big") {
110-
// Read will store the bytes based on the endianness of the host system.
111-
counter = u64::from_be_bytes(buf);
112-
} else {
113-
counter = u64::from_le_bytes(buf);
114-
}
96+
let counter = u64::from_ne_bytes(buf);
11597
assert_eq!(counter, 1);
11698
unsafe { assert_eq!(VAL, 1) };
11799
});
118100
unsafe { VAL = 1 };
119-
let data: [u8; 8];
120-
if cfg!(target_endian = "big") {
121-
// Adjust the data based on the endianness of host system.
122-
data = [0, 0, 0, 0, 0, 0, 0, 1];
123-
} else {
124-
data = [1, 0, 0, 0, 0, 0, 0, 0];
125-
}
101+
let data: [u8; 8] = 1_u64.to_ne_bytes();
126102
let res: i64 =
127103
unsafe { libc::write(fd, data.as_ptr() as *const libc::c_void, 8).try_into().unwrap() };
128104
// write returns number of bytes written, which is always 8.

0 commit comments

Comments
 (0)