Skip to content

Commit 12fd073

Browse files
committed
Use rustversion to conditionally make methods const
Signed-off-by: Joe Richey <[email protected]>
1 parent 9378dd3 commit 12fd073

File tree

4 files changed

+114
-143
lines changed

4 files changed

+114
-143
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ edition = "2018"
2929
bit_field = "0.10.1"
3030
bitflags = "1.0.4"
3131
volatile = "0.4.4"
32+
rustversion = "1.0.5"
3233

3334
[features]
3435
default = [ "nightly", "instructions" ]

src/structures/idt.rs

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -410,39 +410,37 @@ pub struct InterruptDescriptorTable {
410410
}
411411

412412
impl InterruptDescriptorTable {
413-
// TODO: Remove const_fn! when our minimum supported stable Rust version is 1.61
414-
const_fn! {
415-
/// Creates a new IDT filled with non-present entries.
416-
#[inline]
417-
pub fn new() -> InterruptDescriptorTable {
418-
InterruptDescriptorTable {
419-
divide_error: Entry::missing(),
420-
debug: Entry::missing(),
421-
non_maskable_interrupt: Entry::missing(),
422-
breakpoint: Entry::missing(),
423-
overflow: Entry::missing(),
424-
bound_range_exceeded: Entry::missing(),
425-
invalid_opcode: Entry::missing(),
426-
device_not_available: Entry::missing(),
427-
double_fault: Entry::missing(),
428-
coprocessor_segment_overrun: Entry::missing(),
429-
invalid_tss: Entry::missing(),
430-
segment_not_present: Entry::missing(),
431-
stack_segment_fault: Entry::missing(),
432-
general_protection_fault: Entry::missing(),
433-
page_fault: Entry::missing(),
434-
reserved_1: Entry::missing(),
435-
x87_floating_point: Entry::missing(),
436-
alignment_check: Entry::missing(),
437-
machine_check: Entry::missing(),
438-
simd_floating_point: Entry::missing(),
439-
virtualization: Entry::missing(),
440-
reserved_2: [Entry::missing(); 8],
441-
vmm_communication_exception: Entry::missing(),
442-
security_exception: Entry::missing(),
443-
reserved_3: Entry::missing(),
444-
interrupts: [Entry::missing(); 256 - 32],
445-
}
413+
/// Creates a new IDT filled with non-present entries.
414+
#[inline]
415+
#[rustversion::attr(since(1.61), const)]
416+
pub fn new() -> InterruptDescriptorTable {
417+
InterruptDescriptorTable {
418+
divide_error: Entry::missing(),
419+
debug: Entry::missing(),
420+
non_maskable_interrupt: Entry::missing(),
421+
breakpoint: Entry::missing(),
422+
overflow: Entry::missing(),
423+
bound_range_exceeded: Entry::missing(),
424+
invalid_opcode: Entry::missing(),
425+
device_not_available: Entry::missing(),
426+
double_fault: Entry::missing(),
427+
coprocessor_segment_overrun: Entry::missing(),
428+
invalid_tss: Entry::missing(),
429+
segment_not_present: Entry::missing(),
430+
stack_segment_fault: Entry::missing(),
431+
general_protection_fault: Entry::missing(),
432+
page_fault: Entry::missing(),
433+
reserved_1: Entry::missing(),
434+
x87_floating_point: Entry::missing(),
435+
alignment_check: Entry::missing(),
436+
machine_check: Entry::missing(),
437+
simd_floating_point: Entry::missing(),
438+
virtualization: Entry::missing(),
439+
reserved_2: [Entry::missing(); 8],
440+
vmm_communication_exception: Entry::missing(),
441+
security_exception: Entry::missing(),
442+
reserved_3: Entry::missing(),
443+
interrupts: [Entry::missing(); 256 - 32],
446444
}
447445
}
448446

src/structures/paging/frame.rs

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,17 @@ impl<S: PageSize> PhysFrame<S> {
3030
Ok(unsafe { PhysFrame::from_start_address_unchecked(address) })
3131
}
3232

33-
// TODO: Remove const_fn! when our minimum supported stable Rust version is 1.61
34-
const_fn! {
35-
/// Returns the frame that starts at the given virtual address.
36-
///
37-
/// ## Safety
38-
///
39-
/// The address must be correctly aligned.
40-
#[inline]
41-
pub unsafe fn from_start_address_unchecked(start_address: PhysAddr) -> Self {
42-
PhysFrame {
43-
start_address,
44-
size: PhantomData,
45-
}
33+
/// Returns the frame that starts at the given virtual address.
34+
///
35+
/// ## Safety
36+
///
37+
/// The address must be correctly aligned.
38+
#[inline]
39+
#[rustversion::attr(since(1.61), const)]
40+
pub unsafe fn from_start_address_unchecked(start_address: PhysAddr) -> Self {
41+
PhysFrame {
42+
start_address,
43+
size: PhantomData,
4644
}
4745
}
4846

@@ -55,40 +53,32 @@ impl<S: PageSize> PhysFrame<S> {
5553
}
5654
}
5755

58-
// TODO: Remove const_fn! when our minimum supported stable Rust version is 1.61
59-
const_fn! {
60-
/// Returns the start address of the frame.
61-
#[inline]
62-
pub fn start_address(self) -> PhysAddr {
63-
self.start_address
64-
}
56+
/// Returns the start address of the frame.
57+
#[inline]
58+
#[rustversion::attr(since(1.61), const)]
59+
pub fn start_address(self) -> PhysAddr {
60+
self.start_address
6561
}
6662

67-
// TODO: Remove const_fn! when our minimum supported stable Rust version is 1.61
68-
const_fn! {
69-
/// Returns the size the frame (4KB, 2MB or 1GB).
70-
#[inline]
71-
pub fn size(self) -> u64 {
72-
S::SIZE
73-
}
63+
/// Returns the size the frame (4KB, 2MB or 1GB).
64+
#[inline]
65+
#[rustversion::attr(since(1.61), const)]
66+
pub fn size(self) -> u64 {
67+
S::SIZE
7468
}
7569

76-
// TODO: Remove const_fn! when our minimum supported stable Rust version is 1.61
77-
const_fn! {
78-
/// Returns a range of frames, exclusive `end`.
79-
#[inline]
80-
pub fn range(start: PhysFrame<S>, end: PhysFrame<S>) -> PhysFrameRange<S> {
81-
PhysFrameRange { start, end }
82-
}
70+
/// Returns a range of frames, exclusive `end`.
71+
#[inline]
72+
#[rustversion::attr(since(1.61), const)]
73+
pub fn range(start: PhysFrame<S>, end: PhysFrame<S>) -> PhysFrameRange<S> {
74+
PhysFrameRange { start, end }
8375
}
8476

85-
// TODO: Remove const_fn! when our minimum supported stable Rust version is 1.61
86-
const_fn! {
87-
/// Returns a range of frames, inclusive `end`.
88-
#[inline]
89-
pub fn range_inclusive(start: PhysFrame<S>, end: PhysFrame<S>) -> PhysFrameRangeInclusive<S> {
90-
PhysFrameRangeInclusive { start, end }
91-
}
77+
/// Returns a range of frames, inclusive `end`.
78+
#[inline]
79+
#[rustversion::attr(since(1.61), const)]
80+
pub fn range_inclusive(start: PhysFrame<S>, end: PhysFrame<S>) -> PhysFrameRangeInclusive<S> {
81+
PhysFrameRangeInclusive { start, end }
9282
}
9383
}
9484

src/structures/paging/page.rs

Lines changed: 51 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,17 @@ impl<S: PageSize> Page<S> {
7777
Ok(Page::containing_address(address))
7878
}
7979

80-
// TODO: Remove const_fn! when our minimum supported stable Rust version is 1.61
81-
const_fn! {
82-
/// Returns the page that starts at the given virtual address.
83-
///
84-
/// ## Safety
85-
///
86-
/// The address must be correctly aligned.
87-
#[inline]
88-
pub unsafe fn from_start_address_unchecked(start_address: VirtAddr) -> Self {
89-
Page {
90-
start_address,
91-
size: PhantomData,
92-
}
80+
/// Returns the page that starts at the given virtual address.
81+
///
82+
/// ## Safety
83+
///
84+
/// The address must be correctly aligned.
85+
#[inline]
86+
#[rustversion::attr(since(1.61), const)]
87+
pub unsafe fn from_start_address_unchecked(start_address: VirtAddr) -> Self {
88+
Page {
89+
start_address,
90+
size: PhantomData,
9391
}
9492
}
9593

@@ -102,78 +100,62 @@ impl<S: PageSize> Page<S> {
102100
}
103101
}
104102

105-
// TODO: Remove const_fn! when our minimum supported stable Rust version is 1.61
106-
const_fn! {
107-
/// Returns the start address of the page.
108-
#[inline]
109-
pub fn start_address(self) -> VirtAddr {
110-
self.start_address
111-
}
103+
/// Returns the start address of the page.
104+
#[inline]
105+
#[rustversion::attr(since(1.61), const)]
106+
pub fn start_address(self) -> VirtAddr {
107+
self.start_address
112108
}
113109

114-
// TODO: Remove const_fn! when our minimum supported stable Rust version is 1.61
115-
const_fn! {
116-
/// Returns the size the page (4KB, 2MB or 1GB).
117-
#[inline]
118-
pub fn size(self) -> u64 {
119-
S::SIZE
120-
}
110+
/// Returns the size the page (4KB, 2MB or 1GB).
111+
#[inline]
112+
#[rustversion::attr(since(1.61), const)]
113+
pub fn size(self) -> u64 {
114+
S::SIZE
121115
}
122116

123-
// TODO: Remove const_fn! when our minimum supported stable Rust version is 1.61
124-
const_fn! {
125-
/// Returns the level 4 page table index of this page.
126-
#[inline]
127-
pub fn p4_index(self) -> PageTableIndex {
128-
self.start_address().p4_index()
129-
}
117+
/// Returns the level 4 page table index of this page.
118+
#[inline]
119+
#[rustversion::attr(since(1.61), const)]
120+
pub fn p4_index(self) -> PageTableIndex {
121+
self.start_address().p4_index()
130122
}
131123

132-
// TODO: Remove const_fn! when our minimum supported stable Rust version is 1.61
133-
const_fn! {
134-
/// Returns the level 3 page table index of this page.
135-
#[inline]
136-
pub fn p3_index(self) -> PageTableIndex {
137-
self.start_address().p3_index()
138-
}
124+
/// Returns the level 3 page table index of this page.
125+
#[inline]
126+
#[rustversion::attr(since(1.61), const)]
127+
pub fn p3_index(self) -> PageTableIndex {
128+
self.start_address().p3_index()
139129
}
140130

141-
// TODO: Remove const_fn! when our minimum supported stable Rust version is 1.61
142-
const_fn! {
143-
/// Returns the table index of this page at the specified level.
144-
#[inline]
145-
pub fn page_table_index(self, level: PageTableLevel) -> PageTableIndex {
146-
self.start_address().page_table_index(level)
147-
}
131+
/// Returns the table index of this page at the specified level.
132+
#[inline]
133+
#[rustversion::attr(since(1.61), const)]
134+
pub fn page_table_index(self, level: PageTableLevel) -> PageTableIndex {
135+
self.start_address().page_table_index(level)
148136
}
149137

150-
// TODO: Remove const_fn! when our minimum supported stable Rust version is 1.61
151-
const_fn! {
152-
/// Returns a range of pages, exclusive `end`.
153-
#[inline]
154-
pub fn range(start: Self, end: Self) -> PageRange<S> {
155-
PageRange { start, end }
156-
}
138+
/// Returns a range of pages, exclusive `end`.
139+
#[inline]
140+
#[rustversion::attr(since(1.61), const)]
141+
pub fn range(start: Self, end: Self) -> PageRange<S> {
142+
PageRange { start, end }
157143
}
158144

159-
// TODO: Remove const_fn! when our minimum supported stable Rust version is 1.61
160-
const_fn! {
161-
/// Returns a range of pages, inclusive `end`.
162-
#[inline]
163-
pub fn range_inclusive(start: Self, end: Self) -> PageRangeInclusive<S> {
164-
PageRangeInclusive { start, end }
165-
}
145+
/// Returns a range of pages, inclusive `end`.
146+
#[inline]
147+
#[rustversion::attr(since(1.61), const)]
148+
pub fn range_inclusive(start: Self, end: Self) -> PageRangeInclusive<S> {
149+
PageRangeInclusive { start, end }
166150
}
167151
}
168152

169153
impl<S: NotGiantPageSize> Page<S> {
170-
// TODO: Remove const_fn! when our minimum supported stable Rust version is 1.61
171-
const_fn! {
172-
/// Returns the level 2 page table index of this page.
173-
#[inline]
174-
pub fn p2_index(self) -> PageTableIndex {
175-
self.start_address().p2_index()
176-
}
154+
/// Returns the level 2 page table index of this page.
155+
#[inline]
156+
#[rustversion::attr(since(1.61), const)]
157+
pub fn p2_index(self) -> PageTableIndex {
158+
self.start_address().p2_index()
177159
}
178160
}
179161

0 commit comments

Comments
 (0)