1 % Common container/wrapper methods [FIXME: needs RFC]
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.
7 In general, the `get` family of methods is used to access contained
8 data without any risk of thread 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.
14 For a container with keys/indexes of type `K` and elements of type `V`:
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>
21 // Convenience for .get(key).map(|elt| elt.clone())
22 fn get_clone(&self, key: K) -> Option<V>
24 // Lookup element, failing if it is not found:
25 impl Index<K, V> for Container { ... }
26 impl IndexMut<K, V> for Container { ... }
31 Prefer specific conversion functions like `as_bytes` or `into_vec` whenever
32 possible. Otherwise, use:
35 // Extract contents without failing
37 fn get_mut(&mut self) -> &mut V
41 #### Wrappers/Cells around `Copy` data
44 // Extract contents without failing
48 #### `Option`-like types
50 Finally, we have the cases of types like `Option` and `Result`, which
51 play a special role for failure.
56 // Extract contents or fail if not available
58 fn expect(self, &str) -> V
64 // Extract the contents of Ok variant; fail if Err
67 // Extract the contents of Err variant; fail if Ok
68 fn assert_err(self) -> E