Skip to content

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

Merged
merged 2 commits into from
Mar 4, 2024

Conversation

SteveMcFarlin
Copy link

@SteveMcFarlin SteveMcFarlin commented Mar 3, 2024

This PR is to address issue #5. I have also removed a redundant guard per advice from clippy.

This is in reference to issue PacktPublishing#5
I have also removed a redundant guard per advice from clippy
@cfsamson
Copy link
Collaborator

cfsamson commented Mar 3, 2024

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:

  1. Touch as little code as possible so the example doesn't deviate too much from the book
  2. Make it very clear for users that only clone the repository and doesn't check the issue tracker what changes are made.

So, changing match statement Ok(n) if n == 0 to Ok(0) is something I want to avoid. It's a better way to write it (and I use that later in the book from chapter 7 onwards), but readers will wonder why the code is different from the book and it's not an important change to make.

For the conditional compilation, I would prefer to use cfg_attr instead so we don't have to duplicate the whole struct declaration:

#[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 main.rs, and make the change like outlined above.

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.

@SteveMcFarlin
Copy link
Author

I will do that this evening, and revert the change in main.rs. I totally get why you want to keep the code as close to the book as possible. Thanks.

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
@SteveMcFarlin
Copy link
Author

Should be good to go. I do like the cfg_attr solution. Far cleaner.

@cfsamson
Copy link
Collaborator

cfsamson commented Mar 4, 2024

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.

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.

Should be good to go. I do like the cfg_attr solution. Far cleaner.

Looks good now, thanks for making the PR 👍

@cfsamson cfsamson merged commit d3bea0c into PacktPublishing:main Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants