@@ -1220,11 +1220,14 @@ impl<T> Vec<T> {
1220
1220
}
1221
1221
1222
1222
impl < T : Clone > Vec < T > {
1223
- /// Resizes the `Vec` in-place so that `len() ` is equal to `new_len`.
1223
+ /// Resizes the `Vec` in-place so that `len` is equal to `new_len`.
1224
1224
///
1225
- /// If `new_len` is greater than `len() `, the `Vec` is extended by the
1225
+ /// If `new_len` is greater than `len`, the `Vec` is extended by the
1226
1226
/// difference, with each additional slot filled with `value`.
1227
- /// If `new_len` is less than `len()`, the `Vec` is simply truncated.
1227
+ /// If `new_len` is less than `len`, the `Vec` is simply truncated.
1228
+ ///
1229
+ /// This method requires `Clone` to clone the passed value. If you'd
1230
+ /// rather create a value with `Default` instead, see [`resize_default`].
1228
1231
///
1229
1232
/// # Examples
1230
1233
///
@@ -1237,19 +1240,100 @@ impl<T: Clone> Vec<T> {
1237
1240
/// vec.resize(2, 0);
1238
1241
/// assert_eq!(vec, [1, 2]);
1239
1242
/// ```
1243
+ ///
1244
+ /// [`resize_default`]: #method.resize_default
1240
1245
#[ stable( feature = "vec_resize" , since = "1.5.0" ) ]
1241
1246
pub fn resize ( & mut self , new_len : usize , value : T ) {
1242
1247
let len = self . len ( ) ;
1243
1248
1244
1249
if new_len > len {
1245
- self . extend_with_element ( new_len - len, value) ;
1250
+ self . extend_with ( new_len - len, ExtendElement ( value) )
1251
+ } else {
1252
+ self . truncate ( new_len) ;
1253
+ }
1254
+ }
1255
+
1256
+ /// Clones and appends all elements in a slice to the `Vec`.
1257
+ ///
1258
+ /// Iterates over the slice `other`, clones each element, and then appends
1259
+ /// it to this `Vec`. The `other` vector is traversed in-order.
1260
+ ///
1261
+ /// Note that this function is same as `extend` except that it is
1262
+ /// specialized to work with slices instead. If and when Rust gets
1263
+ /// specialization this function will likely be deprecated (but still
1264
+ /// available).
1265
+ ///
1266
+ /// # Examples
1267
+ ///
1268
+ /// ```
1269
+ /// let mut vec = vec![1];
1270
+ /// vec.extend_from_slice(&[2, 3, 4]);
1271
+ /// assert_eq!(vec, [1, 2, 3, 4]);
1272
+ /// ```
1273
+ #[ stable( feature = "vec_extend_from_slice" , since = "1.6.0" ) ]
1274
+ pub fn extend_from_slice ( & mut self , other : & [ T ] ) {
1275
+ self . spec_extend ( other. iter ( ) )
1276
+ }
1277
+ }
1278
+
1279
+ impl < T : Default > Vec < T > {
1280
+ /// Resizes the `Vec` in-place so that `len` is equal to `new_len`.
1281
+ ///
1282
+ /// If `new_len` is greater than `len`, the `Vec` is extended by the
1283
+ /// difference, with each additional slot filled with `Default::default()`.
1284
+ /// If `new_len` is less than `len`, the `Vec` is simply truncated.
1285
+ ///
1286
+ /// This method uses `Default` to create new values on every push. If
1287
+ /// you'd rather `Clone` a given value, use [`resize`].
1288
+ ///
1289
+ ///
1290
+ /// # Examples
1291
+ ///
1292
+ /// ```
1293
+ /// #![feature(vec_resize_default)]
1294
+ ///
1295
+ /// let mut vec = vec![1, 2, 3];
1296
+ /// vec.resize_default(5);
1297
+ /// assert_eq!(vec, [1, 2, 3, 0, 0]);
1298
+ ///
1299
+ /// let mut vec = vec![1, 2, 3, 4];
1300
+ /// vec.resize_default(2);
1301
+ /// assert_eq!(vec, [1, 2]);
1302
+ /// ```
1303
+ ///
1304
+ /// [`resize`]: #method.resize
1305
+ #[ unstable( feature = "vec_resize_default" , issue = "41758" ) ]
1306
+ pub fn resize_default ( & mut self , new_len : usize ) {
1307
+ let len = self . len ( ) ;
1308
+
1309
+ if new_len > len {
1310
+ self . extend_with ( new_len - len, ExtendDefault ) ;
1246
1311
} else {
1247
1312
self . truncate ( new_len) ;
1248
1313
}
1249
1314
}
1315
+ }
1250
1316
1251
- /// Extend the vector by `n` additional clones of `value`.
1252
- fn extend_with_element ( & mut self , n : usize , value : T ) {
1317
+ // This code generalises `extend_with_{element,default}`.
1318
+ trait ExtendWith < T > {
1319
+ fn next ( & self ) -> T ;
1320
+ fn last ( self ) -> T ;
1321
+ }
1322
+
1323
+ struct ExtendElement < T > ( T ) ;
1324
+ impl < T : Clone > ExtendWith < T > for ExtendElement < T > {
1325
+ fn next ( & self ) -> T { self . 0 . clone ( ) }
1326
+ fn last ( self ) -> T { self . 0 }
1327
+ }
1328
+
1329
+ struct ExtendDefault ;
1330
+ impl < T : Default > ExtendWith < T > for ExtendDefault {
1331
+ fn next ( & self ) -> T { Default :: default ( ) }
1332
+ fn last ( self ) -> T { Default :: default ( ) }
1333
+ }
1334
+ impl < T > Vec < T > {
1335
+ /// Extend the vector by `n` values, using the given generator.
1336
+ fn extend_with < E : ExtendWith < T > > ( & mut self , n : usize , value : E ) {
1253
1337
self . reserve ( n) ;
1254
1338
1255
1339
unsafe {
@@ -1261,43 +1345,21 @@ impl<T: Clone> Vec<T> {
1261
1345
1262
1346
// Write all elements except the last one
1263
1347
for _ in 1 ..n {
1264
- ptr:: write ( ptr, value. clone ( ) ) ;
1348
+ ptr:: write ( ptr, value. next ( ) ) ;
1265
1349
ptr = ptr. offset ( 1 ) ;
1266
- // Increment the length in every step in case clone () panics
1350
+ // Increment the length in every step in case next () panics
1267
1351
local_len. increment_len ( 1 ) ;
1268
1352
}
1269
1353
1270
1354
if n > 0 {
1271
1355
// We can write the last element directly without cloning needlessly
1272
- ptr:: write ( ptr, value) ;
1356
+ ptr:: write ( ptr, value. last ( ) ) ;
1273
1357
local_len. increment_len ( 1 ) ;
1274
1358
}
1275
1359
1276
1360
// len set by scope guard
1277
1361
}
1278
1362
}
1279
-
1280
- /// Clones and appends all elements in a slice to the `Vec`.
1281
- ///
1282
- /// Iterates over the slice `other`, clones each element, and then appends
1283
- /// it to this `Vec`. The `other` vector is traversed in-order.
1284
- ///
1285
- /// Note that this function is same as `extend` except that it is
1286
- /// specialized to work with slices instead. If and when Rust gets
1287
- /// specialization this function will likely be deprecated (but still
1288
- /// available).
1289
- ///
1290
- /// # Examples
1291
- ///
1292
- /// ```
1293
- /// let mut vec = vec![1];
1294
- /// vec.extend_from_slice(&[2, 3, 4]);
1295
- /// assert_eq!(vec, [1, 2, 3, 4]);
1296
- /// ```
1297
- #[ stable( feature = "vec_extend_from_slice" , since = "1.6.0" ) ]
1298
- pub fn extend_from_slice ( & mut self , other : & [ T ] ) {
1299
- self . spec_extend ( other. iter ( ) )
1300
- }
1301
1363
}
1302
1364
1303
1365
// Set the length of the vec when the `SetLenOnDrop` value goes out of scope.
@@ -1389,7 +1451,7 @@ trait SpecFromElem: Sized {
1389
1451
impl < T : Clone > SpecFromElem for T {
1390
1452
default fn from_elem ( elem : Self , n : usize ) -> Vec < Self > {
1391
1453
let mut v = Vec :: with_capacity ( n) ;
1392
- v. extend_with_element ( n, elem) ;
1454
+ v. extend_with ( n, ExtendElement ( elem) ) ;
1393
1455
v
1394
1456
}
1395
1457
}
@@ -1424,7 +1486,7 @@ macro_rules! impl_spec_from_elem {
1424
1486
}
1425
1487
}
1426
1488
let mut v = Vec :: with_capacity( n) ;
1427
- v. extend_with_element ( n, elem) ;
1489
+ v. extend_with ( n, ExtendElement ( elem) ) ;
1428
1490
v
1429
1491
}
1430
1492
}
0 commit comments