flipperzero icon indicating copy to clipboard operation
flipperzero copied to clipboard

Update to Rust Edition 2024

Open loftyinclination opened this issue 10 months ago • 2 comments

This is landing soon, and it'd maybe be nice to update. Not sure what effect updating would have on the msrv, but the 2024 edition does come with a few nice features for embedded code and FFI (in particular, declaring functions in unsafe extern blocks as safe).

I've tried doing this update locally and found a few minor issues that were easily fixed;

Would be interested to hear your thoughts on updating to this new edition. I've got a local branch that I could raise a draft PR for if that would be helpful.

loftyinclination avatar Jan 25 '25 12:01 loftyinclination

Not sure if this is a local issue, but after changing edition to 2024, a number of the string conversion functions (panic, logging, and some furi string methods) fail to compile due to expecting a u8 pointer but instead receiving i8 pointers.

error[E0308]: mismatched types
    --> flipperzero/examples/i2c-ds3231.rs:79:9
     |
79   |         error!("DS3231 is not connected and ready");
     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     |         |
     |         expected `*const u8`, found `*const i8`
     |         arguments to this function are incorrect
     |
     = note: expected raw pointer `*const u8`
                found raw pointer `*const i8`
note: function defined here
    --> /home/lofty/Documents/Creative/Programming/Rust/flipper-zero-api/crates/sys/src/bindings.rs:1968:12
     |
1968 |     pub fn furi_log_print_format(
     |            ^^^^^^^^^^^^^^^^^^^^^
     = note: this error originates in the macro `$crate::log` which comes from the expansion of the macro `error` (in Nightly builds, run with -Z macro-backtrace for more info)

this seems to be caused by the return value of CStr::as_ptr, core::ffi::c_char being platform specific. switching to use *const c_char instead of *const i8 or *const u8 in the logging methods seems to fix this.

loftyinclination avatar Jan 25 '25 14:01 loftyinclination

Hi @loftyinclination,

Thanks for opening this issue, so we can track what would be needed to switch to 2024 edition.

As you point out, it looks like the currently generated aren't compatible with the new edition. These are generated by bindgen, so we probably need to first wait for bindgen support for the 2024 edition.

  • https://github.com/rust-lang/rust-bindgen/issues/2901
  • https://github.com/rust-lang/rust-bindgen/issues/3093

It looks like the unsafe attributes are supported in all editions of Rust from 1.82.0 forward. We currently have the Rust version set to 1.81.0, but I have no problem with us updating that to a newer version (and more recent nightly than 2024-09-10).

I think a good first step would be to enable the rust-2024-compatibility lint group which would allow tracking down the various compatibility issues.

[lints.rust]
rust_2024_compatibility = "warn"

We probably won't get them fixed before I cut the 0.14.0 release, but I think it would be a good thing to work on once that's out the door.

dcoles avatar Jan 26 '25 01:01 dcoles