Skip to content

Commit a15e953

Browse files
committed
Conventions around no_std
1 parent 0bf5281 commit a15e953

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

README.md

+51
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Guidelines use active voice.
5757
- [ ] Iterator type names match the methods that produce them ([C-ITER-TY])
5858
- [ ] Ownership suffixes use `_mut` and `_ref` ([C-OWN-SUFFIX])
5959
- [ ] Single-element containers implement appropriate getters ([C-GETTERS])
60+
- [ ] Feature names are free of placeholder words ([C-FEATURE])
6061
- **Interoperability** *(crate interacts nicely with other library functionality)*
6162
- [ ] Types eagerly implement common traits ([C-COMMON-TRAITS])
6263
- `Copy`, `Clone`, `Eq`, `PartialEq`, `Ord`, `PartialOrd`, `Hash`, `Debug`,
@@ -429,6 +430,56 @@ unsafe fn get_unchecked(&self, index) -> &V;
429430
- [`std::collections::hash_map::OccupiedEntry::get_mut`](https://doc.rust-lang.org/std/collections/hash_map/struct.OccupiedEntry.html#method.get_mut)
430431
- [`<[_]>::get_unchecked`](https://doc.rust-lang.org/std/primitive.slice.html#method.get_unchecked)
431432

433+
[C-FEATURE]: #c-feature
434+
<a id="c-feature"></a>
435+
### Feature names are free of placeholder words (C-FEATURE)
436+
437+
Do not include words in the name of a [Cargo feature] that convey zero meaning,
438+
as in `use-abc` or `with-abc`. Name the feature `abc` directly.
439+
440+
[Cargo features]: http://doc.crates.io/manifest.html#the-features-section
441+
442+
This arises most commonly for crates that have an optional dependency on the
443+
Rust standard library. The canonical way to do this correctly is:
444+
445+
```toml
446+
// In Cargo.toml
447+
448+
[features]
449+
default = ["std"]
450+
std = []
451+
```
452+
453+
```rust
454+
// In lib.rs
455+
456+
#![cfg_attr(not(feature = "std"), no_std)]
457+
```
458+
459+
Do not call the feature `use-std` or `with-std` or any creative name that is not
460+
`std`. This naming convention aligns with the naming of implicit features
461+
inferred by Cargo for optional dependencies. Consider crate `x` with optional
462+
dependencies on Serde and on the Rust standard library:
463+
464+
```toml
465+
[package]
466+
name = "x"
467+
version = "0.1.0"
468+
469+
[features]
470+
std = ["serde/std"]
471+
472+
[dependencies]
473+
serde = { version = "1.0", optional = true }
474+
```
475+
476+
When we depend on `x`, we can enable the optional Serde dependency with
477+
`features = ["serde"]`. Similarly we can enable the optional standard library
478+
dependency with `features = ["std"]`.
479+
480+
As a related note, Cargo requires that features are additive so a feature named
481+
negatively like `no-abc` is practically never correct.
482+
432483

433484
<a id="interoperability"></a>
434485
## Interoperability

0 commit comments

Comments
 (0)