Skip to content

API incompatible with Rust discouraging references to static mut #49

@antonok-edm

Description

@antonok-edm

As of rust-lang/rust#114447, it appears that the common pattern of

static mut EP_MEMORY: [u32; 1024] = [0; 1024];
let usb_bus = UsbBus::new(usb, unsafe { &mut EP_MEMORY });

is no longer encouraged, and will stop working in the 2024 edition.

warning: creating a mutable reference to mutable static is discouraged
    |
231 |         let usb_bus = UsbBus::new(usb, unsafe { &mut EP_MEMORY });
    |                                                 ^^^^^^^^^^^^^^ mutable reference to mutable static
    |
        = note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
    = note: this will be a hard error in the 2024 edition
    = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior
help: use `addr_of_mut!` instead to create a raw pointer
    |
231 |         unsafe { USB_ALLOCATOR = Some(UsbBus::new(usb, addr_of_mut!(EP_MEMORY))) };
    |                                                        ~~~~~~~~~~~~~~~~~~~~~~~

The examples still use this outdated pattern.

My understanding is that the "preferred" solution is to make UsbBus::new take a pointer rather than a reference, although I'm not 100% sure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions