Skip to content

Commit e0810c5

Browse files
committed
Move accessor guideline next to conversion guideline
This makes it easier to contrast the two subtly different use cases.
1 parent e17bacf commit e0810c5

File tree

2 files changed

+53
-53
lines changed

2 files changed

+53
-53
lines changed

src/checklist.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
- **Naming** *(crate aligns with Rust naming conventions)*
66
- [ ] Casing conforms to RFC 430 ([C-CASE])
77
- [ ] Ad-hoc conversions follow `as_`, `to_`, `into_` conventions ([C-CONV])
8+
- [ ] Getter names follow Rust convention ([C-GETTER])
89
- [ ] Methods on collections that produce iterators follow `iter`, `iter_mut`, `into_iter` ([C-ITER])
910
- [ ] Iterator type names match the methods that produce them ([C-ITER-TY])
10-
- [ ] Getter names follow Rust convention ([C-GETTER])
1111
- [ ] Feature names are free of placeholder words ([C-FEATURE])
1212
- [ ] Names use a consistent word order ([C-WORD-ORDER])
1313
- **Interoperability** *(crate interacts nicely with other library functionality)*
@@ -75,9 +75,9 @@
7575

7676
[C-CASE]: naming.html#c-case
7777
[C-CONV]: naming.html#c-naming
78+
[C-GETTER]: naming.html#c-getter
7879
[C-ITER]: naming.html#c-iter
7980
[C-ITER-TY]: naming.html#c-iter-ty
80-
[C-GETTER]: naming.html#c-getter
8181
[C-FEATURE]: naming.html#c-feature
8282
[C-WORD-ORDER]: naming.html#c-word-order
8383

src/naming.md

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,57 @@ fn as_mut_slice(&mut self) -> &mut [T];
126126
- [`Option::into_iter`](https://doc.rust-lang.org/std/option/enum.Option.html#method.into_iter)
127127

128128

129+
<a id="c-getter"></a>
130+
## Getter names follow Rust convention (C-GETTER)
131+
132+
With a few exceptions, the `get_` prefix is not used for getters in Rust code.
133+
134+
```rust
135+
pub struct S {
136+
first: First,
137+
second: Second,
138+
}
139+
140+
impl S {
141+
// Not get_first.
142+
pub fn first(&self) -> &First {
143+
&self.first
144+
}
145+
146+
// Not get_first_mut, get_mut_first, or mut_first.
147+
pub fn first_mut(&mut self) -> &mut First {
148+
&mut self.first
149+
}
150+
}
151+
```
152+
153+
The `get` naming is used only when there is a single and obvious thing that
154+
could reasonably be gotten by a getter. For example [`Cell::get`] accesses the
155+
content of a `Cell`.
156+
157+
[`Cell::get`]: https://doc.rust-lang.org/std/cell/struct.Cell.html#method.get
158+
159+
For getters that do runtime validation such as bounds checking, consider adding
160+
unsafe `_unchecked` variants. Typically those will have the following
161+
signatures.
162+
163+
```rust
164+
fn get(&self, index: K) -> Option<&V>;
165+
fn get_mut(&mut self, index: K) -> Option<&mut V>;
166+
unsafe fn get_unchecked(&self, index: K) -> &V;
167+
unsafe fn get_unchecked_mut(&mut self, index: K) -> &mut V;
168+
```
169+
170+
### Examples from the standard library
171+
172+
- [`std::io::Cursor::get_mut`](https://doc.rust-lang.org/std/io/struct.Cursor.html#method.get_mut)
173+
- [`std::ptr::Unique::get_mut`](https://doc.rust-lang.org/std/ptr/struct.Unique.html#method.get_mut)
174+
- [`std::sync::PoisonError::get_mut`](https://doc.rust-lang.org/std/sync/struct.PoisonError.html#method.get_mut)
175+
- [`std::sync::atomic::AtomicBool::get_mut`](https://doc.rust-lang.org/std/sync/atomic/struct.AtomicBool.html#method.get_mut)
176+
- [`std::collections::hash_map::OccupiedEntry::get_mut`](https://doc.rust-lang.org/std/collections/hash_map/struct.OccupiedEntry.html#method.get_mut)
177+
- [`<[T]>::get_unchecked`](https://doc.rust-lang.org/std/primitive.slice.html#method.get_unchecked)
178+
179+
129180
<a id="c-iter"></a>
130181
## Methods on collections that produce iterators follow `iter`, `iter_mut`, `into_iter` (C-ITER)
131182

@@ -203,57 +254,6 @@ example [`vec::IntoIter`].
203254
[btree_map::Values]: https://doc.rust-lang.org/std/collections/btree_map/struct.Values.html
204255

205256

206-
<a id="c-getter"></a>
207-
## Getter names follow Rust convention (C-GETTER)
208-
209-
With a few exceptions, the `get_` prefix is not used for getters in Rust code.
210-
211-
```rust
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-
}
228-
```
229-
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.
239-
240-
```rust
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;
245-
```
246-
247-
### Examples from the standard library
248-
249-
- [`std::io::Cursor::get_mut`](https://doc.rust-lang.org/std/io/struct.Cursor.html#method.get_mut)
250-
- [`std::ptr::Unique::get_mut`](https://doc.rust-lang.org/std/ptr/struct.Unique.html#method.get_mut)
251-
- [`std::sync::PoisonError::get_mut`](https://doc.rust-lang.org/std/sync/struct.PoisonError.html#method.get_mut)
252-
- [`std::sync::atomic::AtomicBool::get_mut`](https://doc.rust-lang.org/std/sync/atomic/struct.AtomicBool.html#method.get_mut)
253-
- [`std::collections::hash_map::OccupiedEntry::get_mut`](https://doc.rust-lang.org/std/collections/hash_map/struct.OccupiedEntry.html#method.get_mut)
254-
- [`<[T]>::get_unchecked`](https://doc.rust-lang.org/std/primitive.slice.html#method.get_unchecked)
255-
256-
257257
<a id="c-feature"></a>
258258
## Feature names are free of placeholder words (C-FEATURE)
259259

0 commit comments

Comments
 (0)