Skip to content
This repository was archived by the owner on Oct 9, 2018. It is now read-only.

Commit 329085c

Browse files
committed
Updates to modules section
1 parent d978b0a commit 329085c

File tree

1 file changed

+46
-45
lines changed

1 file changed

+46
-45
lines changed

features/modules.md

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,45 @@
55
> We should discuss visibility, nesting, `mod.rs`, and any interesting patterns
66
> around modules.
77
8-
#### Naming conventions
9-
> **[OPEN]**
10-
> - Anything else?
11-
> - Are there cases where *not* separating words with underscores is OK,
12-
> or should this be a hard rule?
13-
14-
- Module names should contain only lowercae letters and underscores.
15-
For example, use `std::io::timer`, not `Std::IO::Timer`.
16-
- Multiple words should be separated by underscores.
17-
Use `std::local_data`, not `std::localData` or `std::localdata`.
18-
19-
#### Headers
20-
> **[OPEN]** Is this header organization suggestion valid?
8+
### Headers [RFC]
219

2210
Organize module headers as follows:
2311
1. [Imports](../style/imports.md).
2412
1. `mod` declarations.
2513
1. `pub mod` declarations.
2614

27-
#### Avoid `path` directives
28-
> **[OPEN]** This is hardly ever seen in the Rust codebase (only 4 uses, all in
29-
> `libsyntax`) and seems like overall a bad idea.
15+
### Avoid `path` directives. [RFC]
3016

31-
Avoid using `#[path="..."]` directives except where it is *absolutely*
32-
necessary.
17+
Avoid using `#[path="..."]` directives; make the file system and
18+
module hierarchy match, instead.
3319

34-
### Use the module hirearchy to organize APIs into coherent sections
35-
> **[OPEN]**
20+
### Use the module hirearchy to organize APIs into coherent sections. [FIXME]
3621

37-
The module hirearchy defines both the public and internal API of your module.
38-
Breaking related functionality into submodules makes it understandable to both
39-
users and contributors to the module.
22+
> **[FIXME]** Flesh this out with examples; explain what a "coherent
23+
> section" is with examples.
24+
>
25+
> The module hirearchy defines both the public and internal API of your module.
26+
> Breaking related functionality into submodules makes it understandable to both
27+
> users and contributors to the module.
28+
29+
### Place modules in their own file. [RFC]
4030

41-
#### Place modules in separate files
4231
> **[OPEN]**
43-
> - "<100 lines" is completely arbitrary, but it's a clearer recommendation
32+
> - "<100 lines" is arbitrary, but it's a clearer recommendation
4433
> than "~1 page" or similar suggestions that vary by screen size, etc.
4534
4635
For all except very short modules (<100 lines) and [tests](../testing/README.md),
47-
place the module `foo` in a separate file: either `foo.rs` or `foo/mod.rs`,
48-
depending on your needs, rather than declaring it inline like
36+
place the module `foo` in a separate file, as in:
37+
38+
```rust
39+
pub mod foo;
40+
41+
// in foo.rs or foo/mod.rs
42+
pub fn bar() { println!("..."); }
43+
/* ... */
44+
```
45+
46+
rather than declaring it inline:
4947

5048
```rust
5149
pub mod foo {
@@ -54,11 +52,10 @@ pub mod foo {
5452
}
5553
```
5654

57-
#### Use folders to organize submodules
58-
> **[OPEN]**
55+
#### Use subdirectories for modules with children. [RFC]
5956

6057
For modules that themselves have submodules, place the module in a separate
61-
folder (e.g., `bar/mod.rs` for a module `bar`) rather than the same directory.
58+
directory (e.g., `bar/mod.rs` for a module `bar`) rather than the same directory.
6259

6360
Note the structure of
6461
[`std::io`](http://doc.rust-lang.org/std/io/). Many of the submodules lack
@@ -68,7 +65,7 @@ and
6865
[`io::stdio`](http://doc.rust-lang.org/std/io/stdio/).
6966
On the other hand,
7067
[`io::net`](http://doc.rust-lang.org/std/io/net/)
71-
contains submodules, so it lives in a separate folder:
68+
contains submodules, so it lives in a separate directory:
7269

7370
```
7471
io/mod.rs
@@ -84,33 +81,36 @@ io/mod.rs
8481
...
8582
```
8683

87-
While it is possible to define all of `io` within a single folder, mirroring
88-
the module hirearchy in the directory structure makes submodules of `io::net`
89-
easier to find.
84+
While it is possible to define all of `io` within a single directory,
85+
mirroring the module hirearchy in the directory structure makes
86+
submodules of `io::net` easier to find.
9087

91-
#### Top-level definitions
92-
> **[OPEN]**
88+
### Consider top-level definitions or reexports. [RFC]
9389

94-
Define or [reexport](http://doc.rust-lang.org/std/io/#reexports) commonly used
95-
definitions at the top level of your module.
90+
For modules with submodules,
91+
define or [reexport](http://doc.rust-lang.org/std/io/#reexports) commonly used
92+
definitions at the top level:
9693

97-
Functionality that is related to the module itself should be defined in
98-
`mod.rs`, while functionality specific to a submodule should live in its
99-
related submodule and be reexported elsewhere.
94+
* Functionality relevant to the module itself or to many of its
95+
children should be defined in `mod.rs`.
96+
* Functionality specific to a submodule should live in that
97+
submodule. Reexport at the top level for the most important or
98+
common definitions.
10099

101100
For example,
102101
[`IoError`](http://doc.rust-lang.org/std/io/struct.IoError.html)
103-
is defined in `io/mod.rs`, since it pertains to the entirety of the submodule,
102+
is defined in `io/mod.rs`, since it pertains to the entirety of `io`,
104103
while
105104
[`TcpStream`](http://doc.rust-lang.org/std/io/net/tcp/struct.TcpStream.html)
106105
is defined in `io/net/tcp.rs` and reexported in the `io` module.
107106

108-
### Use internal module hirearchies for hiding implementations
107+
### Use internal module hirearchies for organization. [RFC]
108+
109109
> **[OPEN]**
110110
> - Referencing internal modules from the standard library is subject to
111111
> becoming outdated.
112112
113-
Internal module hirearchies (including private submodules) may be used to
113+
Internal module hirearchies (i.e., private submodules) may be used to
114114
hide implementation details that are not part of the module's API.
115115

116116
For example, in [`std::io`](http://doc.rust-lang.org/std/io/), `mod mem`
@@ -129,4 +129,5 @@ mod mem;
129129
```
130130

131131
This hides the detail that there even exists a `mod mem` in `io`, and
132-
helps keep code organized while offering freedom to change the implementation.
132+
helps keep code organized while offering freedom to change the
133+
implementation.

0 commit comments

Comments
 (0)