Skip to content

Commit

Permalink
Create align_offset feature so that we can continue to work on 1.34
Browse files Browse the repository at this point in the history
  • Loading branch information
Lokathor committed Sep 5, 2023
1 parent caff759 commit 1ba4215
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
15 changes: 11 additions & 4 deletions Cargo.toml
Expand Up @@ -18,12 +18,19 @@ extern_crate_alloc = []
extern_crate_std = ["extern_crate_alloc"]
zeroable_maybe_uninit = []
zeroable_atomics = []
min_const_generics = []
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.

# 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!
Expand Down
17 changes: 12 additions & 5 deletions src/internal.rs
Expand Up @@ -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 {
// 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.
ptr.align_offset(align) == 0
#[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.
ptr.align_offset(align) == 0
}
#[cfg(not(feature = "align_offset"))]
{
((ptr as usize) % align) == 0
}
}

/// Re-interprets `&[u8]` as `&T`.
Expand Down

0 comments on commit 1ba4215

Please sign in to comment.