22
33//! Library to safely and fallibly initialize pinned `struct`s using in-place constructors.
44//!
5+ //! [Pinning][pinning] is Rust's way of ensuring data does not move.
6+ //!
57//! It also allows in-place initialization of big `struct`s that would otherwise produce a stack
68//! overflow.
79//!
1921//! # Nightly only
2022//!
2123//! This library requires unstable features and thus can only be used with a nightly compiler.
22- //! The used features are:
24+ //! The internally used features are:
2325//! - `allocator_api`
2426//! - `new_uninit` (only if the `alloc` or `std` features are enabled)
2527//! - `get_mut_unchecked` (only if the `alloc` or `std` features are enabled)
4547//!
4648//! # Examples
4749//!
48- //! Throught some examples we will make use of the `CMutex` type which can be found in the examples
49- //! directory of the repository. It is essentially a rebuild of the `mutex` from the Linux kernel
50- //! in userland. So it also uses a wait list and a basic spinlock. Importantly it needs to be
51- //! pinned to be locked and thus is a prime candidate for this library.
50+ //! Throught some examples we will make use of the `CMutex` type which can be found in
51+ //! `../examples/mutex.rs`. It is essentially a rebuild of the `mutex` from the Linux kernel in userland. So
52+ //! it also uses a wait list and a basic spinlock. Importantly it needs to be pinned to be locked
53+ //! and thus is a prime candidate for using this library.
5254//!
5355//! ## Using the [`pin_init!`] macro
5456//!
217219//! }
218220//! ```
219221//!
220- //! For more information on how to use [`pin_init_from_closure()`], you can take a look at the
221- //! uses inside the `kernel` crate from the [Rust-for-Linux] project. The `sync` module is a good
222- //! starting point.
222+ //! For more information on how to use [`pin_init_from_closure()`], take a look at the uses inside
223+ //! the `kernel` crate. The [`sync`] module is a good starting point.
223224//!
225+ //! [`sync`]: https://github.com/Rust-for-Linux/linux/tree/rust-next/rust/kernel/sync
226+ //! [pinning]: https://doc.rust-lang.org/std/pin/index.html
224227//! [structurally pinned fields]:
225228//! https://doc.rust-lang.org/std/pin/index.html#pinning-is-structural-for-field
226229//! [stack]: crate::stack_pin_init
@@ -551,6 +554,9 @@ macro_rules! stack_try_pin_init {
551554/// - Fields that you want to initialize in-place have to use `<-` instead of `:`.
552555/// - In front of the initializer you can write `&this in` to have access to a [`NonNull<Self>`]
553556/// pointer named `this` inside of the initializer.
557+ /// - Using struct update syntax one can place `..Zeroable::zeroed()` at the very end of the
558+ /// struct, this initializes every field with 0 and then runs all initializers specified in the
559+ /// body. This can only be done if [`Zeroable`] is implemented for the struct.
554560///
555561/// For instance:
556562///
@@ -575,8 +581,6 @@ macro_rules! stack_try_pin_init {
575581/// ```
576582///
577583/// [`NonNull<Self>`]: core::ptr::NonNull
578- // For a detailed example of how this macro works, see the module documentation of the hidden
579- // module `__internal` inside of `__internal.rs`.
580584#[ macro_export]
581585macro_rules! pin_init {
582586 ( $( & $this: ident in) ? $t: ident $( :: <$( $generics: ty) ,* $( , ) ?>) ? {
@@ -625,8 +629,6 @@ macro_rules! pin_init {
625629/// }
626630/// # let _ = Box::pin_init(BigBuf::new());
627631/// ```
628- // For a detailed example of how this macro works, see the module documentation of the hidden
629- // module `__internal` inside of `__internal.rs`.
630632#[ macro_export]
631633macro_rules! try_pin_init {
632634 ( $( & $this: ident in) ? $t: ident $( :: <$( $generics: ty) ,* $( , ) ?>) ? {
@@ -658,6 +660,7 @@ macro_rules! try_pin_init {
658660///
659661/// This initializer is for initializing data in-place that might later be moved. If you want to
660662/// pin-initialize, use [`pin_init!`].
663+ ///
661664/// # Examples
662665///
663666/// ```rust
@@ -679,8 +682,6 @@ macro_rules! try_pin_init {
679682/// }
680683/// # let _ = Box::init(BigBuf::new());
681684/// ```
682- // For a detailed example of how this macro works, see the module documentation of the hidden
683- // module `__internal` inside of `__internal.rs`.
684685#[ macro_export]
685686macro_rules! init {
686687 ( $( & $this: ident in) ? $t: ident $( :: <$( $generics: ty) ,* $( , ) ?>) ? {
@@ -726,8 +727,6 @@ macro_rules! init {
726727/// }
727728/// # let _ = Box::init(BigBuf::new());
728729/// ```
729- // For a detailed example of how this macro works, see the module documentation of the hidden
730- // module `__internal` inside of `__internal.rs`.
731730#[ macro_export]
732731macro_rules! try_init {
733732 ( $( & $this: ident in) ? $t: ident $( :: <$( $generics: ty) ,* $( , ) ?>) ? {
@@ -750,16 +749,16 @@ macro_rules! try_init {
750749///
751750/// To use this initializer, you will need a suitable memory location that can hold a `T`. This can
752751/// be [`Box<T>`], [`Arc<T>`] or even the stack (see [`stack_pin_init!`]). Use the
753- /// [`InPlaceInit::pin_init `] function of a smart pointer like [`Arc<T>`] on this.
752+ /// [`InPlaceInit::try_pin_init `] function of a smart pointer like [`Arc<T>`] on this.
754753///
755754/// Also see the [module description](self).
756755///
757756/// # Safety
758757///
759- /// When implementing this type you will need to take great care. Also there are probably very few
758+ /// When implementing this trait you will need to take great care. Also there are probably very few
760759/// cases where a manual implementation is necessary. Use [`pin_init_from_closure`] where possible.
761760///
762- /// The [`PinInit::__pinned_init`] function
761+ /// The [`PinInit::__pinned_init`] function:
763762/// - returns `Ok(())` if it initialized every field of `slot`,
764763/// - returns `Err(err)` if it encountered an error and then cleaned `slot`, this means:
765764/// - `slot` can be deallocated without UB occurring,
@@ -837,17 +836,17 @@ where
837836///
838837/// To use this initializer, you will need a suitable memory location that can hold a `T`. This can
839838/// be [`Box<T>`], [`Arc<T>`] or even the stack (see [`stack_pin_init!`]). Use the
840- /// [`InPlaceInit::init `] function of a smart pointer like [`Arc<T>`] on this. Because
839+ /// [`InPlaceInit::try_init `] function of a smart pointer like [`Arc<T>`] on this. Because
841840/// [`PinInit<T, E>`] is a super trait, you can use every function that takes it as well.
842841///
843842/// Also see the [module description](self).
844843///
845844/// # Safety
846845///
847- /// When implementing this type you will need to take great care. Also there are probably very few
846+ /// When implementing this trait you will need to take great care. Also there are probably very few
848847/// cases where a manual implementation is necessary. Use [`init_from_closure`] where possible.
849848///
850- /// The [`Init::__init`] function
849+ /// The [`Init::__init`] function:
851850/// - returns `Ok(())` if it initialized every field of `slot`,
852851/// - returns `Err(err)` if it encountered an error and then cleaned `slot`, this means:
853852/// - `slot` can be deallocated without UB occurring,
@@ -1124,6 +1123,7 @@ pub trait InPlaceInit<T>: Sized {
11241123
11251124 /// Use the given initializer to in-place initialize a `T`.
11261125 fn init ( init : impl Init < T > ) -> Result < Self , AllocError > {
1126+ // SAFETY: We delegate to `init` and only change the error type.
11271127 let init = unsafe {
11281128 init_from_closure ( |slot| match init. __init ( slot) {
11291129 Ok ( ( ) ) => Ok ( ( ) ) ,
@@ -1219,14 +1219,11 @@ impl<T> InPlaceInit<T> for Arc<T> {
12191219/// println!("Foo is being dropped!");
12201220/// }
12211221/// }
1222- /// # let _ = Box::pin_init(pin_init!(Foo { mtx <- CMutex::new(0) }));
12231222/// ```
12241223///
12251224/// # Safety
12261225///
12271226/// This trait must be implemented via the [`pinned_drop`] proc-macro attribute on the impl.
1228- ///
1229- /// [`pinned_drop`]: pinned_init_macro::pinned_drop
12301227pub unsafe trait PinnedDrop : __internal:: HasPinData {
12311228 /// Executes the pinned destructor of this type.
12321229 ///
0 commit comments