Skip to content

Commit 4aaa21c

Browse files
committed
constify align_down, is_aligned & into
We can't constify the public methods because const traits are not yet stable, but we can introduce private const methods that don't use traits. We'll use these internally in other const methods.
1 parent 8da3546 commit 4aaa21c

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/addr.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,15 @@ impl VirtAddr {
177177
where
178178
U: Into<u64>,
179179
{
180-
VirtAddr::new_truncate(align_down(self.0, align.into()))
180+
self.align_down_u64(align.into())
181+
}
182+
183+
/// Aligns the virtual address downwards to the given alignment.
184+
///
185+
/// See the `align_down` function for more information.
186+
#[inline]
187+
pub(crate) const fn align_down_u64(self, align: u64) -> Self {
188+
VirtAddr::new_truncate(align_down(self.0, align))
181189
}
182190

183191
/// Checks whether the virtual address has the demanded alignment.
@@ -186,7 +194,13 @@ impl VirtAddr {
186194
where
187195
U: Into<u64>,
188196
{
189-
self.align_down(align) == self
197+
self.is_aligned_u64(align.into())
198+
}
199+
200+
/// Checks whether the virtual address has the demanded alignment.
201+
#[inline]
202+
pub(crate) const fn is_aligned_u64(self, align: u64) -> bool {
203+
self.align_down_u64(align).as_u64() == self.as_u64()
190204
}
191205

192206
/// Returns the 12-bit page offset of this virtual address.

src/structures/paging/page_table.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ impl PageTableIndex {
306306
pub const fn new_truncate(index: u16) -> Self {
307307
Self(index % ENTRY_COUNT as u16)
308308
}
309+
310+
pub(crate) const fn into_u64(self) -> u64 {
311+
self.0 as u64
312+
}
309313
}
310314

311315
impl From<PageTableIndex> for u16 {
@@ -325,7 +329,7 @@ impl From<PageTableIndex> for u32 {
325329
impl From<PageTableIndex> for u64 {
326330
#[inline]
327331
fn from(index: PageTableIndex) -> Self {
328-
u64::from(index.0)
332+
index.into_u64()
329333
}
330334
}
331335

0 commit comments

Comments
 (0)