Skip to content

Commit 469c620

Browse files
committed
Account for pointer metadata in pointer bounds
1 parent 078cb58 commit 469c620

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

crates/core_simd/src/cast.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,19 @@ unsafe impl SimdCast for f64 {}
3737
/// # Safety
3838
/// Implementing this trait asserts that the type is a valid vector element for the `simd_cast_ptr`
3939
/// intrinsic.
40-
pub unsafe trait SimdCastPtr: SimdElement {}
40+
pub unsafe trait SimdCastPtr<T> {}
4141

4242
// Safety: pointers can be cast to other pointer types
43-
unsafe impl<T> SimdCastPtr for *const T {}
43+
unsafe impl<T, U> SimdCastPtr<T> for *const U
44+
where
45+
U: core::ptr::Pointee,
46+
T: core::ptr::Pointee<Metadata = U::Metadata>,
47+
{
48+
}
4449
// Safety: pointers can be cast to other pointer types
45-
unsafe impl<T> SimdCastPtr for *mut T {}
50+
unsafe impl<T, U> SimdCastPtr<T> for *mut U
51+
where
52+
U: core::ptr::Pointee,
53+
T: core::ptr::Pointee<Metadata = U::Metadata>,
54+
{
55+
}

crates/core_simd/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
simd_ffi,
99
staged_api,
1010
stdsimd,
11-
strict_provenance
11+
strict_provenance,
12+
ptr_metadata
1213
)]
1314
#![cfg_attr(feature = "generic_const_exprs", feature(generic_const_exprs))]
1415
#![cfg_attr(feature = "generic_const_exprs", allow(incomplete_features))]

crates/core_simd/src/vector.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,10 @@ where
220220
/// Lanewise casts pointers to another pointer type.
221221
#[must_use]
222222
#[inline]
223-
pub fn cast_ptr<U: SimdCastPtr>(self) -> Simd<U, LANES>
223+
pub fn cast_ptr<U>(self) -> Simd<U, LANES>
224224
where
225-
T: SimdCastPtr,
225+
T: SimdCastPtr<U>,
226+
U: SimdElement,
226227
{
227228
// Safety: supported types are guaranteed by SimdCastPtr
228229
unsafe { intrinsics::simd_cast_ptr(self) }
@@ -753,14 +754,24 @@ unsafe impl SimdElement for f64 {
753754

754755
impl<T> Sealed for *const T {}
755756

756-
// Safety: const pointers are valid SIMD element types, and are supported by this API
757-
unsafe impl<T> SimdElement for *const T {
757+
// Safety: (thin) const pointers are valid SIMD element types, and are supported by this API
758+
//
759+
// Fat pointers may be supported in the future.
760+
unsafe impl<T> SimdElement for *const T
761+
where
762+
T: core::ptr::Pointee<Metadata = ()>,
763+
{
758764
type Mask = isize;
759765
}
760766

761767
impl<T> Sealed for *mut T {}
762768

763-
// Safety: mut pointers are valid SIMD element types, and are supported by this API
764-
unsafe impl<T> SimdElement for *mut T {
769+
// Safety: (thin) mut pointers are valid SIMD element types, and are supported by this API
770+
//
771+
// Fat pointers may be supported in the future.
772+
unsafe impl<T> SimdElement for *mut T
773+
where
774+
T: core::ptr::Pointee<Metadata = ()>,
775+
{
765776
type Mask = isize;
766777
}

0 commit comments

Comments
 (0)