Skip to content

Commit 4501047

Browse files
authored
Merge pull request #4 from epage/guide
Expand on the guide-level explanation
2 parents 42046f8 + 8e667ce commit 4501047

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

text/0000-packages-as-optional-namespaces.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,37 @@ The motivation here is distinct from the general problem of squatting -- with ge
2626

2727
# Guide-level explanation
2828

29-
If you own a crate `foo`, you may create a crate namespaced under it as `foo::bar`. Only people who are owners of `foo` may _create_ a crate `foo::bar` (and all owners of `foo` are implicitly owners of `foo/bar`). After such a crate is created, additional per-crate publishers may be added who will be able to publish subsequent versions as usual.
29+
The owners of the `foo` crate may provide other crates under the `foo` namespace, like `foo::bar`. For users, this makes its official status clearer and makes it easier to discover.
3030

31-
The crate can be imported in Cargo.toml using its name as normal:
31+
Users import these crates in Cargo.toml as normal:
3232

3333
```toml
3434
[dependencies]
35-
"foo::bar" = "1.0"
35+
"foo" = "1.0.42"
36+
"foo::bar" = "3.1"
3637
```
3738

38-
39-
In Rust code, the colons still work:
39+
They will then access this through a facade made of `foo` and all `foo::*` crates, for example:
4040

4141
```rs
42-
use foo::bar::Baz;
42+
let baz = foo::bar::Baz::new();
43+
foo::render(baz);
4344
```
4445

45-
In case there is also an in-scope crate `foo` with an exported `bar` item, this will cause an ambiguity error unless both the item `foo::bar` and the crate `foo::bar` are actually resolving to the same item. This is similar to what rustc already does when it encounters in-use clashes in glob imports.
46+
Some reasons for `foo`s owner to consider using namespaces:
47+
- Avoid name conflicts with third-party authors (since they are reserved)
48+
- Improve discoverability of official crates
49+
- As an alternative to feature flags for optional subsystems
50+
- When different parts of your API might have different compatibility guarantees
51+
52+
When considering this, keep in mind:
53+
- Does it makes sense for this new crate to be presented in the `foo` facade?
54+
- How likely is a crate to move into or out of the namespace?
55+
- Moving the crate in or out of a namespace is a breaking change though it can be worked around by having the old crate re-export the new crate but that does add extra friction to the process.
56+
- There is not currently a mechanism to raise awareness with users that a crate has migrated into or out of a namespace and you might end up leaving users behind.
57+
- If users import both `foo` and `foo::bar` but `foo` also has a `bar` item in its API that isn't just `foo::bar` re-exported, then rustc will error.
58+
59+
Only the owners of `foo` may _create_ the `foo::bar` crate (and all owners of `foo` are implicitly owners of `foo/bar`). After the `foo::bar` crate is created, additional per-crate publishers may be added who will be able to publish subsequent versions as usual.
4660

4761
# Reference-level explanation
4862

0 commit comments

Comments
 (0)