-
Notifications
You must be signed in to change notification settings - Fork 263
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
Avoid unwrap in examples #3050
Avoid unwrap in examples #3050
Conversation
I thought we said not to do this, because user code can't really use |
Was that the reason? I thought we wanted to avoid the effort to do it - in any case teaching users to always unwrap isn't a good thing |
Well I'm just repeating myself because nobody else really stated an opinion on #2811 (except Kirill, but his opinion may have been somewhat derailed by mine). The users can't really do anything else, but handle the error or unwrap (or ignore). They'll copy the I agree with some parts of the PR, where you handle errors instead of unwraps, but I think we shouldn't just search-and-replace unwraps with |
Maybe I missed that discussion - I don't care much about this PR so whatever |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should change our stance here, and accept this PR.
Whilst ?
into main probably isn't that feasible for no_std users, the can still use the ?
operator in library code. Plus they can just trampoline to main2() -> Result<(), /* Type */>
where type could be a giga enum of all errors or Box<dyn Error>
and handle the error right at the top.
Overall the documentation looks cleaner too, unwrap
s are quite noisy.
esp-hal/src/usb_serial_jtag.rs
Outdated
//! ) | ||
//! .ok(); | ||
//! if let Some(serial) = USB_SERIAL.borrow_ref_mut(cs).as_mut() { | ||
//! writeln!(serial, "Hello world!").ok(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
//! writeln!(serial, "Hello world!").ok(); | |
//! writeln!(serial, "Hello world!")?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There a few of these in this file, could you resolve them all?
I think I will remain in opposition still, |
Not sure I follow this? What's there to miss 😅? They'll have to handle the error some how, and the compiler will complain at them if they don't. Plus the docs display the signature.
I do some what agree with this, but I'm still not convinced we shouldn't accept this PR. |
If we want to build APIs for users who don't understand Rust's error handling, I guess we need to change a bit more |
This is especially true in library code. If I'm at the point in development where I'm copying examples from the Rust doc,
I agree with Daniel that it's hard to see, more round trips with compiler. At least with For code snippets in Rust modules |
I'd say someone at that level will copy s.th. and never think about error handling if everything is "fine". If they run into compiler errors, they at least need to think about it Additionally, the more involved examples (assuming no one copies a single function call) don't show all the code (because it's distracting noise) and copying them won't get you with something working no matter if the code unwraps or not. In any case we are making a lot of assumption what users do and don't do, what they know and don't know. Luckily, this (in contrast of the actual API) is something we can revisit any time. Changing all that back shouldn't be an issue if we see users are running into problems (I don't say they won't - we just don't know currently) Would be really interesting how an average (real) user journey looks like. We are assuming they read "The Book", setup things and verify everything is working fine by running "Hello World" .... experience suggest s.th. different at least for some users 😆 ... |
d3a0593
to
598bd6f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for addressing my concerns.
Imo this is an improvement, but I do understand the push back (and thanks for discussing it). Like @bjoernQ this is docs and we can change them at any time if we feel this is too confusing as a whole.
598bd6f
to
3a6e72a
Compare
Thank you for your contribution!
We appreciate the time and effort you've put into this pull request.
To help us review it efficiently, please ensure you've gone through the following checklist:
Submission Checklist 📝
cargo xtask fmt-packages
command to ensure that all changed code is formatted correctly.CHANGELOG.md
in the proper section.Extra:
Pull Request Details 📖
Description
Remove the
C-QUESTION-MARK
exception from API_GUIDELINES.md and get rid of unwraps in doc-examples.Testing
Generate docs and have a look