@@ -133,6 +133,18 @@ impl<T> RingBuf<T> {
133
133
/// Retrieve an element in the RingBuf by index
134
134
///
135
135
/// 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
+ /// ```
136
148
pub fn get < ' a > ( & ' a self , i : uint ) -> & ' a T {
137
149
let idx = self . raw_index ( i) ;
138
150
match * self . elts . get ( idx) {
@@ -144,6 +156,19 @@ impl<T> RingBuf<T> {
144
156
/// Retrieve an element in the RingBuf by index
145
157
///
146
158
/// 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
+ /// ```
147
172
pub fn get_mut < ' a > ( & ' a mut self , i : uint ) -> & ' a mut T {
148
173
let idx = self . raw_index ( i) ;
149
174
match * self . elts . get_mut ( idx) {
@@ -157,6 +182,20 @@ impl<T> RingBuf<T> {
157
182
/// `i` and `j` may be equal.
158
183
///
159
184
/// 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
+ /// ```
160
199
pub fn swap ( & mut self , i : uint , j : uint ) {
161
200
assert ! ( i < self . len( ) ) ;
162
201
assert ! ( j < self . len( ) ) ;
@@ -196,11 +235,38 @@ impl<T> RingBuf<T> {
196
235
}
197
236
198
237
/// 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
+ /// ```
199
250
pub fn iter < ' a > ( & ' a self ) -> Items < ' a , T > {
200
251
Items { index : 0 , rindex : self . nelts , lo : self . lo , elts : self . elts . as_slice ( ) }
201
252
}
202
253
203
254
/// 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
+ /// ```
204
270
pub fn mut_iter < ' a > ( & ' a mut self ) -> MutItems < ' a , T > {
205
271
let start_index = raw_index ( self . lo , self . elts . len ( ) , 0 ) ;
206
272
let end_index = raw_index ( self . lo , self . elts . len ( ) , self . nelts ) ;
0 commit comments