Skip to content

Commit 4461f03

Browse files
committed
auto merge of #15949 : alexcrichton/rust/rollup, r=alexcrichton
2 parents 6203f8a + 1031ad8 commit 4461f03

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+929
-374
lines changed

src/doc/rust.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -3864,13 +3864,13 @@ Function parameters are immutable unless declared with `mut`. The
38643864
and `fn f(mut x: Box<int>, y: Box<int>)` declare one mutable variable `x` and
38653865
one immutable variable `y`).
38663866

3867-
Methods that take either `self` or `~self` can optionally place them in a
3867+
Methods that take either `self` or `Box<Self>` can optionally place them in a
38683868
mutable slot by prefixing them with `mut` (similar to regular arguments):
38693869

38703870
~~~
38713871
trait Changer {
38723872
fn change(mut self) -> Self;
3873-
fn modify(mut ~self) -> Box<Self>;
3873+
fn modify(mut self: Box<Self>) -> Box<Self>;
38743874
}
38753875
~~~
38763876

src/doc/tutorial.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1971,7 +1971,7 @@ like any other function, except for the name `self`.
19711971

19721972
The type of `self` is the type on which the method is implemented,
19731973
or a pointer thereof. As an argument it is written either `self`,
1974-
`&self`, or `~self`.
1974+
`&self`, or `self: TYPE`.
19751975
A caller must in turn have a compatible pointer type to call the method.
19761976

19771977
~~~
@@ -1984,7 +1984,7 @@ A caller must in turn have a compatible pointer type to call the method.
19841984
# }
19851985
impl Shape {
19861986
fn draw_reference(&self) { /* ... */ }
1987-
fn draw_owned(~self) { /* ... */ }
1987+
fn draw_owned(self: Box<Shape>) { /* ... */ }
19881988
fn draw_value(self) { /* ... */ }
19891989
}
19901990
@@ -2009,7 +2009,7 @@ to a reference.
20092009
# }
20102010
# impl Shape {
20112011
# fn draw_reference(&self) { /* ... */ }
2012-
# fn draw_owned(~self) { /* ... */ }
2012+
# fn draw_owned(self: Box<Shape>) { /* ... */ }
20132013
# fn draw_value(self) { /* ... */ }
20142014
# }
20152015
# let s = Circle(Point { x: 1.0, y: 2.0 }, 3.0);

src/libcollections/deque.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub mod bench {
7171
// measure
7272
let mut i = 0;
7373
b.iter(|| {
74-
map.find(keys.get(i));
74+
map.find(&keys[i]);
7575
i = (i + 1) % n;
7676
})
7777
}

src/libcollections/smallintmap.rs

+133-18
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
/*!
12-
* A simple map based on a vector for small integer keys. Space requirements
13-
* are O(highest integer key).
14-
*/
11+
//! A simple map based on a vector for small integer keys. Space requirements
12+
//! are O(highest integer key).
1513
1614
#![allow(missing_doc)]
1715

@@ -26,18 +24,50 @@ use {Collection, Mutable, Map, MutableMap, MutableSeq};
2624
use {vec, slice};
2725
use vec::Vec;
2826

29-
#[allow(missing_doc)]
27+
/// A map optimized for small integer keys.
28+
///
29+
/// # Example
30+
///
31+
/// ```
32+
/// use std::collections::SmallIntMap;
33+
///
34+
/// let mut months = SmallIntMap::new();
35+
/// months.insert(1, "Jan");
36+
/// months.insert(2, "Feb");
37+
/// months.insert(3, "Mar");
38+
///
39+
/// if !months.contains_key(&12) {
40+
/// println!("The end is near!");
41+
/// }
42+
///
43+
/// assert_eq!(months.find(&1), Some(&"Jan"));
44+
///
45+
/// match months.find_mut(&3) {
46+
/// Some(value) => *value = "Venus",
47+
/// None => (),
48+
/// }
49+
///
50+
/// assert_eq!(months.find(&3), Some(&"Venus"));
51+
///
52+
/// // Print out all months
53+
/// for (key, value) in months.iter() {
54+
/// println!("month {} is {}", key, value);
55+
/// }
56+
///
57+
/// months.clear();
58+
/// assert!(months.is_empty());
59+
/// ```
3060
pub struct SmallIntMap<T> {
3161
v: Vec<Option<T>>,
3262
}
3363

