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

small usability issue with esp_hal::init() #2086

Closed
liebman opened this issue Sep 4, 2024 · 9 comments
Closed

small usability issue with esp_hal::init() #2086

liebman opened this issue Sep 4, 2024 · 9 comments

Comments

@liebman
Copy link
Contributor

liebman commented Sep 4, 2024

If you want to set the clock you can't do:

let peripherals = esp_hal::init(esp_hal::Config {
    cpu_clock: CpuClock::max(),
);

because it's marked with #[non_exhaustive] one must go this route:

let mut config = esp_hal::Config::default();
config.cpu_clock = CpuClock::max();
let peripherals = esp_hal::init(config);
@bugadani
Copy link
Contributor

bugadani commented Sep 4, 2024

This is completely intentional. If we're going to introduce new options, we don't want to break existing code.

The style I prefer is:

let peripherals = esp_hal::init({
    let mut config = esp_hal::Config::default();
    config.cpu_clock = CpuClock::max();
    config
});

Or I think you should be able to do something like:

let peripherals = esp_hal::init(esp_hal::Config {
    cpu_clock: CpuClock::max(),
    ..Default::default()
});

@liebman
Copy link
Contributor Author

liebman commented Sep 4, 2024

or something like this maybe?

let peripherals = esp_hal::init(esp_hal::Config::default()
    .with_cpu_clock(CpuClock::max()));
});

The rust error told you to "look for a new() method" or something like that.

@liebman
Copy link
Contributor Author

liebman commented Sep 4, 2024

let peripherals = esp_hal::init(esp_hal::Config {
    cpu_clock: CpuClock::max(),
    ..Default::default()
});

Of the two you posed I prefer ^^

@bugadani
Copy link
Contributor

bugadani commented Sep 4, 2024

Builders are fine I guess, but unless there's extreme interest for them, I'd love to avoid adding more code for a single use struct :(

@bjoernQ
Copy link
Contributor

bjoernQ commented Sep 5, 2024

IMHO the ..Default::default() way is the most idiomatic and also convenient enough for now

@MabezDev
Copy link
Member

MabezDev commented Sep 5, 2024

As @bugadani mentioned, this is by design so we can add fields and not break code in the future. Imo ..Default::default() is nice enough for now, we can revisit this at a later date if the number of fields/options do get unwieldy.

@MabezDev MabezDev closed this as completed Sep 5, 2024
@liebman
Copy link
Contributor Author

liebman commented Sep 5, 2024

That does not seem to work:

error[E0639]: cannot create non-exhaustive struct using struct expression
  --> board/src/board.rs:52:41
   |
52 |           let peripherals = esp_hal::init(esp_hal::Config {
   |  _________________________________________^
53 | |             cpu_clock: CpuClock::max(),
54 | |             ..Default::default()
55 | |         });
   | |_________^

For more information about this error, try `rustc --explain E0639`.
error: could not compile `board` (lib) due to 1 previous error

@MabezDev
Copy link
Member

MabezDev commented Sep 5, 2024

Ah, you're right. I'm hoping it's something that will be allowed in the future. It currently only works on structs defined within the crate, see: https://rust-lang.github.io/rfcs/2008-non-exhaustive.html#functional-record-updates.

In which case, @bugadani's first option works and it's not too much more code. I personally don't see the advantage of a builder pattern, but I'm open to hear other options.

@bugadani
Copy link
Contributor

bugadani commented Sep 5, 2024

That's sad, but hopefully it's still not the end of the world. Again, we have to balance stability, lines of code (yes, really!) and useability and I think we're in a local maximum of the 3.

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

No branches or pull requests

4 participants