Specialize Vec::from_elem for other numeric types
This commit is contained in:
parent
675475c4d3
commit
aad2062073
2 changed files with 39 additions and 0 deletions
|
|
@ -35,6 +35,7 @@
|
|||
#![feature(box_patterns)]
|
||||
#![feature(box_syntax)]
|
||||
#![cfg_attr(not(test), feature(char_escape_debug))]
|
||||
#![cfg_attr(not(test), feature(core_float))]
|
||||
#![feature(core_intrinsics)]
|
||||
#![feature(dropck_eyepatch)]
|
||||
#![feature(exact_size_is_empty)]
|
||||
|
|
@ -42,6 +43,7 @@
|
|||
#![feature(fused)]
|
||||
#![feature(generic_param_attrs)]
|
||||
#![feature(heap_api)]
|
||||
#![feature(i128_type)]
|
||||
#![feature(inclusive_range)]
|
||||
#![feature(lang_items)]
|
||||
#![feature(manually_drop)]
|
||||
|
|
|
|||
|
|
@ -77,6 +77,8 @@ use core::hash::{self, Hash};
|
|||
use core::intrinsics::{arith_offset, assume};
|
||||
use core::iter::{FromIterator, FusedIterator, TrustedLen};
|
||||
use core::mem;
|
||||
#[cfg(not(test))]
|
||||
use core::num::Float;
|
||||
use core::ops::{InPlace, Index, IndexMut, Place, Placer};
|
||||
use core::ops;
|
||||
use core::ptr;
|
||||
|
|
@ -1404,6 +1406,41 @@ impl SpecFromElem for u8 {
|
|||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_spec_from_elem {
|
||||
($t: ty, $is_zero: expr) => {
|
||||
impl SpecFromElem for $t {
|
||||
#[inline]
|
||||
fn from_elem(elem: $t, n: usize) -> Vec<$t> {
|
||||
if $is_zero(elem) {
|
||||
return Vec {
|
||||
buf: RawVec::with_capacity_zeroed(n),
|
||||
len: n,
|
||||
}
|
||||
}
|
||||
let mut v = Vec::with_capacity(n);
|
||||
v.extend_with_element(n, elem);
|
||||
v
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_spec_from_elem!(i8, |x| x == 0);
|
||||
impl_spec_from_elem!(i16, |x| x == 0);
|
||||
impl_spec_from_elem!(i32, |x| x == 0);
|
||||
impl_spec_from_elem!(i64, |x| x == 0);
|
||||
impl_spec_from_elem!(i128, |x| x == 0);
|
||||
impl_spec_from_elem!(isize, |x| x == 0);
|
||||
|
||||
impl_spec_from_elem!(u16, |x| x == 0);
|
||||
impl_spec_from_elem!(u32, |x| x == 0);
|
||||
impl_spec_from_elem!(u64, |x| x == 0);
|
||||
impl_spec_from_elem!(u128, |x| x == 0);
|
||||
impl_spec_from_elem!(usize, |x| x == 0);
|
||||
|
||||
impl_spec_from_elem!(f32, |x: f32| x == 0. && x.is_sign_positive());
|
||||
impl_spec_from_elem!(f64, |x: f64| x == 0. && x.is_sign_positive());
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Common trait implementations for Vec
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue