Skip to content

Commit c0a9637

Browse files
committed
Clean up Page traits
Page now implements Borrow, Deref and AsRef; along with their mutable variants. Signed-off-by: Nathaniel McCallum <[email protected]>
1 parent f874251 commit c0a9637

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/page.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: Apache-2.0
22

3+
use core::borrow::{Borrow, BorrowMut};
34
use core::mem::{align_of, align_of_val, size_of, size_of_val};
5+
use core::ops::{Deref, DerefMut};
46

57
/// A single page of memory
68
///
@@ -15,23 +17,56 @@ impl const_default::ConstDefault for Page {
1517
}
1618

1719
impl Default for Page {
20+
#[inline]
1821
fn default() -> Self {
19-
Self([[0; 32]; 16])
22+
Self::zeroed()
23+
}
24+
}
25+
26+
impl Deref for Page {
27+
type Target = [u8];
28+
29+
#[inline]
30+
fn deref(&self) -> &Self::Target {
31+
unsafe { self.0.align_to().1 }
32+
}
33+
}
34+
35+
impl DerefMut for Page {
36+
#[inline]
37+
fn deref_mut(&mut self) -> &mut Self::Target {
38+
unsafe { self.0.align_to_mut().1 }
2039
}
2140
}
2241

2342
impl AsRef<[u8]> for Page {
43+
#[inline]
2444
fn as_ref(&self) -> &[u8] {
2545
unsafe { self.0.align_to().1 }
2646
}
2747
}
2848

2949
impl AsMut<[u8]> for Page {
50+
#[inline]
3051
fn as_mut(&mut self) -> &mut [u8] {
3152
unsafe { self.0.align_to_mut().1 }
3253
}
3354
}
3455

56+
impl Borrow<[u8]> for Page {
57+
#[inline]
58+
fn borrow(&self) -> &[u8] {
59+
unsafe { self.0.align_to().1 }
60+
}
61+
}
62+
63+
impl BorrowMut<[u8]> for Page {
64+
#[inline]
65+
fn borrow_mut(&mut self) -> &mut [u8] {
66+
unsafe { self.0.align_to_mut().1 }
67+
}
68+
}
69+
3570
impl Page {
3671
/// Returns the size of the page in bytes
3772
pub const fn size() -> usize {

0 commit comments

Comments
 (0)