Skip to content

Commit 370b329

Browse files
committed
rust: add functions to pin UniqueRef and convert it to Ref.
This allows us to initialise without the need for closures (which has simpler syntax) by using the following sequence: 1. Create a `UniqueRef` 2. Initialise all fields that don't need pinning 3. Convert `UniqueRef` to `Pin<UniqueRef>` 4. Initialise all fields that require pinning 5. Convert `Pin<UniqueRef>` to `Ref` The next patch converts all users of `Ref::try_new_and_init` and `RefUnique::pin_init_and_share` to use parts of the sequence above. Signed-off-by: Wedson Almeida Filho <[email protected]>
1 parent 4946eec commit 370b329

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

rust/kernel/sync/arc.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,21 @@ impl<T: ?Sized> From<UniqueRef<T>> for Ref<T> {
304304
}
305305
}
306306

307+
impl<T: ?Sized> From<UniqueRef<T>> for Pin<UniqueRef<T>> {
308+
fn from(obj: UniqueRef<T>) -> Self {
309+
// SAFETY: It is not possible to move/replace `T` inside a `Pin<UniqueRef<T>>` (unless `T`
310+
// is `Unpin`), so it is ok to convert it to `Pin<UniqueRef<T>>`.
311+
unsafe { Pin::new_unchecked(obj) }
312+
}
313+
}
314+
315+
impl<T: ?Sized> From<Pin<UniqueRef<T>>> for Ref<T> {
316+
fn from(item: Pin<UniqueRef<T>>) -> Self {
317+
// SAFETY: The type invariants of `Ref` guarantee that the data is pinned.
318+
unsafe { Pin::into_inner_unchecked(item).inner }
319+
}
320+
}
321+
307322
/// A borrowed [`Ref`] with manually-managed lifetime.
308323
///
309324
/// # Invariants

0 commit comments

Comments
 (0)