mirror of
https://gitee.com/bianbu-linux/linux-6.6
synced 2025-04-24 14:07:52 -04:00
rust: delete ForeignOwnable::borrow_mut
We discovered that the current design of `borrow_mut` is problematic.
This patch removes it until a better solution can be found.
Specifically, the current design gives you access to a `&mut T`, which
lets you change where the `ForeignOwnable` points (e.g., with
`core::mem::swap`). No upcoming user of this API intended to make that
possible, making all of them unsound.
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
Fixes: 0fc4424d24
("rust: types: introduce `ForeignOwnable`")
Link: https://lore.kernel.org/r/20230706094615.3080784-1-aliceryhl@google.com
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This commit is contained in:
parent
b3d8aa84bb
commit
1d24eb2d53
2 changed files with 3 additions and 22 deletions
|
@ -243,8 +243,7 @@ impl<T: 'static> ForeignOwnable for Arc<T> {
|
||||||
let inner = NonNull::new(ptr as *mut ArcInner<T>).unwrap();
|
let inner = NonNull::new(ptr as *mut ArcInner<T>).unwrap();
|
||||||
|
|
||||||
// SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
|
// SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
|
||||||
// for the lifetime of the returned value. Additionally, the safety requirements of
|
// for the lifetime of the returned value.
|
||||||
// `ForeignOwnable::borrow_mut` ensure that no new mutable references are created.
|
|
||||||
unsafe { ArcBorrow::new(inner) }
|
unsafe { ArcBorrow::new(inner) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,34 +35,16 @@ pub trait ForeignOwnable: Sized {
|
||||||
///
|
///
|
||||||
/// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
|
/// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
|
||||||
/// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
|
/// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
|
||||||
/// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow_mut`]
|
|
||||||
/// for this object must have been dropped.
|
|
||||||
unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Self::Borrowed<'a>;
|
unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Self::Borrowed<'a>;
|
||||||
|
|
||||||
/// Mutably borrows a foreign-owned object.
|
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
|
|
||||||
/// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
|
|
||||||
/// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] and
|
|
||||||
/// [`ForeignOwnable::borrow_mut`] for this object must have been dropped.
|
|
||||||
unsafe fn borrow_mut(ptr: *const core::ffi::c_void) -> ScopeGuard<Self, fn(Self)> {
|
|
||||||
// SAFETY: The safety requirements ensure that `ptr` came from a previous call to
|
|
||||||
// `into_foreign`.
|
|
||||||
ScopeGuard::new_with_data(unsafe { Self::from_foreign(ptr) }, |d| {
|
|
||||||
d.into_foreign();
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Converts a foreign-owned object back to a Rust-owned one.
|
/// Converts a foreign-owned object back to a Rust-owned one.
|
||||||
///
|
///
|
||||||
/// # Safety
|
/// # Safety
|
||||||
///
|
///
|
||||||
/// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
|
/// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
|
||||||
/// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
|
/// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
|
||||||
/// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] and
|
/// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] for
|
||||||
/// [`ForeignOwnable::borrow_mut`] for this object must have been dropped.
|
/// this object must have been dropped.
|
||||||
unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self;
|
unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue