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//!
239243extern 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
246250use 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" ) ) ]
12041221impl < 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") ) ]
12231240impl < 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