Skip to content

Commit 7e26e7b

Browse files
author
Steve McFarlin
committed
Revert match conditional, and use cfg_attr to avoid struct duplication
Tested on: - Docker - Linux 5.10.104-linuxkit aarch64 - Pop-OS! 22.04 amd64
1 parent 9719bae commit 7e26e7b

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

ch04/a-epoll/src/ffi.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
//! # FIXES:
2+
//! The number is identical to the number in the GitHub issue tracker
3+
//!
4+
//! ## FIX ISSUE #5
5+
//! See: https://github.com/PacktPublishing/Asynchronous-Programming-in-Rust/issues/5
6+
//! Readers reported wrong results when running the example on ARM64 instruction set
7+
//! (aarch64). The reason turned out to be that the `Event` struct is only `repr(packed)`
8+
//! on `x86-64` systems due to backwards compatibility. Fixed by conditionally
9+
//! compiling the #[repr(packed)] attribute.
10+
111
pub const EPOLL_CTL_ADD: i32 = 1;
212
pub const EPOLLIN: i32 = 0x1;
313
pub const EPOLLET: i32 = 1 << 31;
@@ -11,17 +21,9 @@ extern "C" {
1121
}
1222

1323
#[derive(Debug)]
14-
#[cfg(not(target_arch = "aarch64"))]
15-
#[repr(C, packed)]
16-
pub struct Event {
17-
pub(crate) events: u32,
18-
// Token to identify event
19-
pub(crate) epoll_data: usize,
20-
}
21-
22-
#[derive(Debug)]
23-
#[cfg(target_arch = "aarch64")]
2424
#[repr(C)]
25+
// FIX #5
26+
#[cfg_attr(target_arch = "x86_64", repr(packed))]
2527
pub struct Event {
2628
pub(crate) events: u32,
2729
// Token to identify event

ch04/a-epoll/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn handle_events(
4949

5050
loop {
5151
match streams[index].read(&mut data) {
52-
Ok(0) => {
52+
Ok(n) if n == 0 => {
5353
// FIX #4
5454
// `insert` returns false if the value already existed in the set. We
5555
// handle it here since we must be sure that the TcpStream is fully

0 commit comments

Comments
 (0)