File tree 3 files changed +48
-1
lines changed
3 files changed +48
-1
lines changed Original file line number Diff line number Diff line change 87
87
88
88
#![ doc( primitive = "slice" ) ]
89
89
90
+ use alloc:: boxed:: Box ;
90
91
use core:: cmp;
91
92
use core:: mem:: size_of;
92
93
use core:: mem;
@@ -298,6 +299,23 @@ impl<'a, T: Clone> CloneableVector<T> for &'a [T] {
298
299
fn into_vec ( self ) -> Vec < T > { self . to_vec ( ) }
299
300
}
300
301
302
+ #[ experimental]
303
+ pub trait BoxedSlice < T > {
304
+ /// Convert `self` into a vector without clones or allocation.
305
+ fn into_vec ( self ) -> Vec < T > ;
306
+ }
307
+
308
+ impl < T > BoxedSlice < T > for Box < [ T ] > {
309
+ #[ experimental]
310
+ fn into_vec ( mut self ) -> Vec < T > {
311
+ unsafe {
312
+ let xs = Vec :: from_raw_parts ( self . len ( ) , self . len ( ) , self . as_mut_ptr ( ) ) ;
313
+ mem:: forget ( self ) ;
314
+ xs
315
+ }
316
+ }
317
+ }
318
+
301
319
/// Extension methods for vectors containing `Clone` elements.
302
320
pub trait ImmutableCloneableVector < T > {
303
321
/// Partitions the vector into two vectors `(a, b)`, where all
@@ -2308,6 +2326,13 @@ mod tests {
2308
2326
let y: & mut [ int ] = [ ] ;
2309
2327
assert ! ( y. last_mut( ) . is_none( ) ) ;
2310
2328
}
2329
+
2330
+ #[ test]
2331
+ fn test_into_vec ( ) {
2332
+ let xs = box [ 1 u, 2 , 3 ] ;
2333
+ let ys = xs. into_vec ( ) ;
2334
+ assert_eq ! ( ys. as_slice( ) , [ 1 u, 2 , 3 ] ) ;
2335
+ }
2311
2336
}
2312
2337
2313
2338
#[ cfg( test) ]
Original file line number Diff line number Diff line change 14
14
15
15
use core:: prelude:: * ;
16
16
17
+ use alloc:: boxed:: Box ;
17
18
use alloc:: heap:: { EMPTY , allocate, reallocate, deallocate} ;
18
19
use core:: cmp:: max;
19
20
use core:: default:: Default ;
@@ -757,6 +758,20 @@ impl<T> Vec<T> {
757
758
}
758
759
}
759
760
761
+ /// Convert the vector into Box<[T]>.
762
+ ///
763
+ /// Note that this will drop any excess capacity. Calling this and converting back to a vector
764
+ /// with `into_vec()` is equivalent to calling `shrink_to_fit()`.
765
+ #[ experimental]
766
+ pub fn into_boxed_slice ( mut self ) -> Box < [ T ] > {
767
+ self . shrink_to_fit ( ) ;
768
+ unsafe {
769
+ let xs: Box < [ T ] > = mem:: transmute ( self . as_mut_slice ( ) ) ;
770
+ mem:: forget ( self ) ;
771
+ xs
772
+ }
773
+ }
774
+
760
775
/// Deprecated, call `push` instead
761
776
#[ inline]
762
777
#[ deprecated = "call .push() instead" ]
@@ -2631,6 +2646,13 @@ mod tests {
2631
2646
assert ! ( vec2 == vec!( ( ) , ( ) , ( ) ) ) ;
2632
2647
}
2633
2648
2649
+ #[ test]
2650
+ fn test_into_boxed_slice ( ) {
2651
+ let xs = vec ! [ 1 u, 2 , 3 ] ;
2652
+ let ys = xs. into_boxed_slice ( ) ;
2653
+ assert_eq ! ( ys. as_slice( ) , [ 1 u, 2 , 3 ] ) ;
2654
+ }
2655
+
2634
2656
#[ bench]
2635
2657
fn bench_new ( b : & mut Bencher ) {
2636
2658
b. iter ( || {
Original file line number Diff line number Diff line change 88
88
#[ doc( no_inline) ] pub use slice:: { MutableCloneableSlice , MutableOrdSlice } ;
89
89
#[ doc( no_inline) ] pub use slice:: { ImmutableSlice , MutableSlice } ;
90
90
#[ doc( no_inline) ] pub use slice:: { ImmutablePartialEqSlice , ImmutableOrdSlice } ;
91
- #[ doc( no_inline) ] pub use slice:: { AsSlice , VectorVector } ;
91
+ #[ doc( no_inline) ] pub use slice:: { AsSlice , VectorVector , BoxedSlice } ;
92
92
#[ doc( no_inline) ] pub use slice:: MutableSliceAllocating ;
93
93
#[ doc( no_inline) ] pub use string:: String ;
94
94
#[ doc( no_inline) ] pub use vec:: Vec ;
You can’t perform that action at this time.
0 commit comments