-
-
Notifications
You must be signed in to change notification settings - Fork 34
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
Proper error handling #3
Comments
More examples: stm32bluepill-mynewt-sensor/src/send_coap.rs Lines 34 to 37 in f51e078
(why is that one declared as
and finally you can convert the integer error codes to an enum stm32bluepill-mynewt-sensor/src/sensor.rs Lines 123 to 139 in f51e078
|
Hi Marcel: Thanks for the tip! The porting of the entire application from C to Rust is still in progress, once I'm done with the meaty parts (e.g. CoAP messaging), I'll come back to the return types. And write an article about this too! Here's different pattern for returning results: https://rust-embedded.github.io/book/static-guarantees/design-contracts.html impl<IN_MODE> GpioConfig<Enabled, Input, IN_MODE> {
pub fn into_input_pull_down(self) -> GpioConfig<Enabled, Input, PulledLow> {
....
// Pull down the GPIO pin
let pulled_low = input_pin.into_input_pull_down(); Any update operation on Wonder if you have any thoughts on this approach? |
Also what do you think about |
No, that's not returning a
IIRC |
Thanks Marcel, I'm testing expect() like this... pub fn start_network_task() -> Result<(), i32> { // Returns an error code upon error.
Ok(())
}
// In main()
let rc = start_network_task();
rc.expect(""); When I did this, the compiled ROM size bloated from 55 KB to 58 KB. I suspect this could be due to core::fmt, I need to check. Any idea how I can cut this bloat? |
I hope you are compiling with
https://docs.rust-embedded.org/book/start/panicking.html could help |
Hmmm wonder why That's one of the reasons I didn't include this for the article. Needs more investigation. For embedded systems, this kind of unexplained bloat could be showstoppers. Thanks for your help! :-) |
Personally, I would not avoid using a
That's the most amazing thing! If you forget to use the |
stm32bluepill-mynewt-sensor/src/send_coap.rs
Lines 19 to 23 in f51e078
Immediatly as I saw the video in the medium post I shuddered.
Do proper error handling in Rust!
Don't return
int
s to indicate errors or failures, useResult<(), ErrorType>
instead. This is just one place, but everywhere where you have those ghastlyassert!(rc == 0)
things, throw them away and burn them! Either useunwrap
, or betterexpect
.The text was updated successfully, but these errors were encountered: