Lines Matching +full:foo +full:- +full:supply

1 // SPDX-License-Identifier: Apache-2.0 OR MIT
3 //! API to safely and fallibly initialize pinned `struct`s using in-place constructors.
5 //! It also allows in-place initialization of big `struct`s that would otherwise produce a stack
8 //! Most `struct`s from the [`sync`] module need to be pinned, because they contain self-referential
13 //! To initialize a `struct` with an in-place constructor you will need two things:
14 //! - an in-place constructor,
15 //! - a memory location that can hold your `struct` (this can be the [stack], an [`Arc<T>`],
18 //! To get an in-place constructor there are generally three options:
19 //! - directly creating an in-place constructor using the [`pin_init!`] macro,
20 //! - a custom function/macro returning an in-place constructor provided by someone else,
21 //! - using the unsafe function [`pin_init_from_closure()`] to manually create an initializer.
23 //! Aside from pinned initialization, this API also supports in-place construction without pinning,
33 //! [structurally pinned fields]. After doing this, you can then create an in-place constructor via
35 //! that you need to write `<-` instead of `:` for fields that you want to initialize in-place.
42 //! struct Foo {
48 //! let foo = pin_init!(Foo {
49 //! a <- new_mutex!(42, "Foo::a"),
54 //! `foo` now is of the type [`impl PinInit<Foo>`]. We can now use any smart pointer that we like
55 //! (or just the stack) to actually initialize a `Foo`:
62 //! # struct Foo {
67 //! # let foo = pin_init!(Foo {
68 //! # a <- new_mutex!(42, "Foo::a"),
71 //! let foo: Result<Pin<KBox<Foo>>> = KBox::pin_init(foo, GFP_KERNEL);
78 //! Many types from the kernel supply a function/macro that returns an initializer, because the
99 //! fn new() -> impl PinInit<Self, Error> {
101 //! status <- new_mutex!(0, "DriverData::status"),
115 //! - when the closure returns `Ok(())`, then it has completed the initialization successfully, so
117 //! - when the closure returns `Err(e)`, then the caller may deallocate the memory at `slot`, so
118 //! you need to take care to clean up anything if your initialization fails mid-way,
119 //! - you may assume that `slot` will stay pinned even after the closure returns until `drop` of
129 //! # pub struct foo;
130 //! # pub unsafe fn init_foo(_ptr: *mut foo) {}
131 //! # pub unsafe fn destroy_foo(_ptr: *mut foo) {}
132 //! # pub unsafe fn enable_foo(_ptr: *mut foo, _flags: u32) -> i32 { 0 }
136 //! # fn from_errno(errno: kernel::ffi::c_int) -> Error {
144 //! /// `foo` is always initialized
148 //! foo: Opaque<bindings::foo>,
154 //! pub fn new(flags: u32) -> impl PinInit<Self, Error> {
156 //! // - when the closure returns `Ok(())`, then it has successfully initialized and
157 //! // enabled `foo`,
158 //! // - when it returns `Err(e)`, then it has cleaned up before
162 //! let foo = addr_of_mut!((*slot).foo);
164 //! // Initialize the `foo`
165 //! bindings::init_foo(Opaque::raw_get(foo));
168 //! let err = bindings::enable_foo(Opaque::raw_get(foo), flags);
170 //! // Enabling has failed, first clean up the foo and then return the error.
171 //! bindings::destroy_foo(Opaque::raw_get(foo));
185 //! // SAFETY: Since `foo` is initialized, destroying is safe.
186 //! unsafe { bindings::destroy_foo(self.foo.get()) };
191 //! For the special case where initializing a field is a single FFI-function call that cannot fail,
200 //! [pinning]: https://doc.rust-lang.org/std/pin/index.html
202 //! https://doc.rust-lang.org/std/pin/index.html#pinning-is-structural-for-field
205 //! [`impl PinInit<Foo>`]: PinInit
244 /// struct Foo {
255 /// stack_pin_init!(let foo = pin_init!(Foo {
256 /// a <- new_mutex!(42),
261 /// let foo: Pin<&mut Foo> = foo;
262 /// pr_info!("a: {}\n", &*foo.a.lock());
305 /// struct Foo {
315 /// stack_try_pin_init!(let foo: Result<Pin<&mut Foo>, AllocError> = pin_init!(Foo {
316 /// a <- new_mutex!(42),
321 /// let foo = foo.unwrap();
322 /// pr_info!("a: {}\n", &*foo.a.lock());
339 /// struct Foo {
349 /// stack_try_pin_init!(let foo: Pin<&mut Foo> =? pin_init!(Foo {
350 /// a <- new_mutex!(42),
355 /// pr_info!("a: {}\n", &*foo.a.lock());
378 /// Construct an in-place, pinned initializer for `struct`s.
389 /// struct Foo {
399 /// # fn demo() -> impl PinInit<Foo> {
402 /// let initializer = pin_init!(Foo {
420 /// # Init-functions
433 /// # struct Foo {
441 /// impl Foo {
442 /// fn new() -> impl PinInit<Self> {
453 /// Users of `Foo` can now create it like this:
460 /// # struct Foo {
468 /// # impl Foo {
469 /// # fn new() -> impl PinInit<Self> {
478 /// let foo = KBox::pin_init(Foo::new(), GFP_KERNEL);
487 /// # struct Foo {
495 /// # impl Foo {
496 /// # fn new() -> impl PinInit<Self> {
508 /// foo1: Foo,
510 /// foo2: Foo,
515 /// fn new(other: u32) -> impl PinInit<Self> {
517 /// foo1 <- Foo::new(),
518 /// foo2 <- Foo::new(),
525 /// Here we see that when using `pin_init!` with `PinInit`, one needs to write `<-` instead of `:`.
526 /// This signifies that the given field is initialized in-place. As with `struct` initializers, just
527 /// writing the field (in this case `other`) without `:` or `<-` means `other: other,`.
533 /// - Fields that you want to initialize in-place have to use `<-` instead of `:`.
534 /// - In front of the initializer you can write `&this in` to have access to a [`NonNull<Self>`]
536 /// - Using struct update syntax one can place `..Zeroable::zeroed()` at the very end of the
588 /// Construct an in-place, fallible pinned initializer for `struct`s.
615 /// fn new() -> impl PinInit<Self, Error> {
658 /// Construct an in-place initializer for `struct`s.
664 /// - `unsafe` code must guarantee either full initialization or return an error and allow
666 /// - the fields are initialized in the order given in the initializer.
667 /// - no references to fields are allowed to be created inside of the initializer.
669 /// This initializer is for initializing data in-place that might later be moved. If you want to
670 /// pin-initialize, use [`pin_init!`].
693 /// Construct an in-place fallible initializer for `struct`s.
701 /// - `unsafe` code must guarantee either full initialization or return an error and allow
703 /// - the fields are initialized in the order given in the initializer.
704 /// - no references to fields are allowed to be created inside of the initializer.
716 /// fn new() -> impl Init<Self, Error> {
793 /// struct Foo<T> {
798 /// impl<T> Foo<T> {
799 /// fn project(self: Pin<&mut Self>) -> Pin<&mut T> {
800 /// assert_pinned!(Foo<T>, elem, T, inline);
826 /// A pin-initializer for the type `T`.
840 /// - returns `Ok(())` if it initialized every field of `slot`,
841 /// - returns `Err(err)` if it encountered an error and then cleaned `slot`, this means:
842 /// - `slot` can be deallocated without UB occurring,
843 /// - `slot` does not need to be dropped,
844 /// - `slot` is not partially initialized.
845 /// - while constructing the `T` at `slot` it upholds the pinning invariants of `T`.
855 /// - `slot` is a valid pointer to uninitialized memory.
856 /// - the caller does not touch `slot` when `Err` is returned, they are only permitted to
858 /// - `slot` will not move until it is dropped, i.e. it will be pinned.
859 unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E>; in __pinned_init()
878 /// struct Foo {
883 /// impl Foo {
885 /// pr_info!("Setting up foo\n");
889 /// let foo = pin_init!(Foo {
891 /// raw <- unsafe {
896 /// }).pin_chain(|foo| {
897 /// foo.setup();
901 fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E> in pin_chain()
903 F: FnOnce(Pin<&mut T>) -> Result<(), E>, in pin_chain()
913 // - returns `Ok(())` on successful initialization,
914 // - returns `Err(err)` on error and in this case `slot` will be dropped.
915 // - considers `slot` pinned.
919 F: FnOnce(Pin<&mut T>) -> Result<(), E>,
921 unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> { in __pinned_init()
948 /// - returns `Ok(())` if it initialized every field of `slot`,
949 /// - returns `Err(err)` if it encountered an error and then cleaned `slot`, this means:
950 /// - `slot` can be deallocated without UB occurring,
951 /// - `slot` does not need to be dropped,
952 /// - `slot` is not partially initialized.
953 /// - while constructing the `T` at `slot` it upholds the pinning invariants of `T`.
968 /// - `slot` is a valid pointer to uninitialized memory.
969 /// - the caller does not touch `slot` when `Err` is returned, they are only permitted to
971 unsafe fn __init(self, slot: *mut T) -> Result<(), E>; in __init()
983 /// struct Foo {
987 /// impl Foo {
989 /// pr_info!("Setting up foo\n");
993 /// let foo = init!(Foo {
994 /// buf <- init::zeroed()
995 /// }).chain(|foo| {
996 /// foo.setup();
1000 fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E> in chain()
1002 F: FnOnce(&mut T) -> Result<(), E>, in chain()
1012 // - returns `Ok(())` on successful initialization,
1013 // - returns `Err(err)` on error and in this case `slot` will be dropped.
1017 F: FnOnce(&mut T) -> Result<(), E>,
1019 unsafe fn __init(self, slot: *mut T) -> Result<(), E> { in __init()
1033 F: FnOnce(&mut T) -> Result<(), E>,
1035 unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> { in __pinned_init()
1046 /// - returns `Ok(())` if it initialized every field of `slot`,
1047 /// - returns `Err(err)` if it encountered an error and then cleaned `slot`, this means:
1048 /// - `slot` can be deallocated without UB occurring,
1049 /// - `slot` does not need to be dropped,
1050 /// - `slot` is not partially initialized.
1051 /// - may assume that the `slot` does not move if `T: !Unpin`,
1052 /// - while constructing the `T` at `slot` it upholds the pinning invariants of `T`.
1055 f: impl FnOnce(*mut T) -> Result<(), E>, in pin_init_from_closure()
1056 ) -> impl PinInit<T, E> { in pin_init_from_closure()
1065 /// - returns `Ok(())` if it initialized every field of `slot`,
1066 /// - returns `Err(err)` if it encountered an error and then cleaned `slot`, this means:
1067 /// - `slot` can be deallocated without UB occurring,
1068 /// - `slot` does not need to be dropped,
1069 /// - `slot` is not partially initialized.
1070 /// - the `slot` may move after initialization.
1071 /// - while constructing the `T` at `slot` it upholds the pinning invariants of `T`.
1074 f: impl FnOnce(*mut T) -> Result<(), E>, in init_from_closure()
1075 ) -> impl Init<T, E> { in init_from_closure()
1081 /// The initializer is a no-op. The `slot` memory is not changed.
1083 pub fn uninit<T, E>() -> impl Init<MaybeUninit<T>, E> { in uninit()
1100 mut make_init: impl FnMut(usize) -> I, in init_array_from_fn()
1101 ) -> impl Init<[T; N], E> in init_array_from_fn()
1145 mut make_init: impl FnMut(usize) -> I, in pin_init_array_from_fn()
1146 ) -> impl PinInit<[T; N], E> in pin_init_array_from_fn()
1178 // SAFETY: Every type can be initialized by-value.
1180 unsafe fn __init(self, slot: *mut T) -> Result<(), E> { in __init()
1187 // SAFETY: Every type can be initialized by-value. `__pinned_init` calls `__init`.
1189 unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> { in __pinned_init()
1195 /// Smart pointer that can initialize memory in-place.
1203 /// Use the given pin-initializer to pin-initialize a `T` inside of a new smart pointer of this
1207 fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Self::PinnedSelf, E> in try_pin_init()
1211 /// Use the given pin-initializer to pin-initialize a `T` inside of a new smart pointer of this
1215 fn pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> error::Result<Self::PinnedSelf> in pin_init()
1226 /// Use the given initializer to in-place initialize a `T`.
1227 fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E> in try_init()
1231 /// Use the given initializer to in-place initialize a `T`.
1232 fn init<E>(init: impl Init<T, E>, flags: Flags) -> error::Result<Self> in init()
1248 fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Self::PinnedSelf, E> in try_pin_init()
1256 fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E> in try_init()
1268 fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Self::PinnedSelf, E> in try_pin_init()
1276 fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E> in try_init()
1292 fn write_init<E>(self, init: impl Init<T, E>) -> Result<Self::Initialized, E>; in write_init()
1294 /// Use the given pin-initializer to write a value into `self`.
1297 fn write_pin_init<E>(self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E>; in write_pin_init()
1303 fn write_init<E>(mut self, init: impl Init<T, E>) -> Result<Self::Initialized, E> { in write_init()
1312 fn write_pin_init<E>(mut self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E> { in write_pin_init()
1331 /// struct Foo {
1337 /// impl PinnedDrop for Foo {
1339 /// pr_info!("Foo is being dropped!\n");
1346 /// This trait must be implemented via the [`pinned_drop`] proc-macro attribute on the impl.
1356 /// This extra parameter will be generated by the `#[pinned_drop]` proc-macro attribute
1377 pub fn zeroed<T: Zeroable>() -> impl Init<T> { in zeroed()
1406 // <https://doc.rust-lang.org/stable/nomicon/exotic-sizes.html#empty-types>. The Rust Reference
1408 // <https://doc.rust-lang.org/stable/reference/behavior-considered-undefined.html>.
1422 // https://doc.rust-lang.org/stable/std/option/index.html#representation).