@@ -1277,6 +1277,53 @@ impl<T, S: VecStorage<T> + ?Sized> Drop for VecInner<T, S> {
1277
1277
}
1278
1278
}
1279
1279
1280
+ #[ cfg( feature = "alloc" ) ]
1281
+ /// Converts the given `alloc::vec::Vec<T>` into a `Vec<T, N>`.
1282
+ impl < T , const N : usize > TryFrom < alloc:: vec:: Vec < T > > for Vec < T , N > {
1283
+ type Error = CapacityError ;
1284
+
1285
+ /// Converts the given `alloc::vec::Vec<T>` into a `Vec<T, N>`.
1286
+ ///
1287
+ /// # Errors
1288
+ ///
1289
+ /// Returns `Err` if the length of the `alloc::vec::Vec<T>` is greater than `N`.
1290
+ fn try_from ( alloc_vec : alloc:: vec:: Vec < T > ) -> Result < Self , Self :: Error > {
1291
+ let mut vec = Vec :: new ( ) ;
1292
+
1293
+ for e in alloc_vec {
1294
+ // Push each element individually to allow handling capacity errors.
1295
+ vec. push ( e) . map_err ( |_| CapacityError { } ) ?;
1296
+ }
1297
+
1298
+ Ok ( vec)
1299
+ }
1300
+ }
1301
+
1302
+ #[ cfg( feature = "alloc" ) ]
1303
+ /// Converts the given `Vec<T, N>` into an `alloc::vec::Vec<T>`.
1304
+ impl < T , const N : usize > TryFrom < Vec < T , N > > for alloc:: vec:: Vec < T > {
1305
+ type Error = alloc:: collections:: TryReserveError ;
1306
+
1307
+ /// Converts the given `Vec<T, N>` into an `alloc::vec::Vec<T>`.
1308
+ ///
1309
+ /// # Errors
1310
+ ///
1311
+ /// Returns `Err` if the `alloc::vec::Vec` fails to allocate memory.
1312
+ fn try_from ( vec : Vec < T , N > ) -> Result < Self , Self :: Error > {
1313
+ let mut alloc_vec = alloc:: vec:: Vec :: new ( ) ;
1314
+
1315
+ // Allocate enough space for the elements, return an error if the
1316
+ // allocation fails.
1317
+ alloc_vec. try_reserve_exact ( vec. len ( ) ) ?;
1318
+
1319
+ // Transfer the elements, since we reserved enough space above, this
1320
+ // should not fail due to OOM.
1321
+ alloc_vec. extend ( vec) ;
1322
+
1323
+ Ok ( alloc_vec)
1324
+ }
1325
+ }
1326
+
1280
1327
impl < ' a , T : Clone , const N : usize > TryFrom < & ' a [ T ] > for Vec < T , N > {
1281
1328
type Error = CapacityError ;
1282
1329
@@ -2099,6 +2146,31 @@ mod tests {
2099
2146
assert ! ( v. spare_capacity_mut( ) . is_empty( ) ) ;
2100
2147
}
2101
2148
2149
+ #[ test]
2150
+ #[ cfg( feature = "alloc" ) ]
2151
+ fn heapless_to_alloc ( ) {
2152
+ let mut hv: Vec < u8 , 4 > = Vec :: new ( ) ;
2153
+ hv. push ( 0 ) . unwrap ( ) ;
2154
+ hv. push ( 1 ) . unwrap ( ) ;
2155
+
2156
+ let av: alloc:: vec:: Vec < u8 > = hv. clone ( ) . try_into ( ) . unwrap ( ) ;
2157
+ assert_eq ! ( av. as_slice( ) , hv. as_slice( ) ) ;
2158
+ }
2159
+
2160
+ #[ test]
2161
+ #[ cfg( feature = "alloc" ) ]
2162
+ fn alloc_to_heapless ( ) {
2163
+ let mut av: alloc:: vec:: Vec < u8 > = alloc:: vec:: Vec :: new ( ) ;
2164
+ av. push ( 0 ) ;
2165
+ av. push ( 1 ) ;
2166
+
2167
+ let hv: Vec < u8 , 2 > = av. clone ( ) . try_into ( ) . unwrap ( ) ;
2168
+ assert_eq ! ( hv. as_slice( ) , av. as_slice( ) ) ;
2169
+
2170
+ let _: crate :: CapacityError =
2171
+ <alloc:: vec:: Vec < u8 > as TryInto < Vec < u8 , 1 > > >:: try_into ( av. clone ( ) ) . unwrap_err ( ) ;
2172
+ }
2173
+
2102
2174
fn _test_variance < ' a : ' b , ' b > ( x : Vec < & ' a ( ) , 42 > ) -> Vec < & ' b ( ) , 42 > {
2103
2175
x
2104
2176
}
0 commit comments