Skip to content

Commit

Permalink
Clean up Page traits
Browse files Browse the repository at this point in the history
Page now implements Borrow, Deref and AsRef; along with their mutable
variants.

Signed-off-by: Nathaniel McCallum <nathaniel@congru.us>
  • Loading branch information
npmccallum committed Sep 7, 2021
1 parent f874251 commit c0a9637
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/page.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: Apache-2.0

use core::borrow::{Borrow, BorrowMut};
use core::mem::{align_of, align_of_val, size_of, size_of_val};
use core::ops::{Deref, DerefMut};

/// A single page of memory
///
Expand All @@ -15,23 +17,56 @@ impl const_default::ConstDefault for Page {
}

impl Default for Page {
#[inline]
fn default() -> Self {
Self([[0; 32]; 16])
Self::zeroed()
}
}

impl Deref for Page {
type Target = [u8];

#[inline]
fn deref(&self) -> &Self::Target {
unsafe { self.0.align_to().1 }
}
}

impl DerefMut for Page {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { self.0.align_to_mut().1 }
}
}

impl AsRef<[u8]> for Page {
#[inline]
fn as_ref(&self) -> &[u8] {
unsafe { self.0.align_to().1 }
}
}

impl AsMut<[u8]> for Page {
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
unsafe { self.0.align_to_mut().1 }
}
}

impl Borrow<[u8]> for Page {
#[inline]
fn borrow(&self) -> &[u8] {
unsafe { self.0.align_to().1 }
}
}

impl BorrowMut<[u8]> for Page {
#[inline]
fn borrow_mut(&mut self) -> &mut [u8] {
unsafe { self.0.align_to_mut().1 }
}
}

impl Page {
/// Returns the size of the page in bytes
pub const fn size() -> usize {
Expand Down

0 comments on commit c0a9637

Please sign in to comment.