5
5
> We should discuss visibility, nesting, ` mod.rs ` , and any interesting patterns
6
6
> around modules.
7
7
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]
21
9
22
10
Organize module headers as follows:
23
11
1 . [ Imports] ( ../style/imports.md ) .
24
12
1 . ` mod ` declarations.
25
13
1 . ` pub mod ` declarations.
26
14
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]
30
16
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 .
33
19
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]
36
21
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]
40
30
41
- #### Place modules in separate files
42
31
> ** [ OPEN] **
43
- > - "<100 lines" is completely arbitrary, but it's a clearer recommendation
32
+ > - "<100 lines" is arbitrary, but it's a clearer recommendation
44
33
> than "~ 1 page" or similar suggestions that vary by screen size, etc.
45
34
46
35
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:
49
47
50
48
``` rust
51
49
pub mod foo {
@@ -54,11 +52,10 @@ pub mod foo {
54
52
}
55
53
```
56
54
57
- #### Use folders to organize submodules
58
- > ** [ OPEN] **
55
+ #### Use subdirectories for modules with children. [ RFC]
59
56
60
57
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.
62
59
63
60
Note the structure of
64
61
[ ` std::io ` ] ( http://doc.rust-lang.org/std/io/ ) . Many of the submodules lack
68
65
[ ` io::stdio ` ] ( http://doc.rust-lang.org/std/io/stdio/ ) .
69
66
On the other hand,
70
67
[ ` 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 :
72
69
73
70
```
74
71
io/mod.rs
@@ -84,33 +81,36 @@ io/mod.rs
84
81
...
85
82
```
86
83
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.
90
87
91
- #### Top-level definitions
92
- > ** [ OPEN] **
88
+ ### Consider top-level definitions or reexports. [ RFC]
93
89
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:
96
93
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.
100
99
101
100
For example,
102
101
[ ` 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 ` ,
104
103
while
105
104
[ ` TcpStream ` ] ( http://doc.rust-lang.org/std/io/net/tcp/struct.TcpStream.html )
106
105
is defined in ` io/net/tcp.rs ` and reexported in the ` io ` module.
107
106
108
- ### Use internal module hirearchies for hiding implementations
107
+ ### Use internal module hirearchies for organization. [ RFC]
108
+
109
109
> ** [ OPEN] **
110
110
> - Referencing internal modules from the standard library is subject to
111
111
> becoming outdated.
112
112
113
- Internal module hirearchies (including private submodules) may be used to
113
+ Internal module hirearchies (i.e., private submodules) may be used to
114
114
hide implementation details that are not part of the module's API.
115
115
116
116
For example, in [ ` std::io ` ] ( http://doc.rust-lang.org/std/io/ ) , ` mod mem `
@@ -129,4 +129,5 @@ mod mem;
129
129
```
130
130
131
131
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