Skip to content

Commit 1a823d1

Browse files
bonziniBennoLossin
authored andcommitted
lib: make "std" independent of allocator_api
When compiling without allocator_api, assume that allocations cannot fail. This way, nightly Rust features are not absolutely needed for pinned_init, and it can be used with stable Rust; right now the minimum supported Rust version is 1.82.0, where the new_uninit version was stabilized. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent d286b7e commit 1a823d1

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ paste = "1.0"
1818
pinned-init-macro = { path = "./pinned-init-macro", version = "=0.0.5" }
1919

2020
[features]
21-
default = ["std"]
22-
std = ["alloc"]
21+
default = ["std", "alloc"]
22+
std = []
2323
alloc = []
2424

2525
[dev-dependencies]

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ it into it's final memory location.
2626

2727
This library allows you to do in-place initialization safely.
2828

29-
### Nightly Needed for `alloc` and `std` features
29+
### Nightly Needed for `alloc` feature
3030

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.
31+
This library requires the `allocator_api` unstable feature when the `alloc` feature
32+
is enabled and thus this feature can only be used with a nightly compiler.
33+
When enabling the `alloc` feature, the user will be required to activate
34+
`allocator_api` as well.
3335

34-
When enabling the `alloc` or `std` feature, the user will be required to activate `allocator_api`
35-
as well.
36+
The feature is enabled by default, thus by default `pinned-init` will require a
37+
nightly compiler. However, using the crate on stable compilers is possible by
38+
disabling `alloc`. In practice this will require the `std` feature, because
39+
stable compilers have neither `Box` nor `Arc` in no-std mode.
3640

3741
## Overview
3842

src/lib.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
//!
1919
//! This library allows you to do in-place initialization safely.
2020
//!
21-
//! ## Nightly Needed for `alloc` and `std` features
21+
//! ## Nightly Needed for `alloc` feature
2222
//!
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.
23+
//! This library requires the `allocator_api` unstable feature when the `alloc` feature
24+
//! is enabled and thus this feature can only be used with a nightly compiler.
25+
//! When enabling the `alloc` feature, the user will be required to activate
26+
//! `allocator_api` as well.
2527
//!
26-
//! When enabling the `alloc` or `std` feature, the user will be required to activate `allocator_api`
27-
//! as well.
28+
//! The feature is enabled by default, thus by default `pinned-init` will require a
29+
//! nightly compiler. However, using the crate on stable compilers is possible by
30+
//! disabling `alloc`. In practice this will require the `std` feature, because
31+
//! stable compilers have neither `Box` nor `Arc` in no-std mode.
2832
//!
2933
//! # Overview
3034
//!
@@ -239,9 +243,9 @@
239243
extern crate alloc;
240244

241245
#[cfg(all(feature = "alloc", not(feature = "std")))]
242-
use alloc::boxed::Box;
243-
#[cfg(feature = "alloc")]
244-
use alloc::sync::Arc;
246+
use alloc::{boxed::Box, sync::Arc};
247+
#[cfg(feature = "std")]
248+
use std::sync::Arc;
245249

246250
use core::{
247251
cell::UnsafeCell,
@@ -1201,32 +1205,45 @@ pub trait InPlaceInit<T>: Sized {
12011205
}
12021206

12031207
#[cfg(feature = "alloc")]
1208+
macro_rules! try_new_uninit {
1209+
($type:ident) => {
1210+
$type::try_new_uninit()?
1211+
};
1212+
}
1213+
#[cfg(all(feature = "std", not(feature = "alloc")))]
1214+
macro_rules! try_new_uninit {
1215+
($type:ident) => {
1216+
$type::new_uninit()
1217+
};
1218+
}
1219+
1220+
#[cfg(any(feature = "std", feature = "alloc"))]
12041221
impl<T> InPlaceInit<T> for Box<T> {
12051222
#[inline]
12061223
fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E>
12071224
where
12081225
E: From<AllocError>,
12091226
{
1210-
Box::try_new_uninit()?.write_pin_init(init)
1227+
try_new_uninit!(Box).write_pin_init(init)
12111228
}
12121229

12131230
#[inline]
12141231
fn try_init<E>(init: impl Init<T, E>) -> Result<Self, E>
12151232
where
12161233
E: From<AllocError>,
12171234
{
1218-
Box::try_new_uninit()?.write_init(init)
1235+
try_new_uninit!(Box).write_init(init)
12191236
}
12201237
}
12211238

1222-
#[cfg(feature = "alloc")]
1239+
#[cfg(any(feature = "std", feature = "alloc"))]
12231240
impl<T> InPlaceInit<T> for Arc<T> {
12241241
#[inline]
12251242
fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E>
12261243
where
12271244
E: From<AllocError>,
12281245
{
1229-
let mut this = Arc::try_new_uninit()?;
1246+
let mut this = try_new_uninit!(Arc);
12301247
let Some(slot) = Arc::get_mut(&mut this) else {
12311248
// SAFETY: the Arc has just been created and has no external referecnes
12321249
unsafe { core::hint::unreachable_unchecked() }
@@ -1244,7 +1261,7 @@ impl<T> InPlaceInit<T> for Arc<T> {
12441261
where
12451262
E: From<AllocError>,
12461263
{
1247-
let mut this = Arc::try_new_uninit()?;
1264+
let mut this = try_new_uninit!(Arc);
12481265
let Some(slot) = Arc::get_mut(&mut this) else {
12491266
// SAFETY: the Arc has just been created and has no external referecnes
12501267
unsafe { core::hint::unreachable_unchecked() }

0 commit comments

Comments
 (0)