2
2
3
3
//! Library to safely and fallibly initialize pinned `struct`s using in-place constructors.
4
4
//!
5
+ //! [Pinning][pinning] is Rust's way of ensuring data does not move.
6
+ //!
5
7
//! It also allows in-place initialization of big `struct`s that would otherwise produce a stack
6
8
//! overflow.
7
9
//!
19
21
//! # Nightly only
20
22
//!
21
23
//! 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:
23
25
//! - `allocator_api`
24
26
//! - `new_uninit` (only if the `alloc` or `std` features are enabled)
25
27
//! - `get_mut_unchecked` (only if the `alloc` or `std` features are enabled)
45
47
//!
46
48
//! # Examples
47
49
//!
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.
52
54
//!
53
55
//! ## Using the [`pin_init!`] macro
54
56
//!
217
219
//! }
218
220
//! ```
219
221
//!
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.
223
224
//!
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
224
227
//! [structurally pinned fields]:
225
228
//! https://doc.rust-lang.org/std/pin/index.html#pinning-is-structural-for-field
226
229
//! [stack]: crate::stack_pin_init
@@ -551,6 +554,9 @@ macro_rules! stack_try_pin_init {
551
554
/// - Fields that you want to initialize in-place have to use `<-` instead of `:`.
552
555
/// - In front of the initializer you can write `&this in` to have access to a [`NonNull<Self>`]
553
556
/// 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.
554
560
///
555
561
/// For instance:
556
562
///
@@ -575,8 +581,6 @@ macro_rules! stack_try_pin_init {
575
581
/// ```
576
582
///
577
583
/// [`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`.
580
584
#[ macro_export]
581
585
macro_rules! pin_init {
582
586
( $( & $this: ident in) ? $t: ident $( :: <$( $generics: ty) ,* $( , ) ?>) ? {
@@ -625,8 +629,6 @@ macro_rules! pin_init {
625
629
/// }
626
630
/// # let _ = Box::pin_init(BigBuf::new());
627
631
/// ```
628
- // For a detailed example of how this macro works, see the module documentation of the hidden
629
- // module `__internal` inside of `__internal.rs`.
630
632
#[ macro_export]
631
633
macro_rules! try_pin_init {
632
634
( $( & $this: ident in) ? $t: ident $( :: <$( $generics: ty) ,* $( , ) ?>) ? {
@@ -658,6 +660,7 @@ macro_rules! try_pin_init {
658
660
///
659
661
/// This initializer is for initializing data in-place that might later be moved. If you want to
660
662
/// pin-initialize, use [`pin_init!`].
663
+ ///
661
664
/// # Examples
662
665
///
663
666
/// ```rust
@@ -679,8 +682,6 @@ macro_rules! try_pin_init {
679
682
/// }
680
683
/// # let _ = Box::init(BigBuf::new());
681
684
/// ```
682
- // For a detailed example of how this macro works, see the module documentation of the hidden
683
- // module `__internal` inside of `__internal.rs`.
684
685
#[ macro_export]
685
686
macro_rules! init {
686
687
( $( & $this: ident in) ? $t: ident $( :: <$( $generics: ty) ,* $( , ) ?>) ? {
@@ -726,8 +727,6 @@ macro_rules! init {
726
727
/// }
727
728
/// # let _ = Box::init(BigBuf::new());
728
729
/// ```
729
- // For a detailed example of how this macro works, see the module documentation of the hidden
730
- // module `__internal` inside of `__internal.rs`.
731
730
#[ macro_export]
732
731
macro_rules! try_init {
733
732
( $( & $this: ident in) ? $t: ident $( :: <$( $generics: ty) ,* $( , ) ?>) ? {
@@ -750,16 +749,16 @@ macro_rules! try_init {
750
749
///
751
750
/// To use this initializer, you will need a suitable memory location that can hold a `T`. This can
752
751
/// 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.
754
753
///
755
754
/// Also see the [module description](self).
756
755
///
757
756
/// # Safety
758
757
///
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
760
759
/// cases where a manual implementation is necessary. Use [`pin_init_from_closure`] where possible.
761
760
///
762
- /// The [`PinInit::__pinned_init`] function
761
+ /// The [`PinInit::__pinned_init`] function:
763
762
/// - returns `Ok(())` if it initialized every field of `slot`,
764
763
/// - returns `Err(err)` if it encountered an error and then cleaned `slot`, this means:
765
764
/// - `slot` can be deallocated without UB occurring,
@@ -837,17 +836,17 @@ where
837
836
///
838
837
/// To use this initializer, you will need a suitable memory location that can hold a `T`. This can
839
838
/// 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
841
840
/// [`PinInit<T, E>`] is a super trait, you can use every function that takes it as well.
842
841
///
843
842
/// Also see the [module description](self).
844
843
///
845
844
/// # Safety
846
845
///
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
848
847
/// cases where a manual implementation is necessary. Use [`init_from_closure`] where possible.
849
848
///
850
- /// The [`Init::__init`] function
849
+ /// The [`Init::__init`] function:
851
850
/// - returns `Ok(())` if it initialized every field of `slot`,
852
851
/// - returns `Err(err)` if it encountered an error and then cleaned `slot`, this means:
853
852
/// - `slot` can be deallocated without UB occurring,
@@ -1124,6 +1123,7 @@ pub trait InPlaceInit<T>: Sized {
1124
1123
1125
1124
/// Use the given initializer to in-place initialize a `T`.
1126
1125
fn init ( init : impl Init < T > ) -> Result < Self , AllocError > {
1126
+ // SAFETY: We delegate to `init` and only change the error type.
1127
1127
let init = unsafe {
1128
1128
init_from_closure ( |slot| match init. __init ( slot) {
1129
1129
Ok ( ( ) ) => Ok ( ( ) ) ,
@@ -1219,14 +1219,11 @@ impl<T> InPlaceInit<T> for Arc<T> {
1219
1219
/// println!("Foo is being dropped!");
1220
1220
/// }
1221
1221
/// }
1222
- /// # let _ = Box::pin_init(pin_init!(Foo { mtx <- CMutex::new(0) }));
1223
1222
/// ```
1224
1223
///
1225
1224
/// # Safety
1226
1225
///
1227
1226
/// This trait must be implemented via the [`pinned_drop`] proc-macro attribute on the impl.
1228
- ///
1229
- /// [`pinned_drop`]: pinned_init_macro::pinned_drop
1230
1227
pub unsafe trait PinnedDrop : __internal:: HasPinData {
1231
1228
/// Executes the pinned destructor of this type.
1232
1229
///
0 commit comments