Skip to content

Commit d2615fd

Browse files
committed
Deprecate the const_fn feature
Just unconditionally make the GDT methods const if we are using the nightly compiler. This allows us to eliminate our `const_fn!` macro. Signed-off-by: Joe Richey <[email protected]>
1 parent 12fd073 commit d2615fd

File tree

4 files changed

+48
-71
lines changed

4 files changed

+48
-71
lines changed

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ repository = "https://github.com/rust-osdev/x86_64"
2525
version = "0.14.8"
2626
edition = "2018"
2727

28+
[build-dependencies]
29+
rustversion = "1.0.5"
30+
2831
[dependencies]
2932
bit_field = "0.10.1"
3033
bitflags = "1.0.4"
@@ -34,13 +37,13 @@ rustversion = "1.0.5"
3437
[features]
3538
default = [ "nightly", "instructions" ]
3639
instructions = []
37-
nightly = [ "const_fn", "step_trait", "abi_x86_interrupt", "doc_cfg" ]
40+
nightly = [ "step_trait", "abi_x86_interrupt", "doc_cfg" ]
3841
abi_x86_interrupt = []
39-
const_fn = []
4042
step_trait = []
4143
doc_cfg = []
4244

4345
# These features are no longer used and only there for backwards compatibility.
46+
const_fn = []
4447
external_asm = []
4548
inline_asm = []
4649

build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[rustversion::nightly]
2+
fn main() {
3+
println!("cargo:rustc-cfg=const_fn");
4+
}
5+
6+
#[rustversion::not(nightly)]
7+
fn main() {}

src/lib.rs

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! and access to various system registers.
33
44
#![cfg_attr(not(test), no_std)]
5-
#![cfg_attr(feature = "const_fn", feature(const_mut_refs))] // GDT add_entry()
5+
#![cfg_attr(const_fn, feature(const_mut_refs))] // GDT add_entry()
66
#![cfg_attr(feature = "abi_x86_interrupt", feature(abi_x86_interrupt))]
77
#![cfg_attr(feature = "step_trait", feature(step_trait))]
88
#![cfg_attr(feature = "doc_cfg", feature(doc_cfg))]
@@ -12,37 +12,6 @@
1212

1313
pub use crate::addr::{align_down, align_up, PhysAddr, VirtAddr};
1414

15-
/// Makes a function const only when `feature = "const_fn"` is enabled.
16-
///
17-
/// This is needed for const functions with bounds on their generic parameters,
18-
/// such as those in `Page` and `PhysFrame` and many more.
19-
macro_rules! const_fn {
20-
(
21-
$(#[$attr:meta])*
22-
$sv:vis fn $($fn:tt)*
23-
) => {
24-
$(#[$attr])*
25-
#[cfg(feature = "const_fn")]
26-
$sv const fn $($fn)*
27-
28-
$(#[$attr])*
29-
#[cfg(not(feature = "const_fn"))]
30-
$sv fn $($fn)*
31-
};
32-
(
33-
$(#[$attr:meta])*
34-
$sv:vis unsafe fn $($fn:tt)*
35-
) => {
36-
$(#[$attr])*
37-
#[cfg(feature = "const_fn")]
38-
$sv const unsafe fn $($fn)*
39-
40-
$(#[$attr])*
41-
#[cfg(not(feature = "const_fn"))]
42-
$sv unsafe fn $($fn)*
43-
};
44-
}
45-
4615
pub mod addr;
4716
pub mod instructions;
4817
pub mod registers;

src/structures/gdt.rs

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -93,35 +93,34 @@ impl GlobalDescriptorTable {
9393
&self.table[..self.next_free]
9494
}
9595

96-
const_fn! {
97-
/// Adds the given segment descriptor to the GDT, returning the segment selector.
98-
///
99-
/// Panics if the GDT has no free entries left.
100-
#[inline]
101-
pub fn add_entry(&mut self, entry: Descriptor) -> SegmentSelector {
102-
let index = match entry {
103-
Descriptor::UserSegment(value) => self.push(value),
104-
Descriptor::SystemSegment(value_low, value_high) => {
105-
let index = self.push(value_low);
106-
self.push(value_high);
107-
index
108-
}
109-
};
96+
/// Adds the given segment descriptor to the GDT, returning the segment selector.
97+
///
98+
/// Panics if the GDT has no free entries left.
99+
#[inline]
100+
#[rustversion::attr(nightly, const)]
101+
pub fn add_entry(&mut self, entry: Descriptor) -> SegmentSelector {
102+
let index = match entry {
103+
Descriptor::UserSegment(value) => self.push(value),
104+
Descriptor::SystemSegment(value_low, value_high) => {
105+
let index = self.push(value_low);
106+
self.push(value_high);
107+
index
108+
}
109+
};
110110

111-
let rpl = match entry {
112-
Descriptor::UserSegment(value) => {
113-
if DescriptorFlags::from_bits_truncate(value).contains(DescriptorFlags::DPL_RING_3)
114-
{
115-
PrivilegeLevel::Ring3
116-
} else {
117-
PrivilegeLevel::Ring0
118-
}
111+
let rpl = match entry {
112+
Descriptor::UserSegment(value) => {
113+
if DescriptorFlags::from_bits_truncate(value).contains(DescriptorFlags::DPL_RING_3)
114+
{
115+
PrivilegeLevel::Ring3
116+
} else {
117+
PrivilegeLevel::Ring0
119118
}
120-
Descriptor::SystemSegment(_, _) => PrivilegeLevel::Ring0,
121-
};
119+
}
120+
Descriptor::SystemSegment(_, _) => PrivilegeLevel::Ring0,
121+
};
122122

123-
SegmentSelector::new(index as u16, rpl)
124-
}
123+
SegmentSelector::new(index as u16, rpl)
125124
}
126125

127126
/// Loads the GDT in the CPU using the `lgdt` instruction. This does **not** alter any of the
@@ -155,17 +154,16 @@ impl GlobalDescriptorTable {
155154
}
156155
}
157156

158-
const_fn! {
159-
#[inline]
160-
fn push(&mut self, value: u64) -> usize {
161-
if self.next_free < self.table.len() {
162-
let index = self.next_free;
163-
self.table[index] = value;
164-
self.next_free += 1;
165-
index
166-
} else {
167-
panic!("GDT full");
168-
}
157+
#[inline]
158+
#[rustversion::attr(nightly, const)]
159+
fn push(&mut self, value: u64) -> usize {
160+
if self.next_free < self.table.len() {
161+
let index = self.next_free;
162+
self.table[index] = value;
163+
self.next_free += 1;
164+
index
165+
} else {
166+
panic!("GDT full");
169167
}
170168
}
171169

0 commit comments

Comments
 (0)