Skip to content

Commit ecce58c

Browse files
committed
auto merge of #15762 : nham/rust/ringbuf_docs, r=alexcrichton
This adds examples for get(), get_mut(), swap(), iter() and mut_iter()
2 parents d4dfb7d + ebe8097 commit ecce58c

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/libcollections/ringbuf.rs

+66
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ impl<T> RingBuf<T> {
133133
/// Retrieve an element in the RingBuf by index
134134
///
135135
/// Fails if there is no element with the given index
136+
///
137+
/// # Example
138+
///
139+
/// ```rust
140+
/// use std::collections::RingBuf;
141+
///
142+
/// let mut buf = RingBuf::new();
143+
/// buf.push(3i);
144+
/// buf.push(4);
145+
/// buf.push(5);
146+
/// assert_eq!(buf.get(1), &4);
147+
/// ```
136148
pub fn get<'a>(&'a self, i: uint) -> &'a T {
137149
let idx = self.raw_index(i);
138150
match *self.elts.get(idx) {
@@ -144,6 +156,19 @@ impl<T> RingBuf<T> {
144156
/// Retrieve an element in the RingBuf by index
145157
///
146158
/// Fails if there is no element with the given index
159+
///
160+
/// # Example
161+
///
162+
/// ```rust
163+
/// use std::collections::RingBuf;
164+
///
165+
/// let mut buf = RingBuf::new();
166+
/// buf.push(3i);
167+
/// buf.push(4);
168+
/// buf.push(5);
169+
/// *buf.get_mut(1) = 7;
170+
/// assert_eq!(buf.get(1), &7);
171+
/// ```
147172
pub fn get_mut<'a>(&'a mut self, i: uint) -> &'a mut T {
148173
let idx = self.raw_index(i);
149174
match *self.elts.get_mut(idx) {
@@ -157,6 +182,20 @@ impl<T> RingBuf<T> {
157182
/// `i` and `j` may be equal.
158183
///
159184
/// Fails if there is no element with the given index
185+
///
186+
/// # Example
187+
///
188+
/// ```rust
189+
/// use std::collections::RingBuf;
190+
///
191+
/// let mut buf = RingBuf::new();
192+
/// buf.push(3i);
193+
/// buf.push(4);
194+
/// buf.push(5);
195+
/// buf.swap(0, 2);
196+
/// assert_eq!(buf.get(0), &5);
197+
/// assert_eq!(buf.get(2), &3);
198+
/// ```
160199
pub fn swap(&mut self, i: uint, j: uint) {
161200
assert!(i < self.len());
162201
assert!(j < self.len());
@@ -196,11 +235,38 @@ impl<T> RingBuf<T> {
196235
}
197236

198237
/// Front-to-back iterator.
238+
///
239+
/// # Example
240+
///
241+
/// ```rust
242+
/// use std::collections::RingBuf;
243+
///
244+
/// let mut buf = RingBuf::new();
245+
/// buf.push(5i);
246+
/// buf.push(3);
247+
/// buf.push(4);
248+
/// assert_eq!(buf.iter().collect::<Vec<&int>>().as_slice(), &[&5, &3, &4]);
249+
/// ```
199250
pub fn iter<'a>(&'a self) -> Items<'a, T> {
200251
Items{index: 0, rindex: self.nelts, lo: self.lo, elts: self.elts.as_slice()}
201252
}
202253

203254
/// Front-to-back iterator which returns mutable values.
255+
///
256+
/// # Example
257+
///
258+
/// ```rust
259+
/// use std::collections::RingBuf;
260+
///
261+
/// let mut buf = RingBuf::new();
262+
/// buf.push(5i);
263+
/// buf.push(3);
264+
/// buf.push(4);
265+
/// for num in buf.mut_iter() {
266+
/// *num = *num - 2;
267+
/// }
268+
/// assert_eq!(buf.mut_iter().collect::<Vec<&mut int>>().as_slice(), &[&mut 3, &mut 1, &mut 2]);
269+
/// ```
204270
pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
205271
let start_index = raw_index(self.lo, self.elts.len(), 0);
206272
let end_index = raw_index(self.lo, self.elts.len(), self.nelts);

0 commit comments

Comments
 (0)