Skip to content

Commit a80355d

Browse files
committed
Condense the getter naming guidelines
1 parent 0c5f968 commit a80355d

File tree

2 files changed

+47
-57
lines changed

2 files changed

+47
-57
lines changed

src/checklist.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
- [ ] Ad-hoc conversions follow `as_`, `to_`, `into_` conventions ([C-CONV])
88
- [ ] Methods on collections that produce iterators follow `iter`, `iter_mut`, `into_iter` ([C-ITER])
99
- [ ] Iterator type names match the methods that produce them ([C-ITER-TY])
10-
- [ ] Ownership suffixes use `_mut` and `_ref` ([C-OWN-SUFFIX])
11-
- [ ] Single-element containers implement appropriate getters ([C-GETTERS])
10+
- [ ] Getter names follow Rust convention ([C-GETTER])
1211
- [ ] Feature names are free of placeholder words ([C-FEATURE])
1312
- [ ] Names use a consistent word order ([C-WORD-ORDER])
1413
- **Interoperability** *(crate interacts nicely with other library functionality)*
@@ -78,8 +77,7 @@
7877
[C-CONV]: naming.html#c-naming
7978
[C-ITER]: naming.html#c-iter
8079
[C-ITER-TY]: naming.html#c-iter-ty
81-
[C-OWN-SUFFIX]: naming.html#c-own-suffix
82-
[C-GETTERS]: naming.html#c-getters
80+
[C-GETTER]: naming.html#c-getter
8381
[C-FEATURE]: naming.html#c-feature
8482
[C-WORD-ORDER]: naming.html#c-word-order
8583

src/naming.md

Lines changed: 45 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ semantics.
106106
[`GzDecoder`]: https://docs.rs/flate2/0.2.19/flate2/read/struct.GzDecoder.html#method.into_inner
107107
[`AtomicBool`]: https://doc.rust-lang.org/std/sync/atomic/struct.AtomicBool.html#method.into_inner
108108

109+
If the `mut` qualifier in the name of a conversion method constitutes part of
110+
the return type, it should appear as it would appear in the type. For example
111+
[`Vec::as_mut_slice`] returns a mut slice; it does what it says. This name is
112+
preferred over `as_slice_mut`.
113+
114+
[`Vec::as_mut_slice`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.as_mut_slice
115+
116+
```rust
117+
// Return type is a mut slice.
118+
fn as_mut_slice(&mut self) -> &mut [T];
119+
```
120+
109121
##### More examples from the standard library
110122

111123
- [`Result::as_ref`](https://doc.rust-lang.org/std/result/enum.Result.html#method.as_ref)
@@ -191,65 +203,45 @@ example [`vec::IntoIter`].
191203
[btree_map::Values]: https://doc.rust-lang.org/std/collections/btree_map/struct.Values.html
192204

193205

194-
<a id="c-own-suffix"></a>
195-
## Ownership suffixes use `_mut`, `_ref` (C-OWN-SUFFIX)
196-
197-
Functions often come in multiple variants: immutably borrowed, mutably borrowed,
198-
and owned.
199-
200-
The right default depends on the function in question. Variants should be marked
201-
through suffixes.
202-
203-
### Exceptions
204-
205-
In the case of iterators, the moving variant should be understood as an `into`
206-
conversion, `into_iter`.
207-
208-
For mutably borrowed variants, if the `mut` qualifier is part of a type name,
209-
it should appear as it would appear in the type. For example
210-
[`Vec::as_mut_slice`] returns a mut slice; it does what it says.
211-
212-
[`Vec::as_mut_slice`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.as_mut_slice
213-
214-
### Immutably borrowed by default
215-
216-
If `foo` uses/produces an immutable borrow by default, use a `_mut` suffix (e.g.
217-
`foo_mut`) for the mutably borrowed variant.
218-
219-
#### Examples from the standard library
220-
221-
TODO [api-guidelines#37](https://github.com/rust-lang-nursery/api-guidelines/issues/37)
222-
223-
### Owned by default
224-
225-
If `foo` uses/produces owned data by default, use:
226-
227-
* The `_ref` suffix (e.g. `foo_ref`) for the immutably borrowed variant.
228-
* The `_mut` suffix (e.g. `foo_mut`) for the mutably borrowed variant.
206+
<a id="c-getter"></a>
207+
## Getter names follow Rust convention (C-GETTER)
229208

230-
#### Examples from the standard library
231-
232-
- [`std::io::BufReader::get_ref`](https://doc.rust-lang.org/std/io/struct.BufReader.html#method.get_ref)
233-
- [`std::io::BufReader::get_mut`](https://doc.rust-lang.org/std/io/struct.BufReader.html#method.get_mut)
234-
235-
236-
<a id="c-getters"></a>
237-
## Single-element containers implement appropriate getters (C-GETTERS)
238-
239-
Single-element containers where accessing the element cannot fail should
240-
implement `get` and `get_mut`, with the following signatures.
209+
With a few exceptions, the `get_` prefix is not used for getters in Rust code.
241210

242211
```rust
243-
fn get(&self) -> &V;
244-
fn get_mut(&mut self) -> &mut V;
212+
pub struct S {
213+
first: First,
214+
second: Second,
215+
}
216+
217+
impl S {
218+
// Not get_first.
219+
pub fn first(&self) -> &First {
220+
&self.first
221+
}
222+
223+
// Not get_first_mut, get_mut_first, or mut_first.
224+
pub fn first_mut(&mut self) -> &mut First {
225+
&mut self.first
226+
}
227+
}
245228
```
246229

247-
For getters that do runtime validation, consider adding unsafe `_unchecked`
248-
variants.
230+
The `get` naming is used only when there is a single and obvious thing that
231+
could reasonably be gotten by a getter. For example [`Cell::get`] accesses the
232+
content of a `Cell`.
233+
234+
[`Cell::get`]: https://doc.rust-lang.org/std/cell/struct.Cell.html#method.get
235+
236+
For getters that do runtime validation such as bounds checking, consider adding
237+
unsafe `_unchecked` variants. Typically those will have the following
238+
signatures.
249239

250240
```rust
251-
unsafe fn get_unchecked(&self, index) -> &V;
252-
unsafe fn get_unchecked_mut(&mut self, index) -> &mut V;
241+
fn get(&self, index: K) -> Option<&V>;
242+
fn get_mut(&mut self, index: K) -> Option<&mut V>;
243+
unsafe fn get_unchecked(&self, index: K) -> &V;
244+
unsafe fn get_unchecked_mut(&mut self, index: K) -> &mut V;
253245
```
254246

255247
### Examples from the standard library
@@ -259,7 +251,7 @@ unsafe fn get_unchecked_mut(&mut self, index) -> &mut V;
259251
- [`std::sync::PoisonError::get_mut`](https://doc.rust-lang.org/std/sync/struct.PoisonError.html#method.get_mut)
260252
- [`std::sync::atomic::AtomicBool::get_mut`](https://doc.rust-lang.org/std/sync/atomic/struct.AtomicBool.html#method.get_mut)
261253
- [`std::collections::hash_map::OccupiedEntry::get_mut`](https://doc.rust-lang.org/std/collections/hash_map/struct.OccupiedEntry.html#method.get_mut)
262-
- [`<[_]>::get_unchecked`](https://doc.rust-lang.org/std/primitive.slice.html#method.get_unchecked)
254+
- [`<[T]>::get_unchecked`](https://doc.rust-lang.org/std/primitive.slice.html#method.get_unchecked)
263255

264256

265257
<a id="c-feature"></a>

0 commit comments

Comments
 (0)