Skip to content

Commit ce6b880

Browse files
committed
uefi: Add slice(_mut) accessors to AlignedBuffer
1 parent ae1d072 commit ce6b880

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

uefi/src/mem/aligned_buffer.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
use alloc::alloc::{Layout, LayoutError, alloc, dealloc};
44
use core::error::Error;
5-
use core::fmt;
65
use core::ptr::NonNull;
6+
use core::{fmt, slice};
77

88
/// Helper class to maintain the lifetime of a memory region allocated with a non-standard alignment.
99
/// Facilitates RAII to properly deallocate when lifetime of the object ends.
@@ -51,6 +51,18 @@ impl AlignedBuffer {
5151
self.ptr.as_ptr()
5252
}
5353

54+
/// Get the underlying memory region as immutable slice.
55+
#[must_use]
56+
pub const fn as_slice(&self) -> &[u8] {
57+
unsafe { slice::from_raw_parts(self.ptr(), self.size()) }
58+
}
59+
60+
/// Get the underlying memory region as mutable slice.
61+
#[must_use]
62+
pub const fn as_slice_mut(&mut self) -> &mut [u8] {
63+
unsafe { slice::from_raw_parts_mut(self.ptr_mut(), self.size()) }
64+
}
65+
5466
/// Get the size of the aligned memory region managed by this instance.
5567
#[must_use]
5668
pub const fn size(&self) -> usize {

0 commit comments

Comments
 (0)