3464
impl<V> Collection for SmallIntMap<V> {
35-
/// Return the number of elements in the map
65+
/// Return the number of elements in the map.
3666
fn len(&self) -> uint {
3767
self.v.iter().filter(|elt| elt.is_some()).count()
3868
}
3969

40-
/// Return true if there are no elements in the map
70+
/// Return `true` if there are no elements in the map.
4171
fn is_empty(&self) -> bool {
4272
self.v.iter().all(|elt| elt.is_none())
4373
}
@@ -49,7 +79,7 @@ impl<V> Mutable for SmallIntMap<V> {
4979
}
5080

5181
impl<V> Map<uint, V> for SmallIntMap<V> {
52-
/// Return a reference to the value corresponding to the key
82+
/// Return a reference to the value corresponding to the key.
5383
fn find<'a>(&'a self, key: &uint) -> Option<&'a V> {
5484
if *key < self.v.len() {
5585
match *self.v.get(*key) {
@@ -63,7 +93,7 @@ impl<V> Map<uint, V> for SmallIntMap<V> {
6393
}
6494

6595
impl<V> MutableMap<uint, V> for SmallIntMap<V> {
66-
/// Return a mutable reference to the value corresponding to the key
96+
/// Return a mutable reference to the value corresponding to the key.
6797
fn find_mut<'a>(&'a mut self, key: &uint) -> Option<&'a mut V> {
6898
if *key < self.v.len() {
6999
match *self.v.get_mut(*key) {
@@ -76,7 +106,7 @@ impl<V> MutableMap<uint, V> for SmallIntMap<V> {
76106
}
77107

78108
/// Insert a key-value pair into the map. An existing value for a
79-
/// key is replaced by the new value. Return true if the key did
109+
/// key is replaced by the new value. Return `true` if the key did
80110
/// not already exist in the map.
81111
fn insert(&mut self, key: uint, value: V) -> bool {
82112
let exists = self.contains_key(&key);
@@ -88,14 +118,14 @@ impl<V> MutableMap<uint, V> for SmallIntMap<V> {
88118
!exists
89119
}
90120

91-
/// Remove a key-value pair from the map. Return true if the key
92-
/// was present in the map, otherwise false.
121+
/// Remove a key-value pair from the map. Return `true` if the key
122+
/// was present in the map, otherwise `false`.
93123
fn remove(&mut self, key: &uint) -> bool {
94124
self.pop(key).is_some()
95125
}
96126

97127
/// Insert a key-value pair from the map. If the key already had a value
98-
/// present in the map, that value is returned. Otherwise None is returned.
128+
/// present in the map, that value is returned. Otherwise `None` is returned.
99129
fn swap(&mut self, key: uint, value: V) -> Option<V> {
100130
match self.find_mut(&key) {
101131
Some(loc) => { return Some(replace(loc, value)); }
@@ -121,20 +151,67 @@ impl<V> Default for SmallIntMap<V> {
121151
}
122152

123153
impl<V> SmallIntMap<V> {
124-
/// Create an empty SmallIntMap
154+
/// Create an empty SmallIntMap.
155+
///
156+
/// # Example
157+
///
158+
/// ```
159+
/// use std::collections::SmallIntMap;
160+
/// let mut map: SmallIntMap<&str> = SmallIntMap::new();
161+
/// ```
125162
pub fn new() -> SmallIntMap<V> { SmallIntMap{v: vec!()} }
126163

127-
/// Create an empty SmallIntMap with capacity `capacity`
164+
/// Create an empty SmallIntMap with space for at least `capacity` elements
165+
/// before resizing.
166+
///
167+
/// # Example
168+
///
169+
/// ```
170+
/// use std::collections::SmallIntMap;
171+
/// let mut map: SmallIntMap<&str> = SmallIntMap::with_capacity(10);
172+
/// ```
128173
pub fn with_capacity(capacity: uint) -> SmallIntMap<V> {
129174
SmallIntMap { v: Vec::with_capacity(capacity) }
130175
}
131176

177+
/// Retrieves a value for the given key.
178+
/// See [`find`](../trait.Map.html#tymethod.find) for a non-failing alternative.
179+
///
180+
/// # Failure
181+
///
182+
/// Fails if the key is not present.
183+
///
184+
/// # Example
185+
///
186+
/// ```
187+
/// use std::collections::SmallIntMap;
188+
///
189+
/// let mut map = SmallIntMap::new();
190+
/// map.insert(1, "a");
191+
/// assert_eq!(map.get(&1), &"a");
192+
/// ```
132193
pub fn get<'a>(&'a self, key: &uint) -> &'a V {
133194
self.find(key).expect("key not present")
134195
}
135196

136197
/// An iterator visiting all key-value pairs in ascending order by the keys.
137-
/// Iterator element type is (uint, &'r V)
198+
/// Iterator element type is `(uint, &'r V)`.
199+
///
200+
/// # Example
201+
///
202+
/// ```
203+
/// use std::collections::SmallIntMap;
204+
///
205+
/// let mut map = SmallIntMap::new();
206+
/// map.insert(1, "a");
207+
/// map.insert(3, "c");
208+
/// map.insert(2, "b");
209+
///
210+
/// // Print `1: a` then `2: b` then `3: c`
211+
/// for (key, value) in map.iter() {
212+
/// println!("{}: {}", key, value);
213+
/// }
214+
/// ```
138215
pub fn iter<'r>(&'r self) -> Entries<'r, V> {
139216
Entries {
140217
front: 0,
@@ -145,7 +222,26 @@ impl<V> SmallIntMap<V> {
145222

146223
/// An iterator visiting all key-value pairs in ascending order by the keys,
147224
/// with mutable references to the values
148-
/// Iterator element type is (uint, &'r mut V)
225+
/// Iterator element type is `(uint, &'r mut V)`.
226+
///
227+
/// # Example
228+
///
229+
/// ```
230+
/// use std::collections::SmallIntMap;
231+
///
232+
/// let mut map = SmallIntMap::new();
233+
/// map.insert(1, "a");
234+
/// map.insert(2, "b");
235+
/// map.insert(3, "c");
236+
///
237+
/// for (key, value) in map.mut_iter() {
238+
/// *value = "x";
239+
/// }
240+
///
241+
/// for (key, value) in map.iter() {
242+
/// assert_eq!(value, &"x");
243+
/// }
244+
/// ```
149245
pub fn mut_iter<'r>(&'r mut self) -> MutEntries<'r, V> {
150246
MutEntries {
151247
front: 0,
@@ -154,7 +250,23 @@ impl<V> SmallIntMap<V> {
154250
}
155251
}
156252

157-
/// Empties the hash map, moving all values into the specified closure
253+
/// Empties the hash map, moving all values into the specified closure.
254+
///
255+
/// # Example
256+
///
257+
/// ```
258+
/// use std::collections::SmallIntMap;
259+
///
260+
/// let mut map = SmallIntMap::new();
261+
/// map.insert(1, "a");
262+
/// map.insert(3, "c");
263+
/// map.insert(2, "b");
264+
///
265+
/// // Not possible with .iter()
266+
/// let vec: Vec<(uint, &str)> = map.move_iter().collect();
267+
///
268+
/// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
269+
/// ```
158270
pub fn move_iter(&mut self)
159271
-> FilterMap<(uint, Option<V>), (uint, V),
160272
Enumerate<vec::MoveItems<Option<V>>>>
@@ -249,6 +361,7 @@ macro_rules! double_ended_iterator {
249361
}
250362
}
251363

364+
/// Forward iterator over a map.
252365
pub struct Entries<'a, T> {
253366
front: uint,
254367
back: uint,
@@ -258,6 +371,8 @@ pub struct Entries<'a, T> {
258371
iterator!(impl Entries -> (uint, &'a T), get_ref)
259372
double_ended_iterator!(impl Entries -> (uint, &'a T), get_ref)
260373

374+
/// Forward iterator over the key-value pairs of a map, with the
375+
/// values being mutable.
261376
pub struct MutEntries<'a, T> {
262377
front: uint,
263378
back: uint,

0 commit comments

Comments
 (0)