Skip to content

Commit 306422e

Browse files
committed
constify more Page functions
1 parent 4aaa21c commit 306422e

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/structures/paging/page.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ impl<S: PageSize> Page<S> {
7777
///
7878
/// Returns an error if the address is not correctly aligned (i.e. is not a valid page start).
7979
#[inline]
80+
#[rustversion::attr(since(1.61), const)]
8081
pub fn from_start_address(address: VirtAddr) -> Result<Self, AddressNotAligned> {
81-
if !address.is_aligned(S::SIZE) {
82+
if !address.is_aligned_u64(S::SIZE) {
8283
return Err(AddressNotAligned);
8384
}
8485
Ok(Page::containing_address(address))
@@ -100,9 +101,10 @@ impl<S: PageSize> Page<S> {
100101

101102
/// Returns the page that contains the given virtual address.
102103
#[inline]
104+
#[rustversion::attr(since(1.61), const)]
103105
pub fn containing_address(address: VirtAddr) -> Self {
104106
Page {
105-
start_address: address.align_down(S::SIZE),
107+
start_address: address.align_down_u64(S::SIZE),
106108
size: PhantomData,
107109
}
108110
}
@@ -185,47 +187,50 @@ impl<S: NotGiantPageSize> Page<S> {
185187
impl Page<Size1GiB> {
186188
/// Returns the 1GiB memory page with the specified page table indices.
187189
#[inline]
190+
#[rustversion::attr(since(1.61), const)]
188191
pub fn from_page_table_indices_1gib(
189192
p4_index: PageTableIndex,
190193
p3_index: PageTableIndex,
191194
) -> Self {
192195
let mut addr = 0;
193-
addr |= u64::from(p4_index) << 39;
194-
addr |= u64::from(p3_index) << 30;
196+
addr |= p4_index.into_u64() << 39;
197+
addr |= p3_index.into_u64() << 30;
195198
Page::containing_address(VirtAddr::new_truncate(addr))
196199
}
197200
}
198201

199202
impl Page<Size2MiB> {
200203
/// Returns the 2MiB memory page with the specified page table indices.
201204
#[inline]
205+
#[rustversion::attr(since(1.61), const)]
202206
pub fn from_page_table_indices_2mib(
203207
p4_index: PageTableIndex,
204208
p3_index: PageTableIndex,
205209
p2_index: PageTableIndex,
206210
) -> Self {
207211
let mut addr = 0;
208-
addr |= u64::from(p4_index) << 39;
209-
addr |= u64::from(p3_index) << 30;
210-
addr |= u64::from(p2_index) << 21;
212+
addr |= p4_index.into_u64() << 39;
213+
addr |= p3_index.into_u64() << 30;
214+
addr |= p2_index.into_u64() << 21;
211215
Page::containing_address(VirtAddr::new_truncate(addr))
212216
}
213217
}
214218

215219
impl Page<Size4KiB> {
216220
/// Returns the 4KiB memory page with the specified page table indices.
217221
#[inline]
222+
#[rustversion::attr(since(1.61), const)]
218223
pub fn from_page_table_indices(
219224
p4_index: PageTableIndex,
220225
p3_index: PageTableIndex,
221226
p2_index: PageTableIndex,
222227
p1_index: PageTableIndex,
223228
) -> Self {
224229
let mut addr = 0;
225-
addr |= u64::from(p4_index) << 39;
226-
addr |= u64::from(p3_index) << 30;
227-
addr |= u64::from(p2_index) << 21;
228-
addr |= u64::from(p1_index) << 12;
230+
addr |= p4_index.into_u64() << 39;
231+
addr |= p3_index.into_u64() << 30;
232+
addr |= p2_index.into_u64() << 21;
233+
addr |= p1_index.into_u64() << 12;
229234
Page::containing_address(VirtAddr::new_truncate(addr))
230235
}
231236

src/structures/paging/page_table.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ impl PageTableIndex {
307307
Self(index % ENTRY_COUNT as u16)
308308
}
309309

310+
#[inline]
310311
pub(crate) const fn into_u64(self) -> u64 {
311312
self.0 as u64
312313
}

0 commit comments

Comments
 (0)