Skip to content

Commit c86972d

Browse files
committed
acpi: make ManagedSlice a little more readable
Rearranging the imported names was necessary to add the `alloc` feature anyways, and I thought this looked a little neater while I was here.
1 parent b2a5f4a commit c86972d

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

acpi/src/managed_slice.rs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,55 @@
1-
use core::{alloc, mem};
1+
use crate::{AcpiError, AcpiResult};
2+
use core::{
3+
alloc::{Allocator, Layout},
4+
mem,
5+
ptr::NonNull,
6+
};
27

38
/// Thin wrapper around a regular slice, taking a reference to an allocator for automatic
49
/// deallocation when the slice is dropped out of scope.
510
#[derive(Debug)]
611
pub struct ManagedSlice<'a, T, A>
712
where
8-
A: alloc::Allocator,
13+
A: Allocator,
914
{
1015
slice: &'a mut [T],
1116
allocator: A,
1217
}
1318

1419
impl<'a, T, A> ManagedSlice<'a, T, A>
1520
where
16-
A: alloc::Allocator,
21+
A: Allocator,
1722
{
18-
/// Attempts to allocate a new `&mut [T]` in the given allocator.
19-
pub fn new_in(len: usize, allocator: A) -> crate::AcpiResult<Self> {
20-
// Safety: Type automatically deallocated memory on `Drop` and;
21-
// Constructed slice is from valid, aligned, allocated memory.
22-
unsafe {
23-
allocator
24-
.allocate(alloc::Layout::array::<T>(len).map_err(|_| crate::AcpiError::AllocError)?)
25-
.map(|mut ptr| core::slice::from_raw_parts_mut(ptr.as_mut().as_mut_ptr().cast(), len))
26-
.map(|slice| Self { slice, allocator })
27-
.map_err(|_| crate::AcpiError::AllocError)
23+
/// Attempt to allocate a new `ManagedSlice` that holds `len` `T`s.
24+
pub fn new_in(len: usize, allocator: A) -> AcpiResult<Self> {
25+
let layout = Layout::array::<T>(len).map_err(|_| AcpiError::AllocError)?;
26+
match allocator.allocate(layout) {
27+
Ok(mut ptr) => {
28+
let slice = unsafe { core::slice::from_raw_parts_mut(ptr.as_mut().as_mut_ptr().cast(), len) };
29+
Ok(ManagedSlice { slice, allocator })
30+
}
31+
Err(_) => Err(AcpiError::AllocError),
2832
}
2933
}
3034
}
3135

3236
impl<'a, T, A> Drop for ManagedSlice<'a, T, A>
3337
where
34-
A: alloc::Allocator,
38+
A: Allocator,
3539
{
3640
fn drop(&mut self) {
37-
// Safety: Slice is required by function to point to non-null memory.
38-
let slice_ptr = unsafe { core::ptr::NonNull::new_unchecked(self.slice.as_ptr().cast_mut().cast::<u8>()) };
39-
// Safety: Slice is constructed from a valid layout.
40-
let slice_layout = unsafe {
41-
alloc::Layout::from_size_align_unchecked(mem::size_of_val(self.slice), mem::align_of_val(self.slice))
42-
};
43-
44-
// Safety: Caller is required to provide a slice allocated with the provided allocator.
45-
unsafe { self.allocator.deallocate(slice_ptr, slice_layout) };
41+
unsafe {
42+
let slice_ptr = NonNull::new_unchecked(self.slice.as_ptr().cast_mut().cast::<u8>());
43+
let slice_layout =
44+
Layout::from_size_align_unchecked(mem::size_of_val(self.slice), mem::align_of_val(self.slice));
45+
self.allocator.deallocate(slice_ptr, slice_layout);
46+
}
4647
}
4748
}
4849

4950
impl<'a, T, A> core::ops::Deref for ManagedSlice<'a, T, A>
5051
where
51-
A: alloc::Allocator,
52+
A: Allocator,
5253
{
5354
type Target = [T];
5455

@@ -59,7 +60,7 @@ where
5960

6061
impl<'a, T, A> core::ops::DerefMut for ManagedSlice<'a, T, A>
6162
where
62-
A: alloc::Allocator,
63+
A: Allocator,
6364
{
6465
fn deref_mut(&mut self) -> &mut Self::Target {
6566
self.slice

0 commit comments

Comments
 (0)