11use core:: any:: Any ;
2- #[ cfg( not( no_global_oom_handling) ) ]
3- use core:: clone:: TrivialClone ;
42use core:: error:: Error ;
3+ #[ cfg( not( no_global_oom_handling) ) ]
4+ use core:: fmt;
55use core:: mem;
66use core:: pin:: Pin ;
7- #[ cfg( not( no_global_oom_handling) ) ]
8- use core:: { fmt, ptr} ;
97
108use crate :: alloc:: Allocator ;
119#[ cfg( not( no_global_oom_handling) ) ]
1210use crate :: borrow:: Cow ;
1311use crate :: boxed:: Box ;
1412#[ cfg( not( no_global_oom_handling) ) ]
15- use crate :: raw_vec:: RawVec ;
16- #[ cfg( not( no_global_oom_handling) ) ]
17- use crate :: str:: from_boxed_utf8_unchecked;
18- #[ cfg( not( no_global_oom_handling) ) ]
1913use crate :: string:: String ;
2014#[ cfg( not( no_global_oom_handling) ) ]
2115use crate :: vec:: Vec ;
6256 }
6357}
6458
65- /// Specialization trait used for `From<&[T]>`.
66- #[ cfg( not( no_global_oom_handling) ) ]
67- trait BoxFromSlice < T > {
68- fn from_slice ( slice : & [ T ] ) -> Self ;
69- }
70-
71- #[ cfg( not( no_global_oom_handling) ) ]
72- impl < T : Clone > BoxFromSlice < T > for Box < [ T ] > {
73- #[ inline]
74- default fn from_slice ( slice : & [ T ] ) -> Self {
75- slice. to_vec ( ) . into_boxed_slice ( )
76- }
77- }
78-
79- #[ cfg( not( no_global_oom_handling) ) ]
80- impl < T : TrivialClone > BoxFromSlice < T > for Box < [ T ] > {
81- #[ inline]
82- fn from_slice ( slice : & [ T ] ) -> Self {
83- let len = slice. len ( ) ;
84- let buf = RawVec :: with_capacity ( len) ;
85- // SAFETY: since `T` implements `TrivialClone`, this is sound and
86- // equivalent to the above.
87- unsafe {
88- ptr:: copy_nonoverlapping ( slice. as_ptr ( ) , buf. ptr ( ) , len) ;
89- buf. into_box ( slice. len ( ) ) . assume_init ( )
90- }
91- }
92- }
93-
9459#[ cfg( not( no_global_oom_handling) ) ]
9560#[ stable( feature = "box_from_slice" , since = "1.17.0" ) ]
9661impl < T : Clone > From < & [ T ] > for Box < [ T ] > {
@@ -109,7 +74,7 @@ impl<T: Clone> From<&[T]> for Box<[T]> {
10974 /// ```
11075 #[ inline]
11176 fn from ( slice : & [ T ] ) -> Box < [ T ] > {
112- < Self as BoxFromSlice < T > > :: from_slice ( slice)
77+ Box :: clone_from_ref ( slice)
11378 }
11479}
11580
@@ -170,7 +135,7 @@ impl From<&str> for Box<str> {
170135 /// ```
171136 #[ inline]
172137 fn from ( s : & str ) -> Box < str > {
173- unsafe { from_boxed_utf8_unchecked ( Box :: from ( s . as_bytes ( ) ) ) }
138+ Box :: clone_from_ref ( s )
174139 }
175140}
176141
0 commit comments