Skip to content

Commit

Permalink
Remove Page::copy()
Browse files Browse the repository at this point in the history
The approach in `Pages::copy()` is better.

Signed-off-by: Nathaniel McCallum <nathaniel@congru.us>
  • Loading branch information
npmccallum committed Sep 7, 2021
1 parent dffefe3 commit 1b9a555
Showing 1 changed file with 0 additions and 23 deletions.
23 changes: 0 additions & 23 deletions src/page.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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 Down Expand Up @@ -77,26 +76,4 @@ impl Page {
pub const fn zeroed() -> Self {
Self([[0; 32]; 16])
}

/// Copy a value into the start of a page
///
/// All unused bytes are zero.
///
/// # Panics
///
/// This function panics if any of these constraints are false:
/// 1. `size_of::<Page>() >= size_of_val(&value)`
/// 2. `align_of::<Page>() >= align_of_val(&value)`
/// 3. `align_of::<Page>() % align_of_val(&value) == 0`
pub fn copy<T: Copy>(value: T) -> Page {
assert!(size_of::<Page>() >= size_of_val(&value));
assert!(align_of::<Page>() >= align_of_val(&value));
assert!(align_of::<Page>() % align_of_val(&value) == 0);

let mut pages = [Page::default()];
let bytes = unsafe { pages.align_to_mut::<u8>().1 };
let typed = unsafe { bytes.align_to_mut().1 };
typed[0] = value;
pages[0]
}
}

0 comments on commit 1b9a555

Please sign in to comment.