Skip to content

Commit 467d381

Browse files
committed
auto merge of #7931 : blake2-ppc/rust/chain-mut-ref, r=pcwalton
First, clean up the uses of "None" and "Some" to always use consistent title case matching the variant names. Add .chain_mut_ref() which is a missing method. A use case example for this method is extraction of an optional value from an Option\<Container\> value.
2 parents 4a726f0 + 625ca7a commit 467d381

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

src/libstd/option.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ impl<T> Option<T> {
118118
}
119119
}
120120

121-
/// Returns true if the option equals `none`
121+
/// Returns true if the option equals `None`
122122
#[inline]
123123
pub fn is_none(&self) -> bool {
124124
match *self { None => true, Some(_) => false }
125125
}
126126

127-
/// Returns true if the option contains some value
127+
/// Returns true if the option contains a `Some` value
128128
#[inline]
129129
pub fn is_some(&self) -> bool { !self.is_none() }
130130

@@ -158,6 +158,17 @@ impl<T> Option<T> {
158158
}
159159
}
160160

161+
/// Update an optional value by optionally running its content by mut reference
162+
/// through a function that returns an option.
163+
#[inline]
164+
pub fn chain_mut_ref<'a, U>(&'a mut self, f: &fn(x: &'a mut T) -> Option<U>)
165+
-> Option<U> {
166+
match *self {
167+
Some(ref mut x) => f(x),
168+
None => None
169+
}
170+
}
171+
161172
/// Filters an optional value using given function.
162173
#[inline(always)]
163174
pub fn filtered(self, f: &fn(t: &T) -> bool) -> Option<T> {
@@ -167,19 +178,19 @@ impl<T> Option<T> {
167178
}
168179
}
169180

170-
/// Maps a `some` value from one type to another by reference
181+
/// Maps a `Some` value from one type to another by reference
171182
#[inline]
172183
pub fn map<'a, U>(&'a self, f: &fn(&'a T) -> U) -> Option<U> {
173184
match *self { Some(ref x) => Some(f(x)), None => None }
174185
}
175186

176-
/// Maps a `some` value from one type to another by a mutable reference
187+
/// Maps a `Some` value from one type to another by a mutable reference
177188
#[inline]
178189
pub fn map_mut<'a, U>(&'a mut self, f: &fn(&'a mut T) -> U) -> Option<U> {
179190
match *self { Some(ref mut x) => Some(f(x)), None => None }
180191
}
181192

182-
/// Maps a `some` value from one type to another by a mutable reference,
193+
/// Maps a `Some` value from one type to another by a mutable reference,
183194
/// or returns a default value.
184195
#[inline]
185196
pub fn map_mut_default<'a, U>(&'a mut self, def: U, f: &fn(&'a mut T) -> U) -> U {
@@ -260,7 +271,7 @@ impl<T> Option<T> {
260271
pub fn get_ref<'a>(&'a self) -> &'a T {
261272
match *self {
262273
Some(ref x) => x,
263-
None => fail!("option::get_ref none")
274+
None => fail!("option::get_ref None")
264275
}
265276
}
266277

@@ -282,7 +293,7 @@ impl<T> Option<T> {
282293
pub fn get_mut_ref<'a>(&'a mut self) -> &'a mut T {
283294
match *self {
284295
Some(ref mut x) => x,
285-
None => fail!("option::get_mut_ref none")
296+
None => fail!("option::get_mut_ref None")
286297
}
287298
}
288299

@@ -306,7 +317,7 @@ impl<T> Option<T> {
306317
*/
307318
match self {
308319
Some(x) => x,
309-
None => fail!("option::unwrap none")
320+
None => fail!("option::unwrap None")
310321
}
311322
}
312323

@@ -320,7 +331,7 @@ impl<T> Option<T> {
320331
*/
321332
#[inline]
322333
pub fn take_unwrap(&mut self) -> T {
323-
if self.is_none() { fail!("option::take_unwrap none") }
334+
if self.is_none() { fail!("option::take_unwrap None") }
324335
self.take().unwrap()
325336
}
326337

@@ -330,7 +341,7 @@ impl<T> Option<T> {
330341
*
331342
* # Failure
332343
*
333-
* Fails if the value equals `none`
344+
* Fails if the value equals `None`
334345
*/
335346
#[inline]
336347
pub fn expect(self, reason: &str) -> T {
@@ -358,7 +369,7 @@ impl<T> Option<T> {
358369
pub fn get(self) -> T {
359370
match self {
360371
Some(x) => return x,
361-
None => fail!("option::get none")
372+
None => fail!("option::get None")
362373
}
363374
}
364375

@@ -368,7 +379,7 @@ impl<T> Option<T> {
368379
match self { Some(x) => x, None => def }
369380
}
370381

371-
/// Applies a function zero or more times until the result is none.
382+
/// Applies a function zero or more times until the result is None.
372383
#[inline]
373384
pub fn while_some(self, blk: &fn(v: T) -> Option<T>) {
374385
let mut opt = self;

0 commit comments

Comments
 (0)