Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a2f4bc0

Browse files
committed
refactor: moved SpecFromElem to spec_from_elem.rs
1 parent dc46013 commit a2f4bc0

File tree

2 files changed

+64
-55
lines changed

2 files changed

+64
-55
lines changed

library/alloc/src/vec/mod.rs

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ mod source_iter_marker;
105105

106106
mod partial_eq;
107107

108+
use self::spec_from_elem::SpecFromElem;
109+
110+
mod spec_from_elem;
111+
108112
/// A contiguous growable array type, written `Vec<T>` but pronounced 'vector'.
109113
///
110114
/// # Examples
@@ -1995,61 +1999,6 @@ pub fn from_elem_in<T: Clone, A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<
19951999
<T as SpecFromElem>::from_elem(elem, n, alloc)
19962000
}
19972001

1998-
// Specialization trait used for Vec::from_elem
1999-
trait SpecFromElem: Sized {
2000-
fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A>;
2001-
}
2002-
2003-
impl<T: Clone> SpecFromElem for T {
2004-
default fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A> {
2005-
let mut v = Vec::with_capacity_in(n, alloc);
2006-
v.extend_with(n, ExtendElement(elem));
2007-
v
2008-
}
2009-
}
2010-
2011-
impl SpecFromElem for i8 {
2012-
#[inline]
2013-
fn from_elem<A: Allocator>(elem: i8, n: usize, alloc: A) -> Vec<i8, A> {
2014-
if elem == 0 {
2015-
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
2016-
}
2017-
unsafe {
2018-
let mut v = Vec::with_capacity_in(n, alloc);
2019-
ptr::write_bytes(v.as_mut_ptr(), elem as u8, n);
2020-
v.set_len(n);
2021-
v
2022-
}
2023-
}
2024-
}
2025-
2026-
impl SpecFromElem for u8 {
2027-
#[inline]
2028-
fn from_elem<A: Allocator>(elem: u8, n: usize, alloc: A) -> Vec<u8, A> {
2029-
if elem == 0 {
2030-
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
2031-
}
2032-
unsafe {
2033-
let mut v = Vec::with_capacity_in(n, alloc);
2034-
ptr::write_bytes(v.as_mut_ptr(), elem, n);
2035-
v.set_len(n);
2036-
v
2037-
}
2038-
}
2039-
}
2040-
2041-
impl<T: Clone + IsZero> SpecFromElem for T {
2042-
#[inline]
2043-
fn from_elem<A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> {
2044-
if elem.is_zero() {
2045-
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
2046-
}
2047-
let mut v = Vec::with_capacity_in(n, alloc);
2048-
v.extend_with(n, ExtendElement(elem));
2049-
v
2050-
}
2051-
}
2052-
20532002
////////////////////////////////////////////////////////////////////////////////
20542003
// Common trait implementations for Vec
20552004
////////////////////////////////////////////////////////////////////////////////
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use crate::alloc::{Allocator};
2+
use crate::raw_vec::RawVec;
3+
use core::ptr::{self};
4+
5+
use super::{Vec, IsZero, ExtendElement};
6+
7+
// Specialization trait used for Vec::from_elem
8+
pub(super) trait SpecFromElem: Sized {
9+
fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A>;
10+
}
11+
12+
impl<T: Clone> SpecFromElem for T {
13+
default fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A> {
14+
let mut v = Vec::with_capacity_in(n, alloc);
15+
v.extend_with(n, ExtendElement(elem));
16+
v
17+
}
18+
}
19+
20+
impl SpecFromElem for i8 {
21+
#[inline]
22+
fn from_elem<A: Allocator>(elem: i8, n: usize, alloc: A) -> Vec<i8, A> {
23+
if elem == 0 {
24+
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
25+
}
26+
unsafe {
27+
let mut v = Vec::with_capacity_in(n, alloc);
28+
ptr::write_bytes(v.as_mut_ptr(), elem as u8, n);
29+
v.set_len(n);
30+
v
31+
}
32+
}
33+
}
34+
35+
impl SpecFromElem for u8 {
36+
#[inline]
37+
fn from_elem<A: Allocator>(elem: u8, n: usize, alloc: A) -> Vec<u8, A> {
38+
if elem == 0 {
39+
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
40+
}
41+
unsafe {
42+
let mut v = Vec::with_capacity_in(n, alloc);
43+
ptr::write_bytes(v.as_mut_ptr(), elem, n);
44+
v.set_len(n);
45+
v
46+
}
47+
}
48+
}
49+
50+
impl<T: Clone + IsZero> SpecFromElem for T {
51+
#[inline]
52+
fn from_elem<A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> {
53+
if elem.is_zero() {
54+
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
55+
}
56+
let mut v = Vec::with_capacity_in(n, alloc);
57+
v.extend_with(n, ExtendElement(elem));
58+
v
59+
}
60+
}

0 commit comments

Comments
 (0)