Skip to content

Commit c6b9753

Browse files
tamirdojeda
authored andcommitted
rust: kernel: reorder ForeignOwnable items
`{into,from}_foreign` before `borrow` is slightly more logical. This removes an inconsistency with `kbox.rs` which already uses this ordering. Reviewed-by: Alice Ryhl <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Signed-off-by: Tamir Duberstein <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Reworded title slightly. - Miguel ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 1468657 commit c6b9753

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

rust/kernel/sync/arc.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,25 +349,25 @@ impl<T: 'static> ForeignOwnable for Arc<T> {
349349
ManuallyDrop::new(self).ptr.as_ptr().cast()
350350
}
351351

352-
unsafe fn borrow<'a>(ptr: *mut crate::ffi::c_void) -> ArcBorrow<'a, T> {
352+
unsafe fn from_foreign(ptr: *mut crate::ffi::c_void) -> Self {
353353
// SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
354354
// call to `Self::into_foreign`.
355355
let inner = unsafe { NonNull::new_unchecked(ptr.cast::<ArcInner<T>>()) };
356356

357-
// SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
358-
// for the lifetime of the returned value.
359-
unsafe { ArcBorrow::new(inner) }
357+
// SAFETY: By the safety requirement of this function, we know that `ptr` came from
358+
// a previous call to `Arc::into_foreign`, which guarantees that `ptr` is valid and
359+
// holds a reference count increment that is transferrable to us.
360+
unsafe { Self::from_inner(inner) }
360361
}
361362

362-
unsafe fn from_foreign(ptr: *mut crate::ffi::c_void) -> Self {
363+
unsafe fn borrow<'a>(ptr: *mut crate::ffi::c_void) -> ArcBorrow<'a, T> {
363364
// SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
364365
// call to `Self::into_foreign`.
365366
let inner = unsafe { NonNull::new_unchecked(ptr.cast::<ArcInner<T>>()) };
366367

367-
// SAFETY: By the safety requirement of this function, we know that `ptr` came from
368-
// a previous call to `Arc::into_foreign`, which guarantees that `ptr` is valid and
369-
// holds a reference count increment that is transferrable to us.
370-
unsafe { Self::from_inner(inner) }
368+
// SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
369+
// for the lifetime of the returned value.
370+
unsafe { ArcBorrow::new(inner) }
371371
}
372372
}
373373

rust/kernel/types.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@ pub trait ForeignOwnable: Sized {
3131
/// [`ForeignOwnable::try_from_foreign`] can result in undefined behavior.
3232
fn into_foreign(self) -> *mut crate::ffi::c_void;
3333

34-
/// Borrows a foreign-owned object.
35-
///
36-
/// # Safety
37-
///
38-
/// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
39-
/// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
40-
unsafe fn borrow<'a>(ptr: *mut crate::ffi::c_void) -> Self::Borrowed<'a>;
41-
4234
/// Converts a foreign-owned object back to a Rust-owned one.
4335
///
4436
/// # Safety
@@ -67,6 +59,14 @@ pub trait ForeignOwnable: Sized {
6759
unsafe { Some(Self::from_foreign(ptr)) }
6860
}
6961
}
62+
63+
/// Borrows a foreign-owned object.
64+
///
65+
/// # Safety
66+
///
67+
/// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
68+
/// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
69+
unsafe fn borrow<'a>(ptr: *mut crate::ffi::c_void) -> Self::Borrowed<'a>;
7070
}
7171

7272
impl ForeignOwnable for () {
@@ -76,9 +76,9 @@ impl ForeignOwnable for () {
7676
core::ptr::NonNull::dangling().as_ptr()
7777
}
7878

79-
unsafe fn borrow<'a>(_: *mut crate::ffi::c_void) -> Self::Borrowed<'a> {}
80-
8179
unsafe fn from_foreign(_: *mut crate::ffi::c_void) -> Self {}
80+
81+
unsafe fn borrow<'a>(_: *mut crate::ffi::c_void) -> Self::Borrowed<'a> {}
8282
}
8383

8484
/// Runs a cleanup function/closure when dropped.

0 commit comments

Comments
 (0)