Skip to content

Commit

Permalink
Specialize Vec::from_elem for other integer types
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrubeck committed Mar 15, 2017
1 parent a457d67 commit 4961f6c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/libcollections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#![feature(fused)]
#![feature(generic_param_attrs)]
#![feature(heap_api)]
#![feature(i128_type)]
#![feature(inclusive_range)]
#![feature(lang_items)]
#![feature(nonzero)]
Expand Down
32 changes: 32 additions & 0 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,38 @@ impl SpecFromElem for u8 {
}
}

macro_rules! impl_spec_from_elem_int {
($t: ty) => {
impl SpecFromElem for $t {
#[inline]
fn from_elem(elem: $t, n: usize) -> Vec<$t> {
if elem == 0 {
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_int!(i8);
impl_spec_from_elem_int!(i16);
impl_spec_from_elem_int!(i32);
impl_spec_from_elem_int!(i64);
impl_spec_from_elem_int!(i128);
impl_spec_from_elem_int!(isize);

impl_spec_from_elem_int!(u16);
impl_spec_from_elem_int!(u32);
impl_spec_from_elem_int!(u64);
impl_spec_from_elem_int!(u128);
impl_spec_from_elem_int!(usize);

////////////////////////////////////////////////////////////////////////////////
// Common trait implementations for Vec
////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 4961f6c

Please sign in to comment.