@@ -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