You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/0000-packages-as-optional-namespaces.md
+21-7Lines changed: 21 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,23 +26,37 @@ The motivation here is distinct from the general problem of squatting -- with ge
26
26
27
27
# Guide-level explanation
28
28
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.
30
30
31
-
The crate can be imported in Cargo.toml using its name as normal:
31
+
Users import these crates in Cargo.toml as normal:
32
32
33
33
```toml
34
34
[dependencies]
35
-
"foo::bar" = "1.0"
35
+
"foo" = "1.0.42"
36
+
"foo::bar" = "3.1"
36
37
```
37
38
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:
40
40
41
41
```rs
42
-
usefoo::bar::Baz;
42
+
letbaz=foo::bar::Baz::new();
43
+
foo::render(baz);
43
44
```
44
45
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.
0 commit comments