[PATCH v4 13/28] rust: alloc: import kernel `Box` type in types.rs

From: Danilo Krummrich
Date: Mon Aug 05 2024 - 11:23:41 EST


Now that we removed `BoxExt` and the corresponding includes in
types.rs, add the new kernel `Box` type instead.

Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
---
rust/kernel/types.rs | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index b80a3b2b62ac..25761da5a816 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -2,7 +2,7 @@

//! Kernel types.

-use crate::alloc::Allocator;
+use crate::alloc::{Allocator, Box};
use crate::init::{self, PinInit};
use core::{
cell::UnsafeCell,
@@ -68,14 +68,14 @@ unsafe fn try_from_foreign(ptr: *const core::ffi::c_void) -> Option<Self> {
}
}

-impl<T: 'static, A> ForeignOwnable for crate::alloc::Box<T, A>
+impl<T: 'static, A> ForeignOwnable for Box<T, A>
where
A: Allocator,
{
type Borrowed<'a> = &'a T;

fn into_foreign(self) -> *const core::ffi::c_void {
- crate::alloc::Box::into_raw(self) as _
+ Box::into_raw(self) as _
}

unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> &'a T {
@@ -89,11 +89,11 @@ unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> &'a T {
unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
// SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
// call to `Self::into_foreign`.
- unsafe { crate::alloc::Box::from_raw(ptr as _) }
+ unsafe { Box::from_raw(ptr as _) }
}
}

-impl<T: 'static, A> ForeignOwnable for Pin<crate::alloc::Box<T, A>>
+impl<T: 'static, A> ForeignOwnable for Pin<Box<T, A>>
where
A: Allocator,
{
@@ -101,7 +101,7 @@ impl<T: 'static, A> ForeignOwnable for Pin<crate::alloc::Box<T, A>>

fn into_foreign(self) -> *const core::ffi::c_void {
// SAFETY: We are still treating the box as pinned.
- crate::alloc::Box::into_raw(unsafe { Pin::into_inner_unchecked(self) }) as _
+ Box::into_raw(unsafe { Pin::into_inner_unchecked(self) }) as _
}

unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Pin<&'a T> {
@@ -118,7 +118,7 @@ unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Pin<&'a T> {
unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
// SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
// call to `Self::into_foreign`.
- unsafe { Pin::new_unchecked(crate::alloc::Box::from_raw(ptr as _)) }
+ unsafe { Pin::new_unchecked(Box::from_raw(ptr as _)) }
}
}

--
2.45.2