Skip to content

Commit

Permalink
Merge pull request torvalds#553 from wedsonaf/pin-uniqueref
Browse files Browse the repository at this point in the history
rust: add functions to pin `UniqueRef` and convert it to `Ref`.
  • Loading branch information
wedsonaf authored Nov 15, 2021
2 parents e2df391 + 370b329 commit d97b99b
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions rust/kernel/sync/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,21 @@ impl<T: ?Sized> From<UniqueRef<T>> for Ref<T> {
}
}

impl<T: ?Sized> From<UniqueRef<T>> for Pin<UniqueRef<T>> {
fn from(obj: UniqueRef<T>) -> Self {
// SAFETY: It is not possible to move/replace `T` inside a `Pin<UniqueRef<T>>` (unless `T`
// is `Unpin`), so it is ok to convert it to `Pin<UniqueRef<T>>`.
unsafe { Pin::new_unchecked(obj) }
}
}

impl<T: ?Sized> From<Pin<UniqueRef<T>>> for Ref<T> {
fn from(item: Pin<UniqueRef<T>>) -> Self {
// SAFETY: The type invariants of `Ref` guarantee that the data is pinned.
unsafe { Pin::into_inner_unchecked(item).inner }
}
}

/// A borrowed [`Ref`] with manually-managed lifetime.
///
/// # Invariants
Expand Down

0 comments on commit d97b99b

Please sign in to comment.