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

API incompatible with Rust discouraging references to static mut #49

Open
antonok-edm opened this issue May 1, 2024 · 1 comment
Open

Comments

@antonok-edm
Copy link

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.

@mvirkkunen
Copy link
Contributor

There's always ways around this, such as &mut *addr_of_mut!(EP_MEMORY) or using MaybeUninit and as_mut_ptr() to get a 'static &mut - which gets you the same safety issues if you use the reference for anything else than intended but is not considered an error for now.

But seeing how the memory is shared with a hardware peripheral, even if the "only one &mut at a time" rule was upheld from the Rust perspective it kind of goes against the philosophy to have hardware write to the buffer at the same time. And I'm not sure if the "only one &mut" rule is even completely followed currently. A pointer would probably be a better idea.

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

2 participants