@@ -118,7 +118,7 @@ pub pure fn map_consume<T, U>(opt: Option<T>,
118
118
* As `map`, but consumes the option and gives `f` ownership to avoid
119
119
* copying.
120
120
*/
121
- if opt. is_some ( ) { Some ( f ( option :: unwrap ( move opt ) ) ) } else { None }
121
+ match opt { None => None , Some ( v ) => Some ( f ( v ) ) }
122
122
}
123
123
124
124
#[ inline( always) ]
@@ -278,12 +278,42 @@ impl<T> Option<T> {
278
278
#[ inline( always) ]
279
279
pure fn map < U > ( & self , f : fn ( x : & T ) -> U ) -> Option < U > { map ( self , f) }
280
280
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
+
281
288
/// Applies a function to the contained value or returns a default
282
289
#[ inline( always) ]
283
290
pure fn map_default < U > ( & self , def : U , f : fn ( x : & T ) -> U ) -> U {
284
291
map_default ( self , move def, f)
285
292
}
286
293
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
+
287
317
/// Performs an operation on the contained value by reference
288
318
#[ inline( always) ]
289
319
pure fn iter ( & self , f : fn ( x : & T ) ) { iter ( self , f) }
@@ -315,6 +345,17 @@ impl<T> Option<T> {
315
345
#[ inline( always) ]
316
346
pure fn unwrap ( self ) -> T { unwrap ( self ) }
317
347
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
+
318
359
/**
319
360
* Gets the value out of an option, printing a specified message on
320
361
* failure
0 commit comments