refactor: moved SpecFromElem to spec_from_elem.rs
This commit is contained in:
parent
dc46013248
commit
a2f4bc0d18
@ -105,6 +105,10 @@ mod source_iter_marker;
|
||||
|
||||
mod partial_eq;
|
||||
|
||||
use self::spec_from_elem::SpecFromElem;
|
||||
|
||||
mod spec_from_elem;
|
||||
|
||||
/// A contiguous growable array type, written `Vec<T>` but pronounced 'vector'.
|
||||
///
|
||||
/// # Examples
|
||||
@ -1995,61 +1999,6 @@ pub fn from_elem_in<T: Clone, A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<
|
||||
<T as SpecFromElem>::from_elem(elem, n, alloc)
|
||||
}
|
||||
|
||||
// Specialization trait used for Vec::from_elem
|
||||
trait SpecFromElem: Sized {
|
||||
fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A>;
|
||||
}
|
||||
|
||||
impl<T: Clone> SpecFromElem for T {
|
||||
default fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A> {
|
||||
let mut v = Vec::with_capacity_in(n, alloc);
|
||||
v.extend_with(n, ExtendElement(elem));
|
||||
v
|
||||
}
|
||||
}
|
||||
|
||||
impl SpecFromElem for i8 {
|
||||
#[inline]
|
||||
fn from_elem<A: Allocator>(elem: i8, n: usize, alloc: A) -> Vec<i8, A> {
|
||||
if elem == 0 {
|
||||
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
|
||||
}
|
||||
unsafe {
|
||||
let mut v = Vec::with_capacity_in(n, alloc);
|
||||
ptr::write_bytes(v.as_mut_ptr(), elem as u8, n);
|
||||
v.set_len(n);
|
||||
v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SpecFromElem for u8 {
|
||||
#[inline]
|
||||
fn from_elem<A: Allocator>(elem: u8, n: usize, alloc: A) -> Vec<u8, A> {
|
||||
if elem == 0 {
|
||||
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
|
||||
}
|
||||
unsafe {
|
||||
let mut v = Vec::with_capacity_in(n, alloc);
|
||||
ptr::write_bytes(v.as_mut_ptr(), elem, n);
|
||||
v.set_len(n);
|
||||
v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone + IsZero> SpecFromElem for T {
|
||||
#[inline]
|
||||
fn from_elem<A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> {
|
||||
if elem.is_zero() {
|
||||
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
|
||||
}
|
||||
let mut v = Vec::with_capacity_in(n, alloc);
|
||||
v.extend_with(n, ExtendElement(elem));
|
||||
v
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Common trait implementations for Vec
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
60
library/alloc/src/vec/spec_from_elem.rs
Normal file
60
library/alloc/src/vec/spec_from_elem.rs
Normal file
@ -0,0 +1,60 @@
|
||||
use crate::alloc::{Allocator};
|
||||
use crate::raw_vec::RawVec;
|
||||
use core::ptr::{self};
|
||||
|
||||
use super::{Vec, IsZero, ExtendElement};
|
||||
|
||||
// Specialization trait used for Vec::from_elem
|
||||
pub(super) trait SpecFromElem: Sized {
|
||||
fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A>;
|
||||
}
|
||||
|
||||
impl<T: Clone> SpecFromElem for T {
|
||||
default fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A> {
|
||||
let mut v = Vec::with_capacity_in(n, alloc);
|
||||
v.extend_with(n, ExtendElement(elem));
|
||||
v
|
||||
}
|
||||
}
|
||||
|
||||
impl SpecFromElem for i8 {
|
||||
#[inline]
|
||||
fn from_elem<A: Allocator>(elem: i8, n: usize, alloc: A) -> Vec<i8, A> {
|
||||
if elem == 0 {
|
||||
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
|
||||
}
|
||||
unsafe {
|
||||
let mut v = Vec::with_capacity_in(n, alloc);
|
||||
ptr::write_bytes(v.as_mut_ptr(), elem as u8, n);
|
||||
v.set_len(n);
|
||||
v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SpecFromElem for u8 {
|
||||
#[inline]
|
||||
fn from_elem<A: Allocator>(elem: u8, n: usize, alloc: A) -> Vec<u8, A> {
|
||||
if elem == 0 {
|
||||
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
|
||||
}
|
||||
unsafe {
|
||||
let mut v = Vec::with_capacity_in(n, alloc);
|
||||
ptr::write_bytes(v.as_mut_ptr(), elem, n);
|
||||
v.set_len(n);
|
||||
v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone + IsZero> SpecFromElem for T {
|
||||
#[inline]
|
||||
fn from_elem<A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> {
|
||||
if elem.is_zero() {
|
||||
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
|
||||
}
|
||||
let mut v = Vec::with_capacity_in(n, alloc);
|
||||
v.extend_with(n, ExtendElement(elem));
|
||||
v
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user