Skip to content

Commit 40618c0

Browse files
committed
rust: pin-init: remove try_ versions of the initializer macros
The `try_[pin_]init!` versions of the initializer macros are superfluous. Instead of forcing the user to always write an error in `try_[pin_]init!` and not allowing one in `[pin_]init!`, combine them into `[pin_]init!` that defaults the error to `core::convert::Infallible`, but also allows to specify a custom one. Projects using pin-init still can provide their own defaulting initializers using the `try_` prefix by using the `#[default_error]` attribute added in a future patch. [ Adjust the definition of the kernel's version of the `try_` initializer macros - Benno] Reviewed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Benno Lossin <lossin@kernel.org>
1 parent 7e9e7a9 commit 40618c0

5 files changed

Lines changed: 35 additions & 122 deletions

File tree

rust/kernel/init.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,14 @@ macro_rules! try_init {
222222
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
223223
$($fields:tt)*
224224
}) => {
225-
::pin_init::try_init!($(&$this in)? $t $(::<$($generics),*>)? {
225+
::pin_init::init!($(&$this in)? $t $(::<$($generics),*>)? {
226226
$($fields)*
227227
}? $crate::error::Error)
228228
};
229229
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
230230
$($fields:tt)*
231231
}? $err:ty) => {
232-
::pin_init::try_init!($(&$this in)? $t $(::<$($generics),*>)? {
232+
::pin_init::init!($(&$this in)? $t $(::<$($generics),*>)? {
233233
$($fields)*
234234
}? $err)
235235
};
@@ -282,14 +282,14 @@ macro_rules! try_pin_init {
282282
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
283283
$($fields:tt)*
284284
}) => {
285-
::pin_init::try_pin_init!($(&$this in)? $t $(::<$($generics),*>)? {
285+
::pin_init::pin_init!($(&$this in)? $t $(::<$($generics),*>)? {
286286
$($fields)*
287287
}? $crate::error::Error)
288288
};
289289
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
290290
$($fields:tt)*
291291
}? $err:ty) => {
292-
::pin_init::try_pin_init!($(&$this in)? $t $(::<$($generics),*>)? {
292+
::pin_init::pin_init!($(&$this in)? $t $(::<$($generics),*>)? {
293293
$($fields)*
294294
}? $err)
295295
};

rust/pin-init/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ struct DriverData {
135135

136136
impl DriverData {
137137
fn new() -> impl PinInit<Self, Error> {
138-
try_pin_init!(Self {
138+
pin_init!(Self {
139139
status <- CMutex::new(0),
140140
buffer: Box::init(pin_init::init_zeroed())?,
141141
}? Error)

rust/pin-init/examples/linked_list.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use core::{
88
cell::Cell,
9-
convert::Infallible,
109
marker::PhantomPinned,
1110
pin::Pin,
1211
ptr::{self, NonNull},
@@ -31,31 +30,31 @@ pub struct ListHead {
3130

3231
impl ListHead {
3332
#[inline]
34-
pub fn new() -> impl PinInit<Self, Infallible> {
35-
try_pin_init!(&this in Self {
33+
pub fn new() -> impl PinInit<Self> {
34+
pin_init!(&this in Self {
3635
next: unsafe { Link::new_unchecked(this) },
3736
prev: unsafe { Link::new_unchecked(this) },
3837
pin: PhantomPinned,
39-
}? Infallible)
38+
})
4039
}
4140

4241
#[inline]
4342
#[allow(dead_code)]
44-
pub fn insert_next(list: &ListHead) -> impl PinInit<Self, Infallible> + '_ {
45-
try_pin_init!(&this in Self {
43+
pub fn insert_next(list: &ListHead) -> impl PinInit<Self> + '_ {
44+
pin_init!(&this in Self {
4645
prev: list.next.prev().replace(unsafe { Link::new_unchecked(this)}),
4746
next: list.next.replace(unsafe { Link::new_unchecked(this)}),
4847
pin: PhantomPinned,
49-
}? Infallible)
48+
})
5049
}
5150

5251
#[inline]
53-
pub fn insert_prev(list: &ListHead) -> impl PinInit<Self, Infallible> + '_ {
54-
try_pin_init!(&this in Self {
52+
pub fn insert_prev(list: &ListHead) -> impl PinInit<Self> + '_ {
53+
pin_init!(&this in Self {
5554
next: list.prev.next().replace(unsafe { Link::new_unchecked(this)}),
5655
prev: list.prev.replace(unsafe { Link::new_unchecked(this)}),
5756
pin: PhantomPinned,
58-
}? Infallible)
57+
})
5958
}
6059

6160
#[inline]

rust/pin-init/examples/pthread_mutex.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ mod pthread_mtx {
9898
// SAFETY: mutex has been initialized
9999
unsafe { pin_init_from_closure(init) }
100100
}
101-
try_pin_init!(Self {
102-
data: UnsafeCell::new(data),
103-
raw <- init_raw(),
104-
pin: PhantomPinned,
105-
}? Error)
101+
pin_init!(Self {
102+
data: UnsafeCell::new(data),
103+
raw <- init_raw(),
104+
pin: PhantomPinned,
105+
}? Error)
106106
}
107107

108108
#[allow(dead_code)]

rust/pin-init/src/lib.rs

Lines changed: 16 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
//!
147147
//! impl DriverData {
148148
//! fn new() -> impl PinInit<Self, Error> {
149-
//! try_pin_init!(Self {
149+
//! pin_init!(Self {
150150
//! status <- CMutex::new(0),
151151
//! buffer: Box::init(pin_init::init_zeroed())?,
152152
//! }? Error)
@@ -528,7 +528,7 @@ macro_rules! stack_pin_init {
528528
/// x: u32,
529529
/// }
530530
///
531-
/// stack_try_pin_init!(let foo: Foo = try_pin_init!(Foo {
531+
/// stack_try_pin_init!(let foo: Foo = pin_init!(Foo {
532532
/// a <- CMutex::new(42),
533533
/// b: Box::try_new(Bar {
534534
/// x: 64,
@@ -555,7 +555,7 @@ macro_rules! stack_pin_init {
555555
/// x: u32,
556556
/// }
557557
///
558-
/// stack_try_pin_init!(let foo: Foo =? try_pin_init!(Foo {
558+
/// stack_try_pin_init!(let foo: Foo =? pin_init!(Foo {
559559
/// a <- CMutex::new(42),
560560
/// b: Box::try_new(Bar {
561561
/// x: 64,
@@ -584,10 +584,10 @@ macro_rules! stack_try_pin_init {
584584
};
585585
}
586586

587-
/// Construct an in-place, pinned initializer for `struct`s.
587+
/// Construct an in-place, fallible pinned initializer for `struct`s.
588588
///
589-
/// This macro defaults the error to [`Infallible`]. If you need a different error, then use
590-
/// [`try_pin_init!`].
589+
/// The error type defaults to [`Infallible`]; if you need a different one, write `? Error` at the
590+
/// end, after the struct initializer.
591591
///
592592
/// The syntax is almost identical to that of a normal `struct` initializer:
593593
///
@@ -783,54 +783,10 @@ macro_rules! pin_init {
783783
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
784784
$($fields:tt)*
785785
}) => {
786-
$crate::try_pin_init!($(&$this in)? $t $(::<$($generics),*>)? {
786+
$crate::pin_init!($(&$this in)? $t $(::<$($generics),*>)? {
787787
$($fields)*
788788
}? ::core::convert::Infallible)
789789
};
790-
}
791-
792-
/// Construct an in-place, fallible pinned initializer for `struct`s.
793-
///
794-
/// If the initialization can complete without error (or [`Infallible`]), then use [`pin_init!`].
795-
///
796-
/// You can use the `?` operator or use `return Err(err)` inside the initializer to stop
797-
/// initialization and return the error.
798-
///
799-
/// IMPORTANT: if you have `unsafe` code inside of the initializer you have to ensure that when
800-
/// initialization fails, the memory can be safely deallocated without any further modifications.
801-
///
802-
/// The syntax is identical to [`pin_init!`] with the following exception: you must append `? $type`
803-
/// after the `struct` initializer to specify the error type you want to use.
804-
///
805-
/// # Examples
806-
///
807-
/// ```rust
808-
/// # #![feature(allocator_api)]
809-
/// # #[path = "../examples/error.rs"] mod error; use error::Error;
810-
/// use pin_init::{pin_data, try_pin_init, PinInit, InPlaceInit, init_zeroed};
811-
///
812-
/// #[pin_data]
813-
/// struct BigBuf {
814-
/// big: Box<[u8; 1024 * 1024 * 1024]>,
815-
/// small: [u8; 1024 * 1024],
816-
/// ptr: *mut u8,
817-
/// }
818-
///
819-
/// impl BigBuf {
820-
/// fn new() -> impl PinInit<Self, Error> {
821-
/// try_pin_init!(Self {
822-
/// big: Box::init(init_zeroed())?,
823-
/// small: [0; 1024 * 1024],
824-
/// ptr: core::ptr::null_mut(),
825-
/// }? Error)
826-
/// }
827-
/// }
828-
/// # let _ = Box::pin_init(BigBuf::new());
829-
/// ```
830-
// For a detailed example of how this macro works, see the module documentation of the hidden
831-
// module `macros` inside of `macros.rs`.
832-
#[macro_export]
833-
macro_rules! try_pin_init {
834790
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
835791
$($fields:tt)*
836792
}? $err:ty) => {
@@ -847,10 +803,10 @@ macro_rules! try_pin_init {
847803
}
848804
}
849805

850-
/// Construct an in-place initializer for `struct`s.
806+
/// Construct an in-place, fallible initializer for `struct`s.
851807
///
852-
/// This macro defaults the error to [`Infallible`]. If you need a different error, then use
853-
/// [`try_init!`].
808+
/// This macro defaults the error to [`Infallible`]; if you need a different one, write `? Error`
809+
/// at the end, after the struct initializer.
854810
///
855811
/// The syntax is identical to [`pin_init!`] and its safety caveats also apply:
856812
/// - `unsafe` code must guarantee either full initialization or return an error and allow
@@ -890,52 +846,10 @@ macro_rules! init {
890846
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
891847
$($fields:tt)*
892848
}) => {
893-
$crate::try_init!($(&$this in)? $t $(::<$($generics),*>)? {
849+
$crate::init!($(&$this in)? $t $(::<$($generics),*>)? {
894850
$($fields)*
895851
}? ::core::convert::Infallible)
896-
}
897-
}
898-
899-
/// Construct an in-place fallible initializer for `struct`s.
900-
///
901-
/// If the initialization can complete without error (or [`Infallible`]), then use
902-
/// [`init!`].
903-
///
904-
/// The syntax is identical to [`try_pin_init!`]. You need to specify a custom error
905-
/// via `? $type` after the `struct` initializer.
906-
/// The safety caveats from [`try_pin_init!`] also apply:
907-
/// - `unsafe` code must guarantee either full initialization or return an error and allow
908-
/// deallocation of the memory.
909-
/// - the fields are initialized in the order given in the initializer.
910-
/// - no references to fields are allowed to be created inside of the initializer.
911-
///
912-
/// # Examples
913-
///
914-
/// ```rust
915-
/// # #![feature(allocator_api)]
916-
/// # use core::alloc::AllocError;
917-
/// # use pin_init::InPlaceInit;
918-
/// use pin_init::{try_init, Init, init_zeroed};
919-
///
920-
/// struct BigBuf {
921-
/// big: Box<[u8; 1024 * 1024 * 1024]>,
922-
/// small: [u8; 1024 * 1024],
923-
/// }
924-
///
925-
/// impl BigBuf {
926-
/// fn new() -> impl Init<Self, AllocError> {
927-
/// try_init!(Self {
928-
/// big: Box::init(init_zeroed())?,
929-
/// small: [0; 1024 * 1024],
930-
/// }? AllocError)
931-
/// }
932-
/// }
933-
/// # let _ = Box::init(BigBuf::new());
934-
/// ```
935-
// For a detailed example of how this macro works, see the module documentation of the hidden
936-
// module `macros` inside of `macros.rs`.
937-
#[macro_export]
938-
macro_rules! try_init {
852+
};
939853
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
940854
$($fields:tt)*
941855
}? $err:ty) => {
@@ -1410,14 +1324,14 @@ where
14101324
/// fn init_foo() -> impl PinInit<Foo, Error> {
14111325
/// pin_init_scope(|| {
14121326
/// let bar = lookup_bar()?;
1413-
/// Ok(try_pin_init!(Foo { a: bar.a.into(), b: bar.b }? Error))
1327+
/// Ok(pin_init!(Foo { a: bar.a.into(), b: bar.b }? Error))
14141328
/// })
14151329
/// }
14161330
/// ```
14171331
///
14181332
/// This initializer will first execute `lookup_bar()`, match on it, if it returned an error, the
14191333
/// initializer itself will fail with that error. If it returned `Ok`, then it will run the
1420-
/// initializer returned by the [`try_pin_init!`] invocation.
1334+
/// initializer returned by the [`pin_init!`] invocation.
14211335
pub fn pin_init_scope<T, E, F, I>(make_init: F) -> impl PinInit<T, E>
14221336
where
14231337
F: FnOnce() -> Result<I, E>,
@@ -1453,14 +1367,14 @@ where
14531367
/// fn init_foo() -> impl Init<Foo, Error> {
14541368
/// init_scope(|| {
14551369
/// let bar = lookup_bar()?;
1456-
/// Ok(try_init!(Foo { a: bar.a.into(), b: bar.b }? Error))
1370+
/// Ok(init!(Foo { a: bar.a.into(), b: bar.b }? Error))
14571371
/// })
14581372
/// }
14591373
/// ```
14601374
///
14611375
/// This initializer will first execute `lookup_bar()`, match on it, if it returned an error, the
14621376
/// initializer itself will fail with that error. If it returned `Ok`, then it will run the
1463-
/// initializer returned by the [`try_init!`] invocation.
1377+
/// initializer returned by the [`init!`] invocation.
14641378
pub fn init_scope<T, E, F, I>(make_init: F) -> impl Init<T, E>
14651379
where
14661380
F: FnOnce() -> Result<I, E>,

0 commit comments

Comments
 (0)