3 use crate::alloc::Allocator;
4 use crate::raw_vec::RawVec;
6 use super::{ExtendElement, IsZero, Vec};
8 // Specialization trait used for Vec::from_elem
9 pub(super) trait SpecFromElem: Sized {
10 fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A>;
13 impl<T: Clone> SpecFromElem for T {
14 default fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A> {
15 let mut v = Vec::with_capacity_in(n, alloc);
16 v.extend_with(n, ExtendElement(elem));
21 impl<T: Clone + IsZero> SpecFromElem for T {
23 default fn from_elem<A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> {
25 return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
27 let mut v = Vec::with_capacity_in(n, alloc);
28 v.extend_with(n, ExtendElement(elem));
33 impl SpecFromElem for i8 {
35 fn from_elem<A: Allocator>(elem: i8, n: usize, alloc: A) -> Vec<i8, A> {
37 return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
40 let mut v = Vec::with_capacity_in(n, alloc);
41 ptr::write_bytes(v.as_mut_ptr(), elem as u8, n);
48 impl SpecFromElem for u8 {
50 fn from_elem<A: Allocator>(elem: u8, n: usize, alloc: A) -> Vec<u8, A> {
52 return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
55 let mut v = Vec::with_capacity_in(n, alloc);
56 ptr::write_bytes(v.as_mut_ptr(), elem, n);