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

Commit 0925e0d

Browse files
committed
Revamp naming section, resolving many of the open naming questions
1 parent 3e98b5d commit 0925e0d

File tree

7 files changed

+238
-146
lines changed

7 files changed

+238
-146
lines changed

SUMMARY.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
* [Whitespace](style/whitespace.md)
55
* [Comments](style/comments.md)
66
* [Braces, semicolons, commas](style/braces.md)
7-
* [Naming](style/naming.md)
7+
* [Naming](style/naming/README.md)
8+
* [Ownership variants](style/naming/ownership.md)
9+
* [Containers/wrappers](style/naming/containers.md)
10+
* [Conversions](style/naming/conversions.md)
11+
* [Iterators](style/naming/iterators.md)
812
* [Imports](style/imports.md)
913
* [Organization](style/organization.md)
1014
* [Guidelines by Rust feature](features/README.md)

style/naming.md

Lines changed: 0 additions & 145 deletions
This file was deleted.

style/naming/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
% Naming conventions
2+
3+
### General conventions
4+
5+
| Item | Convention |
6+
| ---- | ---------- |
7+
| Crates | `snake_case` (but prefer single word) |
8+
| Modules | `snake_case` |
9+
| Types | `CamelCase` |
10+
| Traits | `CamelCase`; prefer transitive verbs, nouns, and then adjectives; avoid suffixes (like `able`) |
11+
| Enum variants | `CamelCase` |
12+
| Functions | `snake_case` |
13+
| Conversions | `as_foo`/`to_foo`/`into_foo` (see [subsection](containers.md)) |
14+
| Methods | `snake_case` |
15+
| General constructors | `new` or `new_with_more_details` |
16+
| Conversion constructors | `from_some_other_type` |
17+
| Local variables | `snake_case` |
18+
| Static variables | `SCREAMING_SNAKE_CASE` |
19+
| Type parameters | single uppercase letter: `T` |
20+
| Lifetimes | short, lowercase: `'a` |
21+
22+
<p>
23+
In `CamelCase`, acronyms count as one word: use `Uuid` rather than `UUID`.
24+
25+
### Referring to types in function/method names [OPEN]
26+
27+
<!-- `&T` -> `ref` -->
28+
<!-- `&mut T` -> `mut` -->
29+
30+
> **[OPEN]** We should establish general conventions for type names
31+
> when they appear as part of functions/methods. For example, that
32+
> `&[U]` is generally called `slice`.
33+
34+
### Avoid redundant prefixes [RFC]
35+
36+
Names of items within a module should not be prefixed with that module's name,
37+
since clients can always reference the item qualified by the module name.
38+
39+
Prefer
40+
41+
``` rust
42+
mod foo {
43+
pub struct Bar { ... }
44+
}
45+
```
46+
47+
over
48+
49+
``` rust
50+
mod foo {
51+
pub struct FooBar { ... }
52+
}
53+
```
54+
55+
56+
### Fallible functions [OPEN]
57+
58+
> **[OPEN]** Should we have a standard marker for functions that can
59+
> cause task failure?
60+
61+
> See https://github.com/mozilla/rust/issues/13159
62+
63+
### Getter/setter methods [OPEN]
64+
65+
> **[OPEN]** Need a naming and signature convention here.
66+
67+
### Escape hatches [OPEN]
68+
69+
> **[OPEN]** Should we standardize a convention for functions that may break API
70+
> guarantees? e.g. `ToCStr::to_c_str_unchecked`
71+
72+
### Predicates
73+
74+
* Simple boolean predicates should be prefixed with `is_` or another
75+
short question word, e.g., `is_empty`.
76+
* Common exceptions: `lt`, `gt`, and other established predicate names.

style/naming/containers.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
% Common container/wrapper methods
2+
3+
Containers, wrappers, and cells all provide ways to access the data
4+
they enclose. Accessor methods often have variants to access the data
5+
by value, by reference, and by mutable reference.
6+
7+
In general, the `get` family of methods is used to access contained
8+
data without any risk of task failure; they return `Option` as
9+
appropriate. This name is chosen rather than names like `find` or
10+
`lookup` because it is appropriate for a wider range of container types.
11+
12+
#### Containers
13+
14+
For a container with keys/indexes of type `K` and elements of type `V`:
15+
16+
```rust
17+
// Look up element without failing
18+
fn get(&self, key: K) -> Option<&V>
19+
fn get_mut(&mut self, key: K) -> Option<&mut V>
20+
21+
// Convenience for .get(key).map(|elt| elt.clone())
22+
fn get_clone(&self, key: K) -> Option<V>
23+
24+
// Lookup element, failing if it is not found:
25+
impl Index<K, V> for Container { ... }
26+
impl IndexMut<K, V> for Container { ... }
27+
```
28+
29+
#### Wrappers/Cells
30+
31+
Prefer specific conversion functions like `as_bytes` or `into_vec` whenever
32+
possible. Otherwise, use:
33+
34+
```rust
35+
// Extract contents without failing
36+
fn get(&self) -> &V
37+
fn get_mut(&mut self) -> &mut V
38+
fn unwrap(self) -> V
39+
```
40+
41+
#### Wrappers/Cells around `Copy` data
42+
43+
```rust
44+
// Extract contents without failing
45+
fn get(&self) -> V
46+
```
47+
48+
#### `Option`-like types
49+
50+
Finally, we have the cases of types like `Option` and `Result`, which
51+
play a special role for failure.
52+
53+
For `Option<V>`:
54+
55+
```rust
56+
// Extract contents or fail if not available
57+
fn assert(self) -> V
58+
fn expect(self, &str) -> V
59+
```
60+
61+
For `Result<V, E>`:
62+
63+
```rust
64+
// Extract the contents of Ok variant; fail if Err
65+
fn assert(self) -> V
66+
67+
// Extract the contents of Err variant; fail if Ok
68+
fn assert_err(self) -> E
69+
```

