-
Notifications
You must be signed in to change notification settings - Fork 287
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
no_std support (using enabled-by-default "std" feature) #135
Conversation
229772f
to
dd36b02
Compare
Sorry to bring up a bikeshedding topic, but I've been looking at @alexcrichton I chose See: |
Long ago I remember we discussed creating a convention for the "enable std support" feature for the |
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 left my comments inline. I believe that conditionally defining Cursor
isn't a valid use of features. The next breaking bytes
change will remove Cursor
usage though.
Hopefully @alexcrichton can review as he is more familiar w/ no std.
serde = { version = "1.0", optional = true } | ||
|
||
[dependencies.iovec] |
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.
Ok, so this means that the vectored IO fns on the traits aren't available, which doesn't seem great (long term).
That said, they could be brought back incrementally w/o a breaking change.
Cargo.toml
Outdated
[features] | ||
default = ["use_std"] | ||
use_alloc = [] | ||
use_std = ["iovec"] |
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.
It seems that use_std
implies use_alloc
. I'm not sure if there is a way to define this in Cargo.toml.
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.
Err, other way around: use_alloc
and use_std
are orthogonal. use_alloc
is intended for no_std
use, i.e. --no-default-features --features=use_alloc
bytes builds without it per this PR, but with so many things disabled it isn't particularly useful
src/buf/into_buf.rs
Outdated
use alloc::vec::Vec; | ||
|
||
#[cfg(not(feature = "use_std"))] | ||
use buf::Cursor; |
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.
Unfortunately, given that features must be additive, I don't think this conditionally vendoring cursor is something that can be done.
This, of course, means that IntoBuf
can't really be implemented without std
:(
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.
A couple options here:
- Gate this (and anything that depends on it) on
use_alloc
- Stop using
std::io::Cursor
entirely (i.e. fixio::Cursor
is a bit out of place #75)
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.
@alexcrichton if buf::Cursor
is defined as a re-export of std::io::Cursor
when use_std
is set, is that ok for "additive" features?
Ah yeah I agree with @carllerche in that the handling of I'm personally not a huge fan of using unstable crates like |
The cleanest solution re: Regarding |
Yeah I think with |
All Personally I would much rather deal with some changes to nightly-only features of |
Oh sure that makes sense for |
Who would be trying to swap |
There's use cases for no_std on stable, nightly is not the only consumer. |
I think pretty much all practical |
Ok, well, my opinion is that this crate should not have a feature where it only uses the |
@alexcrichton would it help if the feature were more clearly labeled to indicate its experimental/nightly nature, e.g. re: feature unioning, the crate builds with all combinations of |
I would still personally prefer to not add such a feature to the crate, but I'd again defer to @carllerche's thoughts. |
I know this is mentioned in the threads that were linked above but to reiterate: I strongly prefer If some crate has an optional dependency on [package]
name = "tarcieri"
[dependencies]
bytes = { version = "0.4", optional = true } then I depend on it like this: [package]
name = "dtolnay"
[dependencies]
tarcieri = { version = "0.1", features = ["bytes"] } Why should it be any different for an optional dependency on the |
@alexcrichton perhaps it'd be better if I started with a less ambitious PR which gates all functionality which requires allocators on the We can then look at what functionality is missing from If it turns out that's a bad approach, we can circle back on handling Does that seem like a better way forward to you? |
@tarcieri seems reasonable to me! |
Okay, I've updated the PR to massively reduce the scope and feature set for Per rust-lang/api-guidelines@a15e953 this uses "std" as the feature name (cc @dtolnay) |
I moved all of the allocator-dependent gating to a separate PR: |
- Changes all references to libcore features from ::std to ::core - Feature gates anything dependent on std on the "std" feature
Sorry for the delay. At a high level, this looks good to me. This simply feature flags everything that requires std. I think @alexcrichton has some thoughts on how to organize it though to reduce the actual number of cfg lines needed... |
Definitely interested in ideas regarding how to simplify this sort of gating, as I've done in in over a dozen crates at this point, and would agree it doesn't feel particularly good doing this way. |
@@ -70,8 +70,13 @@ | |||
|
|||
#![deny(warnings, missing_docs, missing_debug_implementations)] | |||
#![doc(html_root_url = "https://docs.rs/bytes/0.4")] | |||
#![cfg_attr(not(feature = "std"), no_std)] |
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 one of the easiest things to do to ease development of no_std libraries is to unconditionally use the #![no_std]
attribute.
Only if the std
feature is enabled should this have extern crate std
, and then in that case the prelude can be imported to specific modules if necessary.
use core::sync::atomic::{self, AtomicUsize, AtomicPtr}; | ||
use core::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel}; | ||
|
||
#[cfg(feature = "std")] |
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.
Did this file need to change? It looks like the whole module is only compiled when std
is enabled?
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.
Yeah, I'm pretty sure changes to this file could be reverted.
One thing I've found useful is to have a module just called |
Sorry this has been left to stale. I will try to get back to this soon. |
What's the status of this? |
This PR, in and of itself, is pretty useless: as @alexcrichton noted in the line notes none of Even if this PR and #153 were merged (and it looks like #153 won't ever be merged) there's a bigger problem though: For those reasons I've created a mostly-compatible fork of bytes here, which vendors https://github.com/microcrates/bytes My understanding is bytes 0.5 will redo the API so as not to need |
To clarify this, I'm really only interested in the traits and their most basic methods. I'd find it more elegant if they were split, but I doubt I could convince enough people. |
This has been inactive. Unless there is interest to champion this over the finish line, it probably won't get more attention. As such, I'm going to close this. A new PR can be opened once there is time to work on this. |
@carllerche How would you feel about putting the traits into a separate |
There's an RFC to stabilize |
@Kixunil I have no problem moving the traits, but they fundamentally depend on It might be possible to make progress w/o a huge breaking change, but doing so would be pretty complicated and not something that I have time for currently. |
Yeah, fixing #75 is still a blocker on any EDIT: Actually there's some discussion on rust-lang/rfcs#2480 about moving |
::std
to::core
std
on the "std" feature