Skip to content

Commit 3d400fd

Browse files
committed
---
yaml --- r: 41718 b: refs/heads/master c: 7eae397 h: refs/heads/master v: v3
1 parent 11d1899 commit 3d400fd

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 50cd3c18f5cb07f5c12ad6f1cbe872f5971fec32
2+
refs/heads/master: 7eae397e58641392c20b4baefb39257264f7dcde
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 2f46b763da2c098913884f101b6d71d69af41b49
55
refs/heads/try: 3d5418789064fdb463e872a4e651af1c628a3650

trunk/src/libcore/option.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub pure fn map_consume<T, U>(opt: Option<T>,
118118
* As `map`, but consumes the option and gives `f` ownership to avoid
119119
* copying.
120120
*/
121-
if opt.is_some() { Some(f(option::unwrap(move opt))) } else { None }
121+
match opt { None => None, Some(v) => Some(f(v)) }
122122
}
123123

124124
#[inline(always)]
@@ -278,12 +278,42 @@ impl<T> Option<T> {
278278
#[inline(always)]
279279
pure fn map<U>(&self, f: fn(x: &T) -> U) -> Option<U> { map(self, f) }
280280

281+
/// As `map`, but consumes the option and gives `f` ownership to avoid
282+
/// copying.
283+
#[inline(always)]
284+
pure fn map_consume<U>(self, f: fn(v: T) -> U) -> Option<U> {
285+
map_consume(self, f)
286+
}
287+
281288
/// Applies a function to the contained value or returns a default
282289
#[inline(always)]
283290
pure fn map_default<U>(&self, def: U, f: fn(x: &T) -> U) -> U {
284291
map_default(self, move def, f)
285292
}
286293

294+
/// As `map_default`, but consumes the option and gives `f`
295+
/// ownership to avoid copying.
296+
#[inline(always)]
297+
pure fn map_consume_default<U>(self, def: U, f: fn(v: T) -> U) -> U {
298+
match self { None => def, Some(v) => f(v) }
299+
}
300+
301+
/// Apply a function to the contained value or do nothing
302+
fn mutate(&mut self, f: fn(T) -> T) {
303+
if self.is_some() {
304+
*self = Some(f(self.swap_unwrap()));
305+
}
306+
}
307+
308+
/// Apply a function to the contained value or set it to a default
309+
fn mutate_default(&mut self, def: T, f: fn(T) -> T) {
310+
if self.is_some() {
311+
*self = Some(f(self.swap_unwrap()));
312+
} else {
313+
*self = Some(def);
314+
}
315+
}
316+
287317
/// Performs an operation on the contained value by reference
288318
#[inline(always)]
289319
pure fn iter(&self, f: fn(x: &T)) { iter(self, f) }
@@ -315,6 +345,17 @@ impl<T> Option<T> {
315345
#[inline(always)]
316346
pure fn unwrap(self) -> T { unwrap(self) }
317347

348+
/**
349+
* The option dance. Moves a value out of an option type and returns it,
350+
* replacing the original with `None`.
351+
*
352+
* # Failure
353+
*
354+
* Fails if the value equals `None`.
355+
*/
356+
#[inline(always)]
357+
fn swap_unwrap(&mut self) -> T { swap_unwrap(self) }
358+
318359
/**
319360
* Gets the value out of an option, printing a specified message on
320361
* failure

0 commit comments

Comments
 (0)