Comments (8)
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?
from stm32bluepill-mynewt-sensor.
More examples:
stm32bluepill-mynewt-sensor/src/send_coap.rs
Lines 34 to 37 in f51e078
(why is that one declared as extern
although it's not pub
?)
and finally you can convert the integer error codes to an enum
stm32bluepill-mynewt-sensor/src/sensor.rs
Lines 123 to 139 in f51e078
from stm32bluepill-mynewt-sensor.
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 GpioConfig
returns another GpioConfig
object. It's like a chainable API.
Wonder if you have any thoughts on this approach?
from stm32bluepill-mynewt-sensor.
Also what do you think about error-chain
?
http://brson.github.io/2016/11/30/starting-with-error-chain
from stm32bluepill-mynewt-sensor.
Here's different pattern for returning results
No, that's not returning a Result
(as you can see by the return type). It's just returning a struct.
Also what do you think about
error-chain
?
IIRC error-chain
is no longer recommended for new designs. You can of course roll your own Error enum, which should be no problem at all. You don't need a framework. You don't get any real benefits from it when working on an embedded platform.
from stm32bluepill-mynewt-sensor.
I hope you are compiling with --release
.
expect("")
should be expect("start_network_task failed")
or similar.
https://docs.rust-embedded.org/book/start/panicking.html could help
from stm32bluepill-mynewt-sensor.
Hmmm wonder why Result
and expect()
would cause this bloat compared with returning ints. Aren't they functionally equivalent?
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! :-)
from stm32bluepill-mynewt-sensor.
Personally, I would not avoid using a Result
even if that means, that by binary increases by a few kb.
warning: unused
std::result::Result
that must be used
That's the most amazing thing! If you forget to use the Result
, the compiler will tell you. When you return an int, you don't need to handle it and you can easily swallow them. Also it doesn't tell you right away, what happened. It's just a convention that 0 means OK and any other values means Error. With Result
you get it with the type system. Amazing!
from stm32bluepill-mynewt-sensor.
Related Issues (1)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stm32bluepill-mynewt-sensor.