Skip to content

Commit d97b99b

Browse files
authored
Merge pull request #553 from wedsonaf/pin-uniqueref
rust: add functions to pin `UniqueRef` and convert it to `Ref`.
2 parents e2df391 + 370b329 commit d97b99b

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
@@ -298,6 +298,21 @@ impl<T: ?Sized> From<UniqueRef<T>> for Ref<T> {
298298
}
299299
}
300300

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

0 commit comments

Comments
 (0)