Skip to content

Commit e8c6ccd

Browse files
Danilo Krummrichojeda
authored andcommitted
rust: alloc: remove extension of std's Box
Now that all existing `Box` users were moved to the kernel `Box` type, remove the `BoxExt` extension and all other related extensions. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20241004154149.93856-14-dakr@kernel.org Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 8373147 commit e8c6ccd

File tree

6 files changed

+3
-188
lines changed

6 files changed

+3
-188
lines changed

rust/kernel/alloc.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
55
#[cfg(not(any(test, testlib)))]
66
pub mod allocator;
7-
pub mod box_ext;
87
pub mod kbox;
98
pub mod vec_ext;
109

rust/kernel/alloc/box_ext.rs

Lines changed: 0 additions & 89 deletions
This file was deleted.

rust/kernel/init.rs

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,12 @@
211211
//! [`pin_init!`]: crate::pin_init!
212212
213213
use crate::{
214-
alloc::{box_ext::BoxExt, AllocError, Flags, KBox},
214+
alloc::{AllocError, Flags, KBox},
215215
error::{self, Error},
216216
sync::Arc,
217217
sync::UniqueArc,
218218
types::{Opaque, ScopeGuard},
219219
};
220-
use alloc::boxed::Box;
221220
use core::{
222221
cell::UnsafeCell,
223222
convert::Infallible,
@@ -588,7 +587,6 @@ macro_rules! pin_init {
588587
/// # Examples
589588
///
590589
/// ```rust
591-
/// # #![feature(new_uninit)]
592590
/// use kernel::{init::{self, PinInit}, error::Error};
593591
/// #[pin_data]
594592
/// struct BigBuf {
@@ -1245,26 +1243,6 @@ impl<T> InPlaceInit<T> for Arc<T> {
12451243
}
12461244
}
12471245

1248-
impl<T> InPlaceInit<T> for Box<T> {
1249-
type PinnedSelf = Pin<Self>;
1250-
1251-
#[inline]
1252-
fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Self::PinnedSelf, E>
1253-
where
1254-
E: From<AllocError>,
1255-
{
1256-
<Box<_> as BoxExt<_>>::new_uninit(flags)?.write_pin_init(init)
1257-
}
1258-
1259-
#[inline]
1260-
fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
1261-
where
1262-
E: From<AllocError>,
1263-
{
1264-
<Box<_> as BoxExt<_>>::new_uninit(flags)?.write_init(init)
1265-
}
1266-
}
1267-
12681246
impl<T> InPlaceInit<T> for UniqueArc<T> {
12691247
type PinnedSelf = Pin<Self>;
12701248

@@ -1301,28 +1279,6 @@ pub trait InPlaceWrite<T> {
13011279
fn write_pin_init<E>(self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E>;
13021280
}
13031281

1304-
impl<T> InPlaceWrite<T> for Box<MaybeUninit<T>> {
1305-
type Initialized = Box<T>;
1306-
1307-
fn write_init<E>(mut self, init: impl Init<T, E>) -> Result<Self::Initialized, E> {
1308-
let slot = self.as_mut_ptr();
1309-
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1310-
// slot is valid.
1311-
unsafe { init.__init(slot)? };
1312-
// SAFETY: All fields have been initialized.
1313-
Ok(unsafe { self.assume_init() })
1314-
}
1315-
1316-
fn write_pin_init<E>(mut self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E> {
1317-
let slot = self.as_mut_ptr();
1318-
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1319-
// slot is valid and will not be moved, because we pin it later.
1320-
unsafe { init.__pinned_init(slot)? };
1321-
// SAFETY: All fields have been initialized.
1322-
Ok(unsafe { self.assume_init() }.into())
1323-
}
1324-
}
1325-
13261282
impl<T> InPlaceWrite<T> for UniqueArc<MaybeUninit<T>> {
13271283
type Initialized = UniqueArc<T>;
13281284

rust/kernel/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#![feature(coerce_unsized)]
1717
#![feature(dispatch_from_dyn)]
1818
#![feature(lint_reasons)]
19-
#![feature(new_uninit)]
2019
#![feature(unsize)]
2120

2221
// Ensure conditional compilation based on the kernel configuration works;

rust/kernel/prelude.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
#[doc(no_inline)]
1515
pub use core::pin::Pin;
1616

17-
pub use crate::alloc::{box_ext::BoxExt, flags::*, vec_ext::VecExt, KBox, KVBox, VBox};
17+
pub use crate::alloc::{flags::*, vec_ext::VecExt, KBox, KVBox, VBox};
1818

1919
#[doc(no_inline)]
20-
pub use alloc::{boxed::Box, vec::Vec};
20+
pub use alloc::vec::Vec;
2121

2222
#[doc(no_inline)]
2323
pub use macros::{module, pin_data, pinned_drop, vtable, Zeroable};

rust/kernel/types.rs

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
//! Kernel types.
44
55
use crate::init::{self, PinInit};
6-
use alloc::boxed::Box;
76
use core::{
87
cell::UnsafeCell,
98
marker::{PhantomData, PhantomPinned},
109
mem::{ManuallyDrop, MaybeUninit},
1110
ops::{Deref, DerefMut},
12-
pin::Pin,
1311
ptr::NonNull,
1412
};
1513

@@ -71,54 +69,6 @@ pub trait ForeignOwnable: Sized {
7169
}
7270
}
7371

74-
impl<T: 'static> ForeignOwnable for Box<T> {
75-
type Borrowed<'a> = &'a T;
76-
77-
fn into_foreign(self) -> *const core::ffi::c_void {
78-
Box::into_raw(self) as _
79-
}
80-
81-
unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> &'a T {
82-
// SAFETY: The safety requirements for this function ensure that the object is still alive,
83-
// so it is safe to dereference the raw pointer.
84-
// The safety requirements of `from_foreign` also ensure that the object remains alive for
85-
// the lifetime of the returned value.
86-
unsafe { &*ptr.cast() }
87-
}
88-
89-
unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
90-
// SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
91-
// call to `Self::into_foreign`.
92-
unsafe { Box::from_raw(ptr as _) }
93-
}
94-
}
95-
96-
impl<T: 'static> ForeignOwnable for Pin<Box<T>> {
97-
type Borrowed<'a> = Pin<&'a T>;
98-
99-
fn into_foreign(self) -> *const core::ffi::c_void {
100-
// SAFETY: We are still treating the box as pinned.
101-
Box::into_raw(unsafe { Pin::into_inner_unchecked(self) }) as _
102-
}
103-
104-
unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Pin<&'a T> {
105-
// SAFETY: The safety requirements for this function ensure that the object is still alive,
106-
// so it is safe to dereference the raw pointer.
107-
// The safety requirements of `from_foreign` also ensure that the object remains alive for
108-
// the lifetime of the returned value.
109-
let r = unsafe { &*ptr.cast() };
110-
111-
// SAFETY: This pointer originates from a `Pin<Box<T>>`.
112-
unsafe { Pin::new_unchecked(r) }
113-
}
114-
115-
unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
116-
// SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
117-
// call to `Self::into_foreign`.
118-
unsafe { Pin::new_unchecked(Box::from_raw(ptr as _)) }
119-
}
120-
}
121-
12272
impl ForeignOwnable for () {
12373
type Borrowed<'a> = ();
12474

0 commit comments

Comments
 (0)