mirror of
https://github.com/Lokathor/bytemuck.git
synced 2024-11-21 22:32:23 +00:00
Create align_offset
feature so that we can continue to work on 1.34
This commit is contained in:
parent
caff759066
commit
1ba4215050
15
Cargo.toml
15
Cargo.toml
@ -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!
|
||||
|
@ -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`.
|
||||
|
Loading…
Reference in New Issue
Block a user