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
Initial support for no_std platforms #842
Conversation
This is awesome. Thanks for working on it. A few notes:
|
Thanks, glad you like :)
Yeah, it caused a CI fail, too. Removed those changes for now.
I can try something like that. One issue I see is that some of the replacements don't come from |
What is the downside to always using core for imports, and skipping the conditionals? |
There really isn't one, but I wanted to put up the changes for discussion before just replacing. The only minor things are the extra |
I'm also not sure if core meets our MSRV, at least some parts of what we use likely aren't in it at MSRV, so we need to condition it somehow. Also agree we'd ideally not take a hashbrown dependency for std builds, though it's less critical given std's hashmap is just a frozen version of hashbrown. |
Codecov Report
@@ Coverage Diff @@
## main #842 +/- ##
==========================================
+ Coverage 90.44% 90.63% +0.19%
==========================================
Files 59 50 -9
Lines 29785 26843 -2942
==========================================
- Hits 26938 24329 -2609
+ Misses 2847 2514 -333
Continue to review full report at Codecov.
|
I'm not sure either. What is the MSRV for
Definitely, unless switching over to a full |
We support 1.30+, but for no_std its fine if we have a different MSRV if we need to. Ideally only as much as we need to, though. |
Note that we also need |
Nice! Will read through the PR and issue, thanks. core2 mentioned in rust-bitcoin/rust-bitcoin#409 looks like a good possibilty for I can play around with Edit: attempted using |
Cleaned up the conditional compilation by moving all the feature flag stuff into Let me know what you think. If you like it, I can squash it into one commit. |
Nice! Looking much better. Sorry to do this to you, I'm not actually sure we can't just "use core::*" in most places - I was checking how bitcoin_hashes did it (rust-bitcoin/bitcoin_hashes#52) and it looks like that mostly worked even with the same MSRV. If not, can we keep the same module structure (ie instead of just std::HashMap, keep it as std::collections::HashMap) then the diff size should be really trivial/more automated. |
For the stuff that has a 1-to-1 between I also did the Will look at the remaining Marked as draft for now, until the remaining |
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.
Awesome! Just skimmed the diff but this looks great. I think sync is all either Mutex (which we'll need to provide a stub for that uses RefCell under the hood) or atomic (which I think is safe in no_std).
We've bumped our MSRV so we should be able to just use alloc directly now. The rest of rust-bitcoin is likely to do the same in the coming months. |
Awesome! I've been focused on other work. Will likely be able to dedicate some time to updating this PR in the next couple weeks. |
lightning/Cargo.toml
Outdated
@@ -21,6 +21,7 @@ max_level_error = [] | |||
max_level_warn = [] | |||
max_level_info = [] | |||
max_level_debug = [] | |||
no_std = ["hashbrown"] |
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 believe that cargo features are supposed to be additive. The standard seems to be for libraries to have a feature named std
, which makes the library add std features, rather than going the other way.
If the concern is that hashbrown will be linked in unnecessarily for the std case, if there is no use
statement that actually uses it, it will get optimized out.
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.
Hmm, that's an interesting question - yes, an std feature is standard (and used in other rust-bitcoin crates), but in this case it's also "additive" to switch to hashbrown (arguably you could use it in std as well because it has a different default hasher and route finding may be faster with it). I don't really want to rely on the optimizer removing a dependency, as it's easy to link in __attribute__((constructor))
code and bypass that, through I admit it's a somewhat weaker case here given we're still recommending some users use hashbrown. Mostly rust's feature system is about 1/10th as expressive as we generally need it to be :/. I don't think this is cut-and-dry, but let me know what you think.
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.
That argument cuts both ways - if we depend on a crate for the ! no_std
case, then you'll have to rely on the optimizer to optimize it out for the no_std
case. Admittedly, there isn't such a crate yet for now...
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.
Right - because the features interface is so limited we kinda have to take things on a case-by-case basis :(.
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 had thought about this off and on over the past few weeks, and agree with @devrandom that renaming to std
is a good idea.
Will include this in the fixups when updating the branch on the latest main
.
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 wonder if it makes sense to have both a std
feature and a no_std
feature and insist that the user choose one if they turn off default
. I tried it out in rust-bitcoin/rust-bitcoin#603
The advantage is that any dependency that only applies to one of them stays optional
, which is what we want.
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.
Actually, applying to this case, an std
feature would have no consequence, so we're back to what you proposed.
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.
Coming back to this conversation, in #1028 it ends up that we do need an std
feature, to pull in std
from dependencies. So looks like we'll end up with both std
and no_std
features.
spin crate looks like a good replacement for There is also current work on a mutex trait (embedded-wg/RFC-0377) for supported embedded architectures. Maybe |
I don't think we should bother. I think we should simply not support multi-threading in a
|
This definitely makes sense for platforms like hardware wallets, and I like your approach to stubbing out the mutex functionality into a new struct. I understand not supporting multithreading for some What are your thoughts on adding another feature for multithreaded Maybe at the point the embedded-wg work is ready, all the feature-gated |
RPi and other SBC generally run Linux, so don't need no_std support?
Ah, wasn't aware of that. In any case, indeed, once upstream has support for what we want we can use that. In the mea time, I don't really think we need to bother supporting multi-core embedded hardware, its just too uncommon - mostly by the time you get to a fast enough CPU and multiple cores you are running a full OS. |
Add initial no_std support for embedded platforms
5dd70c1
to
7687a34
Compare
Cleaned up the code, and removed a lot of conditional compilation stuff. Didn't attempt the Continuing to review all the uses of the multithreading primitives to try to find a good solution that won't take a ton of effort. Maybe putting all of it in a stub crate, since there doesn't seem to be a single replacement crate that covers all of them. Will use core_io for all the |
FYI, submitted rust-bitcoin/rust-secp256k1#300 to allow use of |
You may be interested in the discussion at rust-bitcoin/bitcoin_hashes#127 which is likely to include relevant traits we can use instead of std::io (or core2 directly). |
At @devrandom's suggestion, I think we could go ahead and land the big s/std/alloc/ rename on imports if we split that out into a standalone PR, which would make this PR much smaller. |
Opened a PR that splits out Do we also want a separate PR for replacing |
Lets at least do a PR with the mass rename of |
Any interest in splitting the locking stuff out into its own PR, maybe using the suggested locking traits above? It looks like rust-bitcoin is moving pretty quick towards no_std. |
Superseded by #1028, which is now merged! |
thank you @GeneFerneau for getting this going |
First attempt to provide alternatives for
std
libraries inrust-lightning/lightning
, allowing for use onno_std
platforms.Some blockers remain for making
rust-lightning/lightning
a fullyno_std
library, likestd::sync::{Arc, Mutex}
,std::error
,std::io::*
, and more.