Skip to content

Commit da5a88a

Browse files
bonziniBennoLossin
authored andcommitted
do not use get_mut_unchecked
Instead of using an unstable feature use unreachable_unchecked() to enable optimization. Suggested-by: Benno Lossin <benno.lossin@proton.me> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 23a1238 commit da5a88a

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ This library allows you to do in-place initialization safely.
2828

2929
### Nightly Needed for `alloc` and `std` features
3030

31-
This library requires unstable features when the `alloc` or `std` features are enabled and thus
32-
can only be used with a nightly compiler. The internally used features are:
33-
- `allocator_api`
34-
- `get_mut_unchecked`
31+
This library requires the `allocator_api` unstable feature when the `alloc` or `std` features
32+
are enabled and thus can only be used with a nightly compiler.
3533

36-
When enabling the `alloc` or `std` feature, the user will be required to activate these features:
37-
- `allocator_api`
34+
When enabling the `alloc` or `std` feature, the user will be required to activate `allocator_api`
35+
as well.
3836

3937
## Overview
4038

src/lib.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020
//!
2121
//! ## Nightly Needed for `alloc` and `std` features
2222
//!
23-
//! This library requires unstable features when the `alloc` or `std` features are enabled and thus
24-
//! can only be used with a nightly compiler. The internally used features are:
25-
//! - `allocator_api`
26-
//! - `get_mut_unchecked`
23+
//! This library requires the `allocator_api` unstable feature when the `alloc` or `std` features
24+
//! are enabled and thus can only be used with a nightly compiler.
2725
//!
28-
//! When enabling the `alloc` or `std` feature, the user will be required to activate these features:
29-
//! - `allocator_api`
26+
//! When enabling the `alloc` or `std` feature, the user will be required to activate `allocator_api`
27+
//! as well.
3028
//!
3129
//! # Overview
3230
//!
@@ -236,7 +234,6 @@
236234
#![forbid(missing_docs, unsafe_op_in_unsafe_fn)]
237235
#![cfg_attr(not(feature = "std"), no_std)]
238236
#![cfg_attr(feature = "alloc", feature(allocator_api))]
239-
#![cfg_attr(feature = "alloc", feature(get_mut_unchecked))]
240237

241238
#[cfg(feature = "alloc")]
242239
extern crate alloc;
@@ -1226,7 +1223,10 @@ impl<T> InPlaceInit<T> for Arc<T> {
12261223
E: From<AllocError>,
12271224
{
12281225
let mut this = Arc::try_new_uninit()?;
1229-
let slot = unsafe { Arc::get_mut_unchecked(&mut this) };
1226+
let Some(slot) = Arc::get_mut(&mut this) else {
1227+
// SAFETY: the Arc has just been created and has no external referecnes
1228+
unsafe { core::hint::unreachable_unchecked() }
1229+
};
12301230
let slot = slot.as_mut_ptr();
12311231
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
12321232
// slot is valid and will not be moved, because we pin it later.
@@ -1241,7 +1241,10 @@ impl<T> InPlaceInit<T> for Arc<T> {
12411241
E: From<AllocError>,
12421242
{
12431243
let mut this = Arc::try_new_uninit()?;
1244-
let slot = unsafe { Arc::get_mut_unchecked(&mut this) };
1244+
let Some(slot) = Arc::get_mut(&mut this) else {
1245+
// SAFETY: the Arc has just been created and has no external referecnes
1246+
unsafe { core::hint::unreachable_unchecked() }
1247+
};
12451248
let slot = slot.as_mut_ptr();
12461249
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
12471250
// slot is valid.

0 commit comments

Comments
 (0)