Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1632165

Browse files
Rollup merge ofrust-lang#125995 - kpreid:const-uninit-stable, r=Nilstrieb
Use inline const blocks to create arrays of `MaybeUninit`.This PR contains 2 changes enabled by the fact that [`inline_const` is now stable](rust-lang#104087), and was split out ofrust-lang#125082.1. Use inline const instead of `unsafe` to construct arrays in `MaybeUninit` examples. Rationale: Demonstrate good practice of avoiding `unsafe` code where it is not strictly necessary.4. Use inline const instead of `unsafe` to implement `MaybeUninit::uninit_array()`. This is arguably giving the compiler more work to do, in exchange for eliminating just one single internal unsafe block, so it's less certain that this is good on net.r​? `@Nilstrieb`
2 parents2c5f1ac +14a6f29 commit1632165

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

‎core/src/mem/maybe_uninit.rs‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,8 @@ use crate::slice;
120120
/// use std::mem::{self, MaybeUninit};
121121
///
122122
/// let data = {
123-
/// // Create an uninitialized array of `MaybeUninit`. The `assume_init` is
124-
/// // safe because the type we are claiming to have initialized here is a
125-
/// // bunch of `MaybeUninit`s, which do not require initialization.
126-
/// let mut data: [MaybeUninit<Vec<u32>>; 1000] = unsafe {
127-
/// MaybeUninit::uninit().assume_init()
128-
/// };
123+
/// // Create an uninitialized array of `MaybeUninit`.
124+
/// let mut data: [MaybeUninit<Vec<u32>>; 1000] = [const { MaybeUninit::uninit() }; 1000];
129125
///
130126
/// // Dropping a `MaybeUninit` does nothing, so if there is a panic during this loop,
131127
/// // we have a memory leak, but there is no memory safety issue.
@@ -147,10 +143,8 @@ use crate::slice;
147143
/// ```
148144
/// use std::mem::MaybeUninit;
149145
///
150-
/// // Create an uninitialized array of `MaybeUninit`. The `assume_init` is
151-
/// // safe because the type we are claiming to have initialized here is a
152-
/// // bunch of `MaybeUninit`s, which do not require initialization.
153-
/// let mut data: [MaybeUninit<String>; 1000] = unsafe { MaybeUninit::uninit().assume_init() };
146+
/// // Create an uninitialized array of `MaybeUninit`.
147+
/// let mut data: [MaybeUninit<String>; 1000] = [const { MaybeUninit::uninit() }; 1000];
154148
/// // Count the number of elements we have assigned.
155149
/// let mut data_len: usize = 0;
156150
///
@@ -348,8 +342,7 @@ impl<T> MaybeUninit<T> {
348342
#[must_use]
349343
#[inline(always)]
350344
pubconstfnuninit_array<constN:usize>() ->[Self;N]{
351-
// SAFETY: An uninitialized `[MaybeUninit<_>; LEN]` is valid.
352-
unsafe{MaybeUninit::<[MaybeUninit<T>;N]>::uninit().assume_init()}
345+
[const{MaybeUninit::uninit()};N]
353346
}
354347

355348
/// Creates a new `MaybeUninit<T>` in an uninitialized state, with the memory being

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp