A feature is an optional part of a package: JSON support, TLS, a faster
path that needs another library. A package declares its features in
atoll.toml, each build turns some of them on, and @cfg compiles code only
when the features it names are on.
Declaring features
Features go under [features]. Each one lists what it turns on:
[features]
default = ["json"] # on unless something says otherwise
json = [] # turns on nothing else; it only gates code
tls = ["dep:acme.dev/crypto"] # turns on an optional dependency
compress = ["acme.dev/http#gzip"] # turns on a dependency's feature
[dependencies]
"acme.dev/crypto" = { version = "^1", optional = true }
"acme.dev/http" = { version = "^2", default-features = false, features = ["h2"] }An entry in a feature’s list is another feature of the package,
dep:<module> for an optional dependency, <module>#<feature> for a
dependency’s feature, or <module>?#<feature> for a dependency’s feature
that applies only when that dependency is on for some other reason. A feature
name is lowercase letters, digits, - and _, starting with a letter.
An optional dependency is left out of the build until a feature that is on turns it on. Importing it while it is off is error ATOLL1083, which names the feature that would turn it on.
Which features are on
The package being built has its default features on. The command line
changes that:
atoll build --features json,tls # these as well as the defaults
atoll build --features acme.dev/http#gzip # a dependency's feature
atoll build --no-default-features # not the defaults
atoll build --all-features # every feature of the packageA dependency’s features are the ones its entry asks for (features = [...]),
plus its default unless the entry says default-features = false, plus any
that a feature turned on in another package asks for.
A package that several others depend on gets one set of features: every feature any of them asks for, the way it gets one version. So features should only ever add. Turning one on must not break code that did not ask for it, because some other package in the build may turn it on.
atoll tree shows the features each package has on, and atoll why <module>#<feature> says who turned one on.
Compiling code under a feature
@cfg compiles a declaration only when its condition holds:
@cfg(feature("json"))
import acme.dev/json
@cfg(feature("json"))
pub fn encode(n int) string {
return json.number(n)
}
@cfg(not(feature("json")))
pub fn encode(n int) string {
return ""
}
@cfg(feature("json"))
pub fn to_json(n int) string {
return json.number(n)
}That is acme.dev/kit, whose manifest declares default = ["json"],
json = ["dep:acme.dev/json"] and fast = []; the project is in the repository
at book/projects/kit. It checks with and without its features:
$ atoll check kit
OK (0 diagnostics)
$ atoll check --no-default-features kit
OK (0 diagnostics)
$ atoll check --all-features kit
OK (0 diagnostics)A condition is feature("name"), not(…), all(…, …) or any(…, …). A
@cfg can go on a declaration, an impl block, a field, a method, an enum
variant, or an import. It cannot go on a parameter or a statement; write two
functions instead.
What a build leaves out is not checked by that build, so atoll check --all-features is how to check all of it. The mistakes are errors rather than
silent:
| Mistake | Error |
|---|---|
| a feature the package does not declare, such as a misspelling | ATOLL1084, naming the features it does declare |
a @cfg that is not a condition, or one on a parameter or statement |
ATOLL1085 |
| using something a build leaves out | ATOLL1086, naming the feature and where to turn it on |
to_json is declared only under @cfg(feature("json")). Called from a package
that depends on acme.dev/kit with default-features = false, so without json,
it reads:
import acme.dev/kit
fn main() int {
println(kit.to_json(42))
return 0
}error[ATOLL1086]: `to_json` is compiled only with feature `json` of `acme.dev/kit`, which is off in this build at main.at:4:13
help: turn it on where you depend on `acme.dev/kit`: `features = ["json"]` in atoll.toml
1 error(s) foundThe editor checks each package with the features a plain atoll check would
turn on, and greys out the code those features leave out.
Adding a dependency with features
atoll add writes the feature keys of a dependency entry:
atoll add acme.dev/http ^2 --features h2,gzip --no-default-features
atoll add acme.dev/crypto ^1 --optionalAn optional dependency needs a feature that turns it on, so --optional also
adds one named after the module: crypto = ["dep:acme.dev/crypto"].
Features and versions
atoll api shows each item with the condition it is compiled under, and lists
the package’s features and defaults:
@cfg(feature("json"))
import acme.dev/json
@cfg(feature("json"))
pub fn encode(n int) string {
return json.number(n)
}
@cfg(not(feature("json")))
pub fn encode(n int) string {
return ""
}$ cd kit
$ atoll api
…
package acme.dev/kit
features fast, json
default json
module acme.dev/kit
@cfg(feature("json"))
pub fn encode(n int) string
@cfg(not(feature("json")))
pub fn encode(n int) stringatoll api diff treats adding a feature, or putting one
into default, as a minor change. Removing a feature, taking one out of
default, or putting an existing item under a feature is a major change,
because a package that used it may no longer build.
The lock records the graph with every feature on, so building with other
features never changes atoll.lock.