Skip to content

Commit dd1a4c1

Browse files
authored
Merge pull request #255 from rust-osdev/constfn
Cleanup const_fn!
2 parents f232228 + a53a57a commit dd1a4c1

File tree

7 files changed

+34
-44
lines changed

7 files changed

+34
-44
lines changed

Changelog.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Unreleased
22

3+
- The following methods no longer require the `nightly` feature to be `const fn`s`:
4+
- `PageTable::new`
5+
- `GlobalDescriptorTable::from_raw_slice`
6+
- `MappedFrame::{start_address, size}`
7+
- `Page<Size4KiB>::p1_index`
38
- Add `Debug` implementation for `InterruptDescriptorTable` ([#253](https://github.com/rust-osdev/x86_64/pull/253))
49
- Improve `Debug` implementations for `Entry` and `EntryOptions`
510

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,19 @@ 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 {
117-
assert!(
118-
slice.len() <= 8,
119-
"initializing a GDT from a slice requires it to be **at most** 8 elements."
120-
);
121116
let next_free = slice.len();
122-
123117
let mut table = [0; 8];
124118
let mut idx = 0;
125119

120+
#[cfg(feature = "const_fn")]
121+
assert!(
122+
next_free <= 8,
123+
"initializing a GDT from a slice requires it to be **at most** 8 elements."
124+
);
125+
#[cfg(not(feature = "const_fn"))]
126+
table[next_free]; // Will fail if slice.len() > 8
127+
126128
while idx != next_free {
127129
table[idx] = slice[idx];
128130
idx += 1;

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)