1
Fork 0

Optimize pointer::as_aligned_to

This commit is contained in:
Maybe Waffle 2022-08-05 17:11:47 +04:00
parent 1f5d8d49eb
commit c195f7c0a4
2 changed files with 2 additions and 8 deletions

View file

@ -1336,11 +1336,8 @@ impl<T: ?Sized> *const T {
panic!("is_aligned_to: align is not a power-of-two");
}
// SAFETY: `is_power_of_two()` will return `false` for zero.
unsafe { core::intrinsics::assume(align != 0) };
// Cast is needed for `T: !Sized`
self.cast::<u8>().addr() % align == 0
self.cast::<u8>().addr() & align - 1 == 0
}
}

View file

@ -1614,11 +1614,8 @@ impl<T: ?Sized> *mut T {
panic!("is_aligned_to: align is not a power-of-two");
}
// SAFETY: `is_power_of_two()` will return `false` for zero.
unsafe { core::intrinsics::assume(align != 0) };
// Cast is needed for `T: !Sized`
self.cast::<u8>().addr() % align == 0
self.cast::<u8>().addr() & align - 1 == 0
}
}