Skip to content

Commit ba3a00c

Browse files
Danilo Krummrichfbq
authored andcommitted
rust: alloc: remove BoxExt extension
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 <[email protected]> Signed-off-by: Danilo Krummrich <[email protected]> Link: https://lore.kernel.org/r/[email protected] [boqun: Adjust with the introduction of InPlaceInit::PinnedSelf]
1 parent ab4aed5 commit ba3a00c

File tree

6 files changed

+3
-184
lines changed

6 files changed

+3
-184
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 & 85 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,
@@ -590,7 +589,6 @@ macro_rules! pin_init {
590589
/// # Examples
591590
///
592591
/// ```rust
593-
/// # #![feature(new_uninit)]
594592
/// use kernel::{init::{self, PinInit}, error::Error};
595593
/// #[pin_data]
596594
/// struct BigBuf {
@@ -1244,26 +1242,6 @@ impl<T> InPlaceInit<T> for Arc<T> {
12441242
}
12451243
}
12461244

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

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

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

rust/kernel/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#![no_std]
1515
#![feature(coerce_unsized)]
1616
#![feature(dispatch_from_dyn)]
17-
#![feature(new_uninit)]
1817
#![feature(receiver_trait)]
1918
#![feature(unsize)]
2019

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)