Create align_offset feature so that we can continue to work on 1.34

This commit is contained in:
Lokathor 2023-09-05 14:08:36 -06:00
parent caff759066
commit 1ba4215050
2 changed files with 23 additions and 9 deletions

View File

@ -18,12 +18,19 @@ extern_crate_alloc = []
extern_crate_std = ["extern_crate_alloc"]
zeroable_maybe_uninit = []
zeroable_atomics = []
min_const_generics = []
# Causes MSRV 1.36, use `align_offset` method instead of casting to `usize` to
# check alignment of pointers, this *may* improve codegen in some cases (but it
# has never been formally benchmarked!)
align_offset = []
min_const_generics = [] # Causes MSRV 1.51
wasm_simd = [] # Until >= 1.54.0 is MSRV this is an off-by-default feature.
must_cast = [] # Until >= 1.57.0 is MSRV this is an off-by-default feature.
aarch64_simd = [] # Until >= 1.59.0 is MSRV this is an off-by-default feature.
# Do not use if you can avoid it, because this is unsound.
# Do not use if you can avoid it, because this is **unsound**!!!!
unsound_ptr_pod_impl = []
# NOT SEMVER SUPPORTED! TEMPORARY ONLY!

View File

@ -136,11 +136,18 @@ pub(crate) unsafe fn pod_read_unaligned<T: Copy>(bytes: &[u8]) -> T {
/// * If `align` is not a power of two. This includes when `align` is zero.
#[inline]
pub(crate) fn is_aligned_to(ptr: *const (), align: usize) -> bool {
#[cfg(feature = "align_offset")]
{
// This is in a way better than `ptr as usize % align == 0`,
// because casting a pointer to an integer has the side effect that it exposes
// the pointer's provenance, which may theoretically inhibit some compiler
// optimizations.
// because casting a pointer to an integer has the side effect that it
// exposes the pointer's provenance, which may theoretically inhibit
// some compiler optimizations.
ptr.align_offset(align) == 0
}
#[cfg(not(feature = "align_offset"))]
{
((ptr as usize) % align) == 0
}
}
/// Re-interprets `&[u8]` as `&T`.