diff options
| author | bors <bors@rust-lang.org> | 2023-11-21 08:40:39 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-11-21 08:40:39 +0000 |
| commit | c5af0610196bc39c44e54f1e08bd9149bdc66929 (patch) | |
| tree | 86d214f395b4d0a2c4ecb44d12b53e67a37c458b /library/alloc/src/vec/spec_from_elem.rs | |
| parent | 85c42b751e65bd77044163e35a3b73ed7d15bec3 (diff) | |
| parent | f13f9809003665fb323ee715102ca8a5914810e8 (diff) | |
| download | rust-c5af0610196bc39c44e54f1e08bd9149bdc66929.tar.gz rust-c5af0610196bc39c44e54f1e08bd9149bdc66929.zip | |
Auto merge of #118126 - Nilstrieb:rollup-5ogh896, r=Nilstrieb
Rollup of 7 pull requests Successful merges: - #117790 (CFI: Add missing use core::ffi::c_int) - #118059 (Explicitly unset $CARGO for compiletest) - #118081 (`rustc_ty_utils` cleanups) - #118094 (feat: specialize `SpecFromElem` for `()`) - #118097 (Update books) - #118115 (Fix occurrences of old fn names in comment and tracing) - #118121 (`rustc_hir` cleanups) r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'library/alloc/src/vec/spec_from_elem.rs')
| -rw-r--r-- | library/alloc/src/vec/spec_from_elem.rs | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/library/alloc/src/vec/spec_from_elem.rs b/library/alloc/src/vec/spec_from_elem.rs index da43d17bf36..01a6db14474 100644 --- a/library/alloc/src/vec/spec_from_elem.rs +++ b/library/alloc/src/vec/spec_from_elem.rs @@ -36,12 +36,12 @@ impl SpecFromElem for i8 { if elem == 0 { return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; } + let mut v = Vec::with_capacity_in(n, alloc); 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 } + v } } @@ -51,11 +51,26 @@ impl SpecFromElem for u8 { if elem == 0 { return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; } + let mut v = Vec::with_capacity_in(n, alloc); unsafe { - let mut v = Vec::with_capacity_in(n, alloc); ptr::write_bytes(v.as_mut_ptr(), elem, n); v.set_len(n); - v } + v + } +} + +// A better way would be to implement this for all ZSTs which are `Copy` and have trivial `Clone` +// but the latter cannot be detected currently +impl SpecFromElem for () { + #[inline] + fn from_elem<A: Allocator>(_elem: (), n: usize, alloc: A) -> Vec<(), A> { + let mut v = Vec::with_capacity_in(n, alloc); + // SAFETY: the capacity has just been set to `n` + // and `()` is a ZST with trivial `Clone` implementation + unsafe { + v.set_len(n); + } + v } } |
