Skip to content

Commit dbd5058

Browse files
BennoLossinojeda
authored andcommitted
rust: make pin-init its own crate
Rename relative paths inside of the crate to still refer to the same items, also rename paths inside of the kernel crate and adjust the build system to build the crate. [ Remove the `expect` (and thus the `lint_reasons` feature) since the tree now uses `quote!` from `rust/macros/export.rs`. Remove the `TokenStream` import removal, since it is now used as well. In addition, temporarily (i.e. just for this commit) use an `--extern force:alloc` to prevent an unknown `new_uninit` error in the `rustdoc` target. For context, please see a similar case in: https://lore.kernel.org/lkml/[email protected]/ And adjusted the message above. - Miguel ] Signed-off-by: Benno Lossin <[email protected]> Reviewed-by: Fiona Behrens <[email protected]> Tested-by: Andreas Hindborg <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent d7659ac commit dbd5058

28 files changed

+164
-153
lines changed

rust/Makefile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ rustdoc-ffi: $(src)/ffi.rs rustdoc-core FORCE
116116
rustdoc-pin_init_internal: private rustdoc_host = yes
117117
rustdoc-pin_init_internal: private rustc_target_flags = --cfg kernel \
118118
--extern proc_macro --crate-type proc-macro
119-
rustdoc-pin_init_internal: $(src)/pin-init/internal/src/_lib.rs FORCE
119+
rustdoc-pin_init_internal: $(src)/pin-init/internal/src/lib.rs FORCE
120120
+$(call if_changed,rustdoc)
121121

122122
rustdoc-pin_init: private rustdoc_host = yes
123123
rustdoc-pin_init: private rustc_target_flags = --extern pin_init_internal \
124-
--extern macros --extern alloc --cfg kernel --cfg feature=\"alloc\"
125-
rustdoc-pin_init: $(src)/pin-init/src/_lib.rs rustdoc-pin_init_internal \
124+
--extern macros --extern force:alloc --cfg kernel --cfg feature=\"alloc\"
125+
rustdoc-pin_init: $(src)/pin-init/src/lib.rs rustdoc-pin_init_internal \
126126
rustdoc-macros FORCE
127127
+$(call if_changed,rustdoc)
128128

@@ -158,12 +158,12 @@ rusttestlib-macros: $(src)/macros/lib.rs FORCE
158158
rusttestlib-pin_init_internal: private rustc_target_flags = --cfg kernel \
159159
--extern proc_macro
160160
rusttestlib-pin_init_internal: private rustc_test_library_proc = yes
161-
rusttestlib-pin_init_internal: $(src)/pin-init/internal/src/_lib.rs FORCE
161+
rusttestlib-pin_init_internal: $(src)/pin-init/internal/src/lib.rs FORCE
162162
+$(call if_changed,rustc_test_library)
163163

164164
rusttestlib-pin_init: private rustc_target_flags = --extern pin_init_internal \
165165
--extern macros --cfg kernel
166-
rusttestlib-pin_init: $(src)/pin-init/src/_lib.rs rusttestlib-macros \
166+
rusttestlib-pin_init: $(src)/pin-init/src/lib.rs rusttestlib-macros \
167167
rusttestlib-pin_init_internal $(obj)/$(libpin_init_internal_name) FORCE
168168
+$(call if_changed,rustc_test_library)
169169

@@ -401,7 +401,7 @@ $(obj)/$(libmacros_name): $(src)/macros/lib.rs FORCE
401401
+$(call if_changed_dep,rustc_procmacro)
402402

403403
$(obj)/$(libpin_init_internal_name): private rustc_target_flags = --cfg kernel
404-
$(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/_lib.rs FORCE
404+
$(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs FORCE
405405
+$(call if_changed_dep,rustc_procmacro)
406406

407407
quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
@@ -486,7 +486,7 @@ $(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
486486
$(obj)/pin_init.o: private skip_gendwarfksyms = 1
487487
$(obj)/pin_init.o: private rustc_target_flags = --extern pin_init_internal \
488488
--extern macros --cfg kernel
489-
$(obj)/pin_init.o: $(src)/pin-init/src/_lib.rs $(obj)/compiler_builtins.o \
489+
$(obj)/pin_init.o: $(src)/pin-init/src/lib.rs $(obj)/compiler_builtins.o \
490490
$(obj)/$(libpin_init_internal_name) $(obj)/$(libmacros_name) FORCE
491491
+$(call if_changed_rule,rustc_library)
492492

rust/kernel/alloc/kbox.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use core::pin::Pin;
1515
use core::ptr::NonNull;
1616
use core::result::Result;
1717

18-
use crate::init::{InPlaceWrite, Init, PinInit, ZeroableOption};
19-
use crate::init_ext::InPlaceInit;
18+
use crate::init::InPlaceInit;
2019
use crate::types::ForeignOwnable;
20+
use pin_init::{InPlaceWrite, Init, PinInit, ZeroableOption};
2121

2222
/// The kernel's [`Box`] type -- a heap allocation for a single value of type `T`.
2323
///

rust/kernel/block/mq/tag_set.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ use crate::{
1010
bindings,
1111
block::mq::{operations::OperationsVTable, request::RequestDataWrapper, Operations},
1212
error,
13-
prelude::PinInit,
14-
try_pin_init,
13+
prelude::try_pin_init,
1514
types::Opaque,
1615
};
1716
use core::{convert::TryInto, marker::PhantomData};
18-
use macros::{pin_data, pinned_drop};
17+
use pin_init::{pin_data, pinned_drop, PinInit};
1918

2019
/// A wrapper for the C `struct blk_mq_tag_set`.
2120
///

rust/kernel/driver.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
//! register using the [`Registration`] class.
77
88
use crate::error::{Error, Result};
9-
use crate::{device, init::PinInit, of, str::CStr, try_pin_init, types::Opaque, ThisModule};
9+
use crate::{device, of, str::CStr, try_pin_init, types::Opaque, ThisModule};
1010
use core::pin::Pin;
11-
use macros::{pin_data, pinned_drop};
11+
use pin_init::{pin_data, pinned_drop, PinInit};
1212

1313
/// The [`RegistrationOps`] trait serves as generic interface for subsystems (e.g., PCI, Platform,
1414
/// Amba, etc.) to provide the corresponding subsystem specific implementation to register /
@@ -114,7 +114,7 @@ macro_rules! module_driver {
114114
impl $crate::InPlaceModule for DriverModule {
115115
fn init(
116116
module: &'static $crate::ThisModule
117-
) -> impl $crate::init::PinInit<Self, $crate::error::Error> {
117+
) -> impl ::pin_init::PinInit<Self, $crate::error::Error> {
118118
$crate::try_pin_init!(Self {
119119
_driver <- $crate::driver::Registration::new(
120120
<Self as $crate::ModuleMetadata>::NAME,

rust/kernel/init.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
//!
2424
//! [`Opaque<T>`]: crate::types::Opaque
2525
//! [`Opaque::ffi_init`]: crate::types::Opaque::ffi_init
26-
//! [`pin_init!`]: crate::pin_init
26+
//! [`pin_init!`]: pin_init::pin_init
2727
//!
2828
//! # Examples
2929
//!
@@ -137,8 +137,8 @@
137137
use crate::{
138138
alloc::{AllocError, Flags},
139139
error::{self, Error},
140-
init::{init_from_closure, pin_init_from_closure, Init, PinInit},
141140
};
141+
use pin_init::{init_from_closure, pin_init_from_closure, Init, PinInit};
142142

143143
/// Smart pointer that can initialize memory in-place.
144144
pub trait InPlaceInit<T>: Sized {
@@ -205,7 +205,8 @@ pub trait InPlaceInit<T>: Sized {
205205
/// # Examples
206206
///
207207
/// ```rust
208-
/// use kernel::{init::zeroed, error::Error};
208+
/// use kernel::error::Error;
209+
/// use pin_init::zeroed;
209210
/// struct BigBuf {
210211
/// big: KBox<[u8; 1024 * 1024 * 1024]>,
211212
/// small: [u8; 1024 * 1024],
@@ -222,22 +223,22 @@ pub trait InPlaceInit<T>: Sized {
222223
/// ```
223224
///
224225
/// [`Infallible`]: core::convert::Infallible
225-
/// [`init!`]: crate::init!
226+
/// [`init!`]: pin_init::init
226227
/// [`try_pin_init!`]: crate::try_pin_init!
227228
/// [`Error`]: crate::error::Error
228229
#[macro_export]
229230
macro_rules! try_init {
230231
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
231232
$($fields:tt)*
232233
}) => {
233-
$crate::_try_init!($(&$this in)? $t $(::<$($generics),* $(,)?>)? {
234+
::pin_init::try_init!($(&$this in)? $t $(::<$($generics),* $(,)?>)? {
234235
$($fields)*
235236
}? $crate::error::Error)
236237
};
237238
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
238239
$($fields:tt)*
239240
}? $err:ty) => {
240-
$crate::_try_init!($(&$this in)? $t $(::<$($generics),* $(,)?>)? {
241+
::pin_init::try_init!($(&$this in)? $t $(::<$($generics),* $(,)?>)? {
241242
$($fields)*
242243
}? $err)
243244
};
@@ -262,7 +263,8 @@ macro_rules! try_init {
262263
///
263264
/// ```rust
264265
/// # #![feature(new_uninit)]
265-
/// use kernel::{init::zeroed, error::Error};
266+
/// use kernel::error::Error;
267+
/// use pin_init::zeroed;
266268
/// #[pin_data]
267269
/// struct BigBuf {
268270
/// big: KBox<[u8; 1024 * 1024 * 1024]>,
@@ -282,21 +284,21 @@ macro_rules! try_init {
282284
/// ```
283285
///
284286
/// [`Infallible`]: core::convert::Infallible
285-
/// [`pin_init!`]: crate::pin_init
287+
/// [`pin_init!`]: pin_init::pin_init
286288
/// [`Error`]: crate::error::Error
287289
#[macro_export]
288290
macro_rules! try_pin_init {
289291
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
290292
$($fields:tt)*
291293
}) => {
292-
$crate::_try_pin_init!($(&$this in)? $t $(::<$($generics),* $(,)?>)? {
294+
::pin_init::try_pin_init!($(&$this in)? $t $(::<$($generics),* $(,)?>)? {
293295
$($fields)*
294296
}? $crate::error::Error)
295297
};
296298
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
297299
$($fields:tt)*
298300
}? $err:ty) => {
299-
$crate::_try_pin_init!($(&$this in)? $t $(::<$($generics),* $(,)?>)? {
301+
::pin_init::try_pin_init!($(&$this in)? $t $(::<$($generics),* $(,)?>)? {
300302
$($fields)*
301303
}? $err)
302304
};

rust/kernel/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ pub mod faux;
5050
#[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
5151
pub mod firmware;
5252
pub mod fs;
53-
#[path = "../pin-init/src/lib.rs"]
5453
pub mod init;
55-
// momentarily use the name `init_ext` and set the path manually
56-
#[path = "init.rs"]
57-
pub mod init_ext;
5854
pub mod io;
5955
pub mod ioctl;
6056
pub mod jump_label;
@@ -116,11 +112,11 @@ pub trait InPlaceModule: Sync + Send {
116112
/// Creates an initialiser for the module.
117113
///
118114
/// It is called when the module is loaded.
119-
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error>;
115+
fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error>;
120116
}
121117

122118
impl<T: Module> InPlaceModule for T {
123-
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error> {
119+
fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error> {
124120
let initer = move |slot: *mut Self| {
125121
let m = <Self as Module>::init(module)?;
126122

@@ -130,7 +126,7 @@ impl<T: Module> InPlaceModule for T {
130126
};
131127

132128
// SAFETY: On success, `initer` always fully initialises an instance of `Self`.
133-
unsafe { init::pin_init_from_closure(initer) }
129+
unsafe { pin_init::pin_init_from_closure(initer) }
134130
}
135131
}
136132

rust/kernel/list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
//! A linked list implementation.
66
7-
use crate::init::PinInit;
87
use crate::sync::ArcBorrow;
98
use crate::types::Opaque;
109
use core::iter::{DoubleEndedIterator, FusedIterator};
1110
use core::marker::PhantomData;
1211
use core::ptr;
12+
use pin_init::PinInit;
1313

1414
mod impl_list_item_mod;
1515
pub use self::impl_list_item_mod::{

rust/kernel/prelude.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ pub use core::pin::Pin;
1717
pub use crate::alloc::{flags::*, Box, KBox, KVBox, KVVec, KVec, VBox, VVec, Vec};
1818

1919
#[doc(no_inline)]
20-
pub use macros::{export, module, pin_data, pinned_drop, vtable, Zeroable};
20+
pub use macros::{export, module, vtable};
21+
22+
pub use pin_init::{init, pin_data, pin_init, pinned_drop, InPlaceWrite, Init, PinInit, Zeroable};
2123

2224
pub use super::{build_assert, build_error};
2325

@@ -28,15 +30,14 @@ pub use super::fmt;
2830
pub use super::{dev_alert, dev_crit, dev_dbg, dev_emerg, dev_err, dev_info, dev_notice, dev_warn};
2931
pub use super::{pr_alert, pr_crit, pr_debug, pr_emerg, pr_err, pr_info, pr_notice, pr_warn};
3032

31-
pub use super::{init, pin_init, try_init, try_pin_init};
33+
pub use super::{try_init, try_pin_init};
3234

3335
pub use super::static_assert;
3436

3537
pub use super::error::{code::*, Error, Result};
3638

3739
pub use super::{str::CStr, ThisModule};
3840

39-
pub use super::init::{InPlaceWrite, Init, PinInit};
40-
pub use super::init_ext::InPlaceInit;
41+
pub use super::init::InPlaceInit;
4142

4243
pub use super::current;

rust/kernel/sync/arc.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
use crate::{
2020
alloc::{AllocError, Flags, KBox},
2121
bindings,
22-
init::{self, InPlaceWrite, Init, PinInit},
23-
init_ext::InPlaceInit,
22+
init::InPlaceInit,
2423
try_init,
2524
types::{ForeignOwnable, Opaque},
2625
};
@@ -33,7 +32,7 @@ use core::{
3332
pin::Pin,
3433
ptr::NonNull,
3534
};
36-
use macros::pin_data;
35+
use pin_init::{self, pin_data, InPlaceWrite, Init, PinInit};
3736

3837
mod std_vendor;
3938

@@ -738,7 +737,7 @@ impl<T> UniqueArc<T> {
738737
try_init!(ArcInner {
739738
// SAFETY: There are no safety requirements for this FFI call.
740739
refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
741-
data <- init::uninit::<T, AllocError>(),
740+
data <- pin_init::uninit::<T, AllocError>(),
742741
}? AllocError),
743742
flags,
744743
)?;

rust/kernel/sync/condvar.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88
use super::{lock::Backend, lock::Guard, LockClassKey};
99
use crate::{
1010
ffi::{c_int, c_long},
11-
init::PinInit,
12-
pin_init,
1311
str::CStr,
1412
task::{MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE, TASK_NORMAL, TASK_UNINTERRUPTIBLE},
1513
time::Jiffies,
1614
types::Opaque,
1715
};
1816
use core::marker::PhantomPinned;
1917
use core::ptr;
20-
use macros::pin_data;
18+
use pin_init::{pin_data, pin_init, PinInit};
2119

2220
/// Creates a [`CondVar`] initialiser with the given name and a newly-created lock class.
2321
#[macro_export]

rust/kernel/sync/lock.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
88
use super::LockClassKey;
99
use crate::{
10-
init::PinInit,
11-
pin_init,
1210
str::CStr,
1311
types::{NotThreadSafe, Opaque, ScopeGuard},
1412
};
1513
use core::{cell::UnsafeCell, marker::PhantomPinned};
16-
use macros::pin_data;
14+
use pin_init::{pin_data, pin_init, PinInit};
1715

1816
pub mod mutex;
1917
pub mod spinlock;

rust/kernel/sync/lock/mutex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub use new_mutex;
2626
/// Since it may block, [`Mutex`] needs to be used with care in atomic contexts.
2727
///
2828
/// Instances of [`Mutex`] need a lock class and to be pinned. The recommended way to create such
29-
/// instances is with the [`pin_init`](crate::pin_init) and [`new_mutex`] macros.
29+
/// instances is with the [`pin_init`](pin_init::pin_init) and [`new_mutex`] macros.
3030
///
3131
/// # Examples
3232
///

rust/kernel/sync/lock/spinlock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub use new_spinlock;
2424
/// unlocked, at which point another CPU will be allowed to make progress.
2525
///
2626
/// Instances of [`SpinLock`] need a lock class and to be pinned. The recommended way to create such
27-
/// instances is with the [`pin_init`](crate::pin_init) and [`new_spinlock`] macros.
27+
/// instances is with the [`pin_init`](pin_init::pin_init) and [`new_spinlock`] macros.
2828
///
2929
/// # Examples
3030
///

rust/kernel/types.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
//! Kernel types.
44
5-
use crate::init::{self, PinInit, Zeroable};
65
use core::{
76
cell::UnsafeCell,
87
marker::{PhantomData, PhantomPinned},
98
mem::{ManuallyDrop, MaybeUninit},
109
ops::{Deref, DerefMut},
1110
ptr::NonNull,
1211
};
12+
use pin_init::{PinInit, Zeroable};
1313

1414
/// Used to transfer ownership to and from foreign (non-Rust) languages.
1515
///
@@ -336,7 +336,7 @@ impl<T> Opaque<T> {
336336
// - `ptr` is a valid pointer to uninitialized memory,
337337
// - `slot` is not accessed on error; the call is infallible,
338338
// - `slot` is pinned in memory.
339-
let _ = unsafe { init::PinInit::<T>::__pinned_init(slot, ptr) };
339+
let _ = unsafe { PinInit::<T>::__pinned_init(slot, ptr) };
340340
})
341341
}
342342

@@ -352,7 +352,7 @@ impl<T> Opaque<T> {
352352
// SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
353353
// initialize the `T`.
354354
unsafe {
355-
init::pin_init_from_closure::<_, ::core::convert::Infallible>(move |slot| {
355+
pin_init::pin_init_from_closure::<_, ::core::convert::Infallible>(move |slot| {
356356
init_func(Self::raw_get(slot));
357357
Ok(())
358358
})
@@ -372,7 +372,9 @@ impl<T> Opaque<T> {
372372
) -> impl PinInit<Self, E> {
373373
// SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
374374
// initialize the `T`.
375-
unsafe { init::pin_init_from_closure::<_, E>(move |slot| init_func(Self::raw_get(slot))) }
375+
unsafe {
376+
pin_init::pin_init_from_closure::<_, E>(move |slot| init_func(Self::raw_get(slot)))
377+
}
376378
}
377379

378380
/// Returns a raw pointer to the opaque data.

rust/macros/helpers.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,3 @@ pub(crate) fn function_name(input: TokenStream) -> Option<Ident> {
8686
}
8787
None
8888
}
89-
90-
include!("../pin-init/internal/src/helpers.rs");

0 commit comments

Comments
 (0)