Skip to content

Commit 67be0e6

Browse files
committed
Make align_up and align_down const
We can't make VirtAddr and PhysAddr methods const as they wrap an into impl.
1 parent 134cacd commit 67be0e6

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/addr.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,12 @@ impl Sub<PhysAddr> for PhysAddr {
534534
/// Returns the greatest x with alignment `align` so that x <= addr. The alignment must be
535535
/// a power of 2.
536536
#[inline]
537-
pub fn align_down(addr: u64, align: u64) -> u64 {
537+
pub const fn align_down(addr: u64, align: u64) -> u64 {
538+
#[cfg(feature = "const_fn")]
538539
assert!(align.is_power_of_two(), "`align` must be a power of two");
540+
#[cfg(not(feature = "const_fn"))]
541+
[(); 1][!align.is_power_of_two() as usize];
542+
539543
addr & !(align - 1)
540544
}
541545

@@ -544,8 +548,12 @@ pub fn align_down(addr: u64, align: u64) -> u64 {
544548
/// Returns the smallest x with alignment `align` so that x >= addr. The alignment must be
545549
/// a power of 2.
546550
#[inline]
547-
pub fn align_up(addr: u64, align: u64) -> u64 {
551+
pub const fn align_up(addr: u64, align: u64) -> u64 {
552+
#[cfg(feature = "const_fn")]
548553
assert!(align.is_power_of_two(), "`align` must be a power of two");
554+
#[cfg(not(feature = "const_fn"))]
555+
[(); 1][!align.is_power_of_two() as usize];
556+
549557
let align_mask = align - 1;
550558
if addr & align_mask == 0 {
551559
addr // already aligned

0 commit comments

Comments
 (0)