Skip to content

Commit ac85bf3

Browse files
committed
add Option methods for in-place mutation
1 parent 3c6da77 commit ac85bf3

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/libcore/option.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,22 @@ impl<T> Option<T> {
284284
match self { None => def, Some(v) => f(v) }
285285
}
286286

287+
/// Apply a function to the contained value or do nothing
288+
fn mutate(&mut self, f: fn(T) -> T) {
289+
if self.is_some() {
290+
*self = Some(f(self.swap_unwrap()));
291+
}
292+
}
293+
294+
/// Apply a function to the contained value or set it to a default
295+
fn mutate_default(&mut self, def: T, f: fn(T) -> T) {
296+
if self.is_some() {
297+
*self = Some(f(self.swap_unwrap()));
298+
} else {
299+
*self = Some(def);
300+
}
301+
}
302+
287303
/// Performs an operation on the contained value by reference
288304
#[inline(always)]
289305
pure fn iter(&self, f: fn(x: &T)) { iter(self, f) }

0 commit comments

Comments
 (0)