@@ -1306,6 +1306,49 @@ impl<T> Vec<T> {
1306
1306
}
1307
1307
other
1308
1308
}
1309
+
1310
+ /// Resizes the `Vec` in-place so that `len` is equal to `new_len`.
1311
+ ///
1312
+ /// If `new_len` is greater than `len`, the `Vec` is extended by the
1313
+ /// difference, with each additional slot filled with the result of
1314
+ /// calling the closure `f`. The return values from `f` will end up
1315
+ /// in the `Vec` in the order they have been generated.
1316
+ ///
1317
+ /// If `new_len` is less than `len`, the `Vec` is simply truncated.
1318
+ ///
1319
+ /// This method uses a closure to create new values on every push. If
1320
+ /// you'd rather [`Clone`] a given value, use [`resize`]. If you want
1321
+ /// to use the [`Default`] trait to generate values, you can pass
1322
+ /// [`Default::default()`] as the second argument..
1323
+ ///
1324
+ /// # Examples
1325
+ ///
1326
+ /// ```
1327
+ /// #![feature(vec_resize_with)]
1328
+ ///
1329
+ /// let mut vec = vec![1, 2, 3];
1330
+ /// vec.resize_with(5, Default::default);
1331
+ /// assert_eq!(vec, [1, 2, 3, 0, 0]);
1332
+ ///
1333
+ /// let mut vec = vec![];
1334
+ /// let mut p = 1;
1335
+ /// vec.resize_with(4, || { p *= 2; p });
1336
+ /// assert_eq!(vec, [2, 4, 8, 16]);
1337
+ /// ```
1338
+ ///
1339
+ /// [`resize`]: #method.resize
1340
+ /// [`Clone`]: ../../std/clone/trait.Clone.html
1341
+ #[ unstable( feature = "vec_resize_with" , issue = "41758" ) ]
1342
+ pub fn resize_with < F > ( & mut self , new_len : usize , f : F )
1343
+ where F : FnMut ( ) -> T
1344
+ {
1345
+ let len = self . len ( ) ;
1346
+ if new_len > len {
1347
+ self . extend_with ( new_len - len, ExtendFunc ( f) ) ;
1348
+ } else {
1349
+ self . truncate ( new_len) ;
1350
+ }
1351
+ }
1309
1352
}
1310
1353
1311
1354
impl < T : Clone > Vec < T > {
@@ -1316,8 +1359,8 @@ impl<T: Clone> Vec<T> {
1316
1359
/// If `new_len` is less than `len`, the `Vec` is simply truncated.
1317
1360
///
1318
1361
/// This method requires [`Clone`] to be able clone the passed value. If
1319
- /// you'd rather create a value with [`Default`] instead, see
1320
- /// [`resize_default `].
1362
+ /// you need more flexibility (or want to rely on [`Default`] instead of
1363
+ /// [`Clone`]), use [`resize_with `].
1321
1364
///
1322
1365
/// # Examples
1323
1366
///
@@ -1333,7 +1376,7 @@ impl<T: Clone> Vec<T> {
1333
1376
///
1334
1377
/// [`Clone`]: ../../std/clone/trait.Clone.html
1335
1378
/// [`Default`]: ../../std/default/trait.Default.html
1336
- /// [`resize_default `]: #method.resize_default
1379
+ /// [`resize_with `]: #method.resize_with
1337
1380
#[ stable( feature = "vec_resize" , since = "1.5.0" ) ]
1338
1381
pub fn resize ( & mut self , new_len : usize , value : T ) {
1339
1382
let len = self . len ( ) ;
@@ -1412,24 +1455,31 @@ impl<T: Default> Vec<T> {
1412
1455
1413
1456
// This code generalises `extend_with_{element,default}`.
1414
1457
trait ExtendWith < T > {
1415
- fn next ( & self ) -> T ;
1458
+ fn next ( & mut self ) -> T ;
1416
1459
fn last ( self ) -> T ;
1417
1460
}
1418
1461
1419
1462
struct ExtendElement < T > ( T ) ;
1420
1463
impl < T : Clone > ExtendWith < T > for ExtendElement < T > {
1421
- fn next ( & self ) -> T { self . 0 . clone ( ) }
1464
+ fn next ( & mut self ) -> T { self . 0 . clone ( ) }
1422
1465
fn last ( self ) -> T { self . 0 }
1423
1466
}
1424
1467
1425
1468
struct ExtendDefault ;
1426
1469
impl < T : Default > ExtendWith < T > for ExtendDefault {
1427
- fn next ( & self ) -> T { Default :: default ( ) }
1470
+ fn next ( & mut self ) -> T { Default :: default ( ) }
1428
1471
fn last ( self ) -> T { Default :: default ( ) }
1429
1472
}
1473
+
1474
+ struct ExtendFunc < F > ( F ) ;
1475
+ impl < T , F : FnMut ( ) -> T > ExtendWith < T > for ExtendFunc < F > {
1476
+ fn next ( & mut self ) -> T { ( self . 0 ) ( ) }
1477
+ fn last ( mut self ) -> T { ( self . 0 ) ( ) }
1478
+ }
1479
+
1430
1480
impl < T > Vec < T > {
1431
1481
/// Extend the vector by `n` values, using the given generator.
1432
- fn extend_with < E : ExtendWith < T > > ( & mut self , n : usize , value : E ) {
1482
+ fn extend_with < E : ExtendWith < T > > ( & mut self , n : usize , mut value : E ) {
1433
1483
self . reserve ( n) ;
1434
1484
1435
1485
unsafe {
0 commit comments