-
Notifications
You must be signed in to change notification settings - Fork 72
Conditionaly compile based on architecture #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This is in reference to issue PacktPublishing#5 I have also removed a redundant guard per advice from clippy
Thanks for submitting this fix. I've been thinking on how to best handle fixes to examples that's also in the book, and there are two things I want to consider:
So, changing match statement For the conditional compilation, I would prefer to use #[derive(Debug)]
#[repr(C)]
#[cfg_attr(target_arch = "x86_64", repr(packed))]
pub struct Event {
pub(crate) events: u32,
// Token to identify event
pub(crate) epoll_data: usize,
} The new ffi.rs file would look like this: //! # FIXES:
//! The number is identical to the number in the GitHub issue tracker
//!
//! ## FIX ISSUE #5
//! See: https://github.com/PacktPublishing/Asynchronous-Programming-in-Rust/issues/5
//! Readers reported wrong results when running the example on ARM64 instruction set
//! (aarch64). The reason turned out to be that the `Event` struct is only `repr(packed)`
//! on `x86-64` systems due to backwards compatibility. Fixed by conditionally
//! compiling the #[repr(packed)] attribute.
pub const EPOLL_CTL_ADD: i32 = 1;
pub const EPOLLIN: i32 = 0x1;
pub const EPOLLET: i32 = 1 << 31;
#[link(name = "c")]
extern "C" {
pub fn epoll_create(size: i32) -> i32;
pub fn close(fd: i32) -> i32;
pub fn epoll_ctl(epfd: i32, op: i32, fd: i32, event: *mut Event) -> i32;
pub fn epoll_wait(epfd: i32, events: *mut Event, maxevents: i32, timeout: i32) -> i32;
}
#[derive(Debug)]
#[repr(C)]
// FIX #5
#[cfg_attr(target_arch = "x86_64", repr(packed))]
pub struct Event {
pub(crate) events: u32,
// Token to identify event
pub(crate) epoll_data: usize,
}
impl Event {
pub fn token(&self) -> usize {
self.epoll_data
}
} I can make these changes unless you want to get the PR in, in which case I would like to drop the change to Could you try to run it just so I'm 100% sure it works as expected on aarch64? I would greatly appreciate that since I don't have that platform combination easily availabe to me. |
I will do that this evening, and revert the change in I am really enjoying the book even though I am really familiar with a majority of it. I was happy you released it as I wanted to test myself, and have some fun while doing it. Well done on making these topics approachable to such a wide audience. |
Tested on: - Docker - Linux 5.10.104-linuxkit aarch64 - Pop-OS! 22.04 amd64
Should be good to go. I do like the cfg_attr solution. Far cleaner. |
That makes me really happy to hear. I've tried my best to make it approachable for a wide audience, so that is just great to hear. And also lovely to get feedback from readers that have experience with some of these topics as well since I don't want it to be boring for readers with prior knowledge. Thanks.
Looks good now, thanks for making the PR 👍 |
This PR is to address issue #5. I have also removed a redundant guard per advice from clippy.