summaryrefslogtreecommitdiffstats
path: root/library/alloc/src/vec/spec_from_elem.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /library/alloc/src/vec/spec_from_elem.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--library/alloc/src/vec/spec_from_elem.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/library/alloc/src/vec/spec_from_elem.rs b/library/alloc/src/vec/spec_from_elem.rs
new file mode 100644
index 000000000..ff364c033
--- /dev/null
+++ b/library/alloc/src/vec/spec_from_elem.rs
@@ -0,0 +1,61 @@
+use core::ptr;
+
+use crate::alloc::Allocator;
+use crate::raw_vec::RawVec;
+
+use super::{ExtendElement, IsZero, Vec};
+
+// 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<T: Clone + IsZero> SpecFromElem for T {
+ #[inline]
+ default 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
+ }
+}
+
+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
+ }
+ }
+}