@@ -77,6 +77,8 @@ use core::hash::{self, Hash};
7777use core:: intrinsics:: { arith_offset, assume} ;
7878use core:: iter:: { FromIterator , FusedIterator , TrustedLen } ;
7979use core:: mem;
80+ #[ cfg( not( test) ) ]
81+ use core:: num:: Float ;
8082use core:: ops:: { InPlace , Index , IndexMut , Place , Placer } ;
8183use core:: ops;
8284use core:: ptr;
@@ -1404,6 +1406,41 @@ impl SpecFromElem for u8 {
14041406 }
14051407}
14061408
1409+ macro_rules! impl_spec_from_elem {
1410+ ( $t: ty, $is_zero: expr) => {
1411+ impl SpecFromElem for $t {
1412+ #[ inline]
1413+ fn from_elem( elem: $t, n: usize ) -> Vec <$t> {
1414+ if $is_zero( elem) {
1415+ return Vec {
1416+ buf: RawVec :: with_capacity_zeroed( n) ,
1417+ len: n,
1418+ }
1419+ }
1420+ let mut v = Vec :: with_capacity( n) ;
1421+ v. extend_with_element( n, elem) ;
1422+ v
1423+ }
1424+ }
1425+ } ;
1426+ }
1427+
1428+ impl_spec_from_elem ! ( i8 , |x| x == 0 ) ;
1429+ impl_spec_from_elem ! ( i16 , |x| x == 0 ) ;
1430+ impl_spec_from_elem ! ( i32 , |x| x == 0 ) ;
1431+ impl_spec_from_elem ! ( i64 , |x| x == 0 ) ;
1432+ impl_spec_from_elem ! ( i128 , |x| x == 0 ) ;
1433+ impl_spec_from_elem ! ( isize , |x| x == 0 ) ;
1434+
1435+ impl_spec_from_elem ! ( u16 , |x| x == 0 ) ;
1436+ impl_spec_from_elem ! ( u32 , |x| x == 0 ) ;
1437+ impl_spec_from_elem ! ( u64 , |x| x == 0 ) ;
1438+ impl_spec_from_elem ! ( u128 , |x| x == 0 ) ;
1439+ impl_spec_from_elem ! ( usize , |x| x == 0 ) ;
1440+
1441+ impl_spec_from_elem ! ( f32 , |x: f32 | x == 0. && x. is_sign_positive( ) ) ;
1442+ impl_spec_from_elem ! ( f64 , |x: f64 | x == 0. && x. is_sign_positive( ) ) ;
1443+
14071444////////////////////////////////////////////////////////////////////////////////
14081445// Common trait implementations for Vec
14091446////////////////////////////////////////////////////////////////////////////////
0 commit comments