style/naming/conversions.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
% Conversions
2+
3+
> **[OPEN]** Should we provide standard traits for conversions? Doing
4+
> so nicely will require
5+
> [trait reform](https://github.com/rust-lang/rfcs/pull/48) to land.
6+
7+
Conversions should be provided as methods, with names prefixed as follows:
8+
9+
| Prefix | Cost | Consumes convertee |
10+
| ------ | ---- | ------------------ |
11+
| `as_` | Free | No |
12+
| `to_` | Expensive | No |
13+
| `into_` | Variable | Yes |
14+
15+
<p>
16+
For example:
17+
18+
* `as_bytes()` gives a `&[u8]` view into a `&str`, which is a no-op.
19+
* `to_owned()` copies a `&str` to a new `String`.
20+
* `into_bytes()` consumes a `String` and yields the underlying
21+
`Vec<u8>`, which is a no-op.
22+
23+
Conversions prefixed `as_` and `into_` typically _decrease abstraction_, either
24+
exposing a view into the underlying representation (`as`) or deconstructing data
25+
into its underlying representation (`into`). Conversions prefixed `to_`, on the
26+
other hand, typically stay at the same level of abstraction but do some work to
27+
change one representation into another.
28+
29+
> **[OPEN]** The distinctions between conversion methods does not work
30+
> so well for `from_` conversion constructors. Is that a problem?

style/naming/iterators.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
% Iterators
2+
3+
#### Method names
4+
5+
For a container with elements of type `U`, iterator methods should be named:
6+
7+
```rust
8+
fn iter(&self) -> T // where T implements Iterator<&U>
9+
fn iter_mut(&mut self) -> T // where T implements Iterator<&mut U>
10+
fn iter_owned(self) -> T // where T implements Iterator<U>
11+
```
12+
13+
The default iterator variant yields shared references `&U`.
14+
15+
#### Type names
16+
17+
Iterators require introducing and exporting new types. These types should use
18+
the following naming convention:
19+
20+
* **Base name**. If the iterator yields something that can be described with a
21+
specific noun, the base name should be the pluralization of that noun
22+
(e.g. an iterator yielding words is called `Words`). Generic contains use the
23+
base name `Items`.
24+
25+
* **Flavor prefix**. Iterators often come in multiple flavors, with the default
26+
flavor providing immutable references. Other flavors should prefix their name:
27+
28+
* Moving iterators have a prefix of `Move`.
29+
* If the default iterator yields an immutable reference, an iterator
30+
yielding a mutable reference has a prefix `Mut`.
31+
* Reverse iterators have a prefix of `Rev`.

style/naming/ownership.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
% Ownership variants
2+
3+
Functions often come in multiple variants: immutably borrowed, mutably
4+
borrowed, and owned.
5+
6+
The right default depends on the function in question. Variants should
7+
be marked through suffixes.
8+
9+
#### Immutably borrowed by default
10+
11+
If `foo` uses/produces an immutable borrow by default, use:
12+
13+
* The `_mut` suffix (e.g. `foo_mut`) for the mutably borrowed variant.
14+
* The `_owned` suffix (e.g. `foo_owned`) for the owned variant.
15+
16+
#### Owned by default
17+
18+
If `foo` uses/produces owned data by default, use:
19+
20+
* The `_ref` suffix (e.g. `foo_ref`) for the immutably borrowed variant.
21+
* The `_mut` suffix (e.g. `foo_mut`) for the mutably borrowed variant.
22+
23+
#### Exceptions
24+
25+
For mutably borrowed variants, if the `mut` qualifier is part of a
26+
type name (e.g. `as_mut_slice`), it should appear as it would appear
27+
in the type.

0 commit comments

Comments
 (0)