Skip to content

Commit da22329

Browse files
committed
update documentation
1 parent db5b2b1 commit da22329

File tree

4 files changed

+50
-42
lines changed

4 files changed

+50
-42
lines changed

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
Library to safely and fallibly initialize pinned `struct`s using in-place constructors.
1212

13+
[Pinning][pinning] is Rust's way of ensuring data does not move.
14+
1315
It also allows in-place initialization of big `struct`s that would otherwise produce a stack
1416
overflow.
1517

@@ -27,7 +29,7 @@ This library allows you to do in-place initialization safely.
2729
## Nightly only
2830

2931
This library requires unstable features and thus can only be used with a nightly compiler.
30-
The used features are:
32+
The internally used features are:
3133
- `allocator_api`
3234
- `new_uninit` (only if the `alloc` or `std` features are enabled)
3335
- `get_mut_unchecked` (only if the `alloc` or `std` features are enabled)
@@ -53,10 +55,10 @@ prefix.
5355

5456
## Examples
5557

56-
Throught some examples we will make use of the `CMutex` type which can be found in the examples
57-
directory of the repository. It is essentially a rebuild of the `mutex` from the Linux kernel
58-
in userland. So it also uses a wait list and a basic spinlock. Importantly it needs to be
59-
pinned to be locked and thus is a prime candidate for this library.
58+
Throught some examples we will make use of the `CMutex` type which can be found in
59+
`../examples/mutex.rs`. It is essentially a rebuild of the `mutex` from the Linux kernel in userland. So
60+
it also uses a wait list and a basic spinlock. Importantly it needs to be pinned to be locked
61+
and thus is a prime candidate for using this library.
6062

6163
### Using the [`pin_init!`] macro
6264

@@ -195,10 +197,11 @@ impl PinnedDrop for RawFoo {
195197
}
196198
```
197199

198-
For more information on how to use [`pin_init_from_closure()`], you can take a look at the
199-
uses inside the `kernel` crate from the [Rust-for-Linux] project. The `sync` module is a good
200-
starting point.
200+
For more information on how to use [`pin_init_from_closure()`], take a look at the uses inside
201+
the `kernel` crate. The [`sync`] module is a good starting point.
201202

203+
[`sync`]: https://github.com/Rust-for-Linux/linux/tree/rust-next/rust/kernel/sync
204+
[pinning]: https://doc.rust-lang.org/std/pin/index.html
202205
[structurally pinned fields]: https://doc.rust-lang.org/std/pin/index.html#pinning-is-structural-for-field
203206
[stack]: https://docs.rs/pinned-init/latest/pinned_init/macro.stack_pin_init.html
204207
[`Arc<T>`]: https://doc.rust-lang.org/stable/alloc/sync/struct.Arc.html

src/__internal.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
// SPDX-License-Identifier: Apache-2.0 OR MIT
22

3-
//! This module contains library-internal items.
3+
//! This module contains library internal items.
44
//!
5-
//! These items must not be used outside of
6-
//! - `lib.rs`
7-
//! - `../pinned-init-macro/src/pin_data.rs`
5+
//! These items must not be used outside of these files in the case of the kernel repository:
6+
//! - `../kernel/init.rs`
7+
//! - `./lib.rs`
8+
//! - `./macros.rs`
9+
//! - `../macros/pin_data.rs`
10+
//! - `../macros/pinned_drop.rs`
11+
//!
12+
//! And in the case of the `pinned-init` repository:
13+
//! - `./lib.rs`
14+
//! - `./macros.rs`
815
//! - `../pinned-init-macro/src/pinned_drop.rs`
16+
//! - `../pinned-init-macro/src/pin_data.rs`
917
1018
use super::*;
1119

@@ -151,7 +159,7 @@ impl<T> StackInit<T> {
151159
/// Creates a new [`StackInit<T>`] that is uninitialized. Use [`stack_pin_init`] instead of this
152160
/// primitive.
153161
///
154-
/// [`stack_pin_init`]: pinned_init::stack_pin_init
162+
/// [`stack_pin_init`]: crate::stack_pin_init
155163
#[inline]
156164
pub fn uninit() -> Self {
157165
Self {

src/lib.rs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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
//!
@@ -19,7 +21,7 @@
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)
@@ -45,10 +47,10 @@
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
//!
@@ -217,10 +219,11 @@
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]
581585
macro_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]
631633
macro_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]
685686
macro_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]
732731
macro_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
12301227
pub unsafe trait PinnedDrop: __internal::HasPinData {
12311228
/// Executes the pinned destructor of this type.
12321229
///

src/macros.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// SPDX-License-Identifier: Apache-2.0 OR MIT
22

3-
//! This module provides the macros that actually implement the proc-macros `pin_data` and
4-
//! `pinned_drop`. It also contains `__init_internal` the implementation of the `{try_}{pin_}init!`
5-
//! macros.
3+
//! This module provides the macros that actually implement the proc-macros `pin_data`,
4+
//! `pinned_drop` and the derive macro for zeroable. It also contains `__init_internal` the
5+
//! implementation of the `{try_}{pin_}init!` macros.
66
//!
77
//! These macros should never be called directly, since they expect their input to be
88
//! in a certain format which is internal. If used incorrectly, these macros can lead to UB even in
@@ -17,7 +17,7 @@
1717
//! # Macro expansion example
1818
//!
1919
//! This section is intended for readers trying to understand the macros in this module and the
20-
//! `pin_init!` macros from `init.rs`.
20+
//! `[try_][pin_]init!` macros from `lib.rs`.
2121
//!
2222
//! We will look at the following example:
2323
//!

0 commit comments

Comments
 (0)