18
18
//!
19
19
//! This library allows you to do in-place initialization safely.
20
20
//!
21
- //! ## Nightly Needed for `alloc` and `std` features
21
+ //! ## Nightly Needed for `alloc` feature
22
22
//!
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.
25
27
//!
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.
28
32
//!
29
33
//! # Overview
30
34
//!
239
243
extern crate alloc;
240
244
241
245
#[ 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 ;
245
249
246
250
use core:: {
247
251
cell:: UnsafeCell ,
@@ -1201,32 +1205,45 @@ pub trait InPlaceInit<T>: Sized {
1201
1205
}
1202
1206
1203
1207
#[ 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" ) ) ]
1204
1221
impl < T > InPlaceInit < T > for Box < T > {
1205
1222
#[ inline]
1206
1223
fn try_pin_init < E > ( init : impl PinInit < T , E > ) -> Result < Pin < Self > , E >
1207
1224
where
1208
1225
E : From < AllocError > ,
1209
1226
{
1210
- Box :: try_new_uninit ( ) ? . write_pin_init ( init)
1227
+ try_new_uninit ! ( Box ) . write_pin_init ( init)
1211
1228
}
1212
1229
1213
1230
#[ inline]
1214
1231
fn try_init < E > ( init : impl Init < T , E > ) -> Result < Self , E >
1215
1232
where
1216
1233
E : From < AllocError > ,
1217
1234
{
1218
- Box :: try_new_uninit ( ) ? . write_init ( init)
1235
+ try_new_uninit ! ( Box ) . write_init ( init)
1219
1236
}
1220
1237
}
1221
1238
1222
- #[ cfg( feature = "alloc" ) ]
1239
+ #[ cfg( any ( feature = "std" , feature = " alloc") ) ]
1223
1240
impl < T > InPlaceInit < T > for Arc < T > {
1224
1241
#[ inline]
1225
1242
fn try_pin_init < E > ( init : impl PinInit < T , E > ) -> Result < Pin < Self > , E >
1226
1243
where
1227
1244
E : From < AllocError > ,
1228
1245
{
1229
- let mut this = Arc :: try_new_uninit ( ) ? ;
1246
+ let mut this = try_new_uninit ! ( Arc ) ;
1230
1247
let Some ( slot) = Arc :: get_mut ( & mut this) else {
1231
1248
// SAFETY: the Arc has just been created and has no external referecnes
1232
1249
unsafe { core:: hint:: unreachable_unchecked ( ) }
@@ -1244,7 +1261,7 @@ impl<T> InPlaceInit<T> for Arc<T> {
1244
1261
where
1245
1262
E : From < AllocError > ,
1246
1263
{
1247
- let mut this = Arc :: try_new_uninit ( ) ? ;
1264
+ let mut this = try_new_uninit ! ( Arc ) ;
1248
1265
let Some ( slot) = Arc :: get_mut ( & mut this) else {
1249
1266
// SAFETY: the Arc has just been created and has no external referecnes
1250
1267
unsafe { core:: hint:: unreachable_unchecked ( ) }
0 commit comments