Skip to content

Commit 8dd7bf4

Browse files
committed
Cleanup const_fn!
Fixes #222 The following methods can be made `const` on stable: - `GlobalDescriptorTable::from_raw_slice` - `MappedFrame::{start_address, size}` - `Page<Size4KiB>::p1_index` The remaining functions still need `const_fn` because: - Some GDT methods use `&mut self` - The IDT uses function pointers as a type argument - The PageSize marker trait is used all over Comments were updated where appropriate. Signed-off-by: Joe Richey <[email protected]>
1 parent f232228 commit 8dd7bf4

File tree

6 files changed

+25
-39
lines changed

6 files changed

+25
-39
lines changed

src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
//! and access to various system registers.
33
44
#![cfg_attr(not(test), no_std)]
5-
#![cfg_attr(feature = "const_fn", feature(const_panic))]
6-
#![cfg_attr(feature = "const_fn", feature(const_mut_refs))]
7-
#![cfg_attr(feature = "const_fn", feature(const_fn_fn_ptr_basics))]
8-
#![cfg_attr(feature = "const_fn", feature(const_fn_trait_bound))]
5+
#![cfg_attr(feature = "const_fn", feature(const_panic))] // Better panic messages
6+
#![cfg_attr(feature = "const_fn", feature(const_mut_refs))] // GDT add_entry()
7+
#![cfg_attr(feature = "const_fn", feature(const_fn_fn_ptr_basics))] // IDT new()
8+
#![cfg_attr(feature = "const_fn", feature(const_fn_trait_bound))] // PageSize marker trait
99
#![cfg_attr(feature = "inline_asm", feature(asm))]
1010
#![cfg_attr(feature = "abi_x86_interrupt", feature(abi_x86_interrupt))]
1111
#![cfg_attr(docsrs, feature(doc_cfg))]

src/structures/gdt.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,15 @@ impl GlobalDescriptorTable {
112112
/// * The user must make sure that the entries are well formed
113113
/// * The provided slice **must not be larger than 8 items** (only up to the first 8 will be observed.)
114114
#[inline]
115-
#[cfg(feature = "const_fn")]
116115
pub const unsafe fn from_raw_slice(slice: &[u64]) -> GlobalDescriptorTable {
116+
#[cfg(feature = "const_fn")]
117117
assert!(
118118
slice.len() <= 8,
119119
"initializing a GDT from a slice requires it to be **at most** 8 elements."
120120
);
121+
#[cfg(not(feature = "const_fn"))]
122+
slice[7]; // Will fail if len > 8
123+
121124
let next_free = slice.len();
122125

123126
let mut table = [0; 8];

src/structures/paging/frame.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use core::ops::{Add, AddAssign, Sub, SubAssign};
1111
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
1212
#[repr(C)]
1313
pub struct PhysFrame<S: PageSize = Size4KiB> {
14-
start_address: PhysAddr,
14+
pub(crate) start_address: PhysAddr, // TODO: remove when start_address() is const
1515
size: PhantomData<S>,
1616
}
1717

src/structures/paging/mapper/mod.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,21 @@ pub enum MappedFrame {
8585
}
8686

8787
impl MappedFrame {
88-
const_fn! {
89-
/// Returns the start address of the frame.
90-
pub fn start_address(&self) -> PhysAddr {
91-
match self {
92-
MappedFrame::Size4KiB(frame) => frame.start_address(),
93-
MappedFrame::Size2MiB(frame) => frame.start_address(),
94-
MappedFrame::Size1GiB(frame) => frame.start_address(),
95-
}
88+
/// Returns the start address of the frame.
89+
pub const fn start_address(&self) -> PhysAddr {
90+
match self {
91+
MappedFrame::Size4KiB(frame) => frame.start_address,
92+
MappedFrame::Size2MiB(frame) => frame.start_address,
93+
MappedFrame::Size1GiB(frame) => frame.start_address,
9694
}
9795
}
9896

99-
const_fn! {
100-
/// Returns the size the frame (4KB, 2MB or 1GB).
101-
pub fn size(&self) -> u64 {
102-
match self {
103-
MappedFrame::Size4KiB(frame) => frame.size(),
104-
MappedFrame::Size2MiB(frame) => frame.size(),
105-
MappedFrame::Size1GiB(frame) => frame.size(),
106-
}
97+
/// Returns the size the frame (4KB, 2MB or 1GB).
98+
pub const fn size(&self) -> u64 {
99+
match self {
100+
MappedFrame::Size4KiB(_) => Size4KiB::SIZE,
101+
MappedFrame::Size2MiB(_) => Size2MiB::SIZE,
102+
MappedFrame::Size1GiB(_) => Size1GiB::SIZE,
107103
}
108104
}
109105
}

src/structures/paging/page.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,10 @@ impl Page<Size4KiB> {
210210
Page::containing_address(VirtAddr::new(addr))
211211
}
212212

213-
const_fn! {
214-
/// Returns the level 1 page table index of this page.
215-
#[inline]
216-
pub fn p1_index(self) -> PageTableIndex {
217-
self.start_address().p1_index()
218-
}
213+
/// Returns the level 1 page table index of this page.
214+
#[inline]
215+
pub const fn p1_index(self) -> PageTableIndex {
216+
self.start_address.p1_index()
219217
}
220218
}
221219

src/structures/paging/page_table.rs

-11
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ pub struct PageTable {
189189

190190
impl PageTable {
191191
/// Creates an empty page table.
192-
#[cfg(feature = "const_fn")]
193192
#[inline]
194193
pub const fn new() -> Self {
195194
const EMPTY: PageTableEntry = PageTableEntry::new();
@@ -198,16 +197,6 @@ impl PageTable {
198197
}
199198
}
200199

201-
/// Creates an empty page table.
202-
#[cfg(not(feature = "const_fn"))]
203-
#[inline]
204-
pub fn new() -> Self {
205-
const EMPTY: PageTableEntry = PageTableEntry::new();
206-
PageTable {
207-
entries: [EMPTY; ENTRY_COUNT],
208-
}
209-
}
210-
211200
/// Clears all entries.
212201
#[inline]
213202
pub fn zero(&mut self) {

0 commit comments

Comments
 (0)