Skip to content
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

Warning when using packed_bits on a field wider than u8. #15

Open
konkers opened this issue Dec 9, 2020 · 1 comment
Open

Warning when using packed_bits on a field wider than u8. #15

konkers opened this issue Dec 9, 2020 · 1 comment

Comments

@konkers
Copy link

konkers commented Dec 9, 2020

I'm trying to create a descriptor for a joystick with 10 buttons:

#[gen_hid_descriptor(
    (collection = APPLICATION, usage_page = GENERIC_DESKTOP, usage = JOYSTICK) = {
        (usage = X,) = {x=input;};
        (usage = Y,) = {y=input;};
        (usage_page = BUTTON, usage_min = BUTTON_1, usage_max = 10) = {
            #[packed_bits 10] #[item_settings data,variable,absolute] buttons=input;
        };
    }
)]
struct InputReport {
    x: u8,
    y: u8,
    buttons: u16,
}

This yields the following warning:

warning: borrow of packed field is unsafe and requires unsafe function or block (error E0133)
  --> targets\stm32f072-disco\src\main.rs:23:1
   |
23 | / #[gen_hid_descriptor(
24 | |     (collection = APPLICATION, usage_page = GENERIC_DESKTOP, usage = JOYSTICK) = {
25 | |         (usage = X,) = {x=input;};
26 | |         (usage = Y,) = {y=input;};
...  |
30 | |     }
31 | | )]
   | |__^
   |
   = note: `#[warn(safe_packed_borrows)]` on by default
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
   = note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior
   = note: this warning originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
@konkers
Copy link
Author

konkers commented Dec 9, 2020

A workaround seems to be to split the buttons into two u8 fields:

#[gen_hid_descriptor(
    (collection = APPLICATION, usage_page = GENERIC_DESKTOP, usage = JOYSTICK) = {
        (usage = X,) = {x=input;};
        (usage = Y,) = {y=input;};
        (usage_page = BUTTON, usage_min = BUTTON_1, usage_max = BUTTON_8) = {
            #[packed_bits 8] #[item_settings data,variable,absolute] buttons0=input;
        };
        (usage_page = BUTTON, usage_min = 9, usage_max = 10) = {
            #[packed_bits 2] #[item_settings data,variable,absolute] buttons1=input;
        };
    }
)]
pub struct InputReport {
    pub x: u8,
    pub y: u8,
    pub buttons0: u8,
    pub buttons1: u8,
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant