Skip to content

Commit 56f1325

Browse files
committed
Auto merge of rust-lang#100848 - xfix:use-metadata-for-slice-len, r=thomcc
Use ptr::metadata in <[T]>::len implementation This avoids duplication of ptr::metadata code. I believe this is acceptable as the previous approach essentially duplicated `ptr::metadata` because back then `rustc_allow_const_fn_unstable` annotation did not exist. I would like somebody to ping `@rust-lang/wg-const-eval` as the documentation says: > Always ping `@rust-lang/wg-const-eval` if you are adding more rustc_allow_const_fn_unstable attributes to any const fn.
2 parents 7feb003 + 155b4c2 commit 56f1325

File tree

3 files changed

+9
-17
lines changed

3 files changed

+9
-17
lines changed

library/core/src/ptr/metadata.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,16 @@ pub const fn from_raw_parts_mut<T: ?Sized>(
135135
}
136136

137137
#[repr(C)]
138-
pub(crate) union PtrRepr<T: ?Sized> {
139-
pub(crate) const_ptr: *const T,
140-
pub(crate) mut_ptr: *mut T,
141-
pub(crate) components: PtrComponents<T>,
138+
union PtrRepr<T: ?Sized> {
139+
const_ptr: *const T,
140+
mut_ptr: *mut T,
141+
components: PtrComponents<T>,
142142
}
143143

144144
#[repr(C)]
145-
pub(crate) struct PtrComponents<T: ?Sized> {
146-
pub(crate) data_address: *const (),
147-
pub(crate) metadata: <T as Pointee>::Metadata,
145+
struct PtrComponents<T: ?Sized> {
146+
data_address: *const (),
147+
metadata: <T as Pointee>::Metadata,
148148
}
149149

150150
// Manual impl needed to avoid `T: Copy` bound.

library/core/src/ptr/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ pub use crate::intrinsics::copy;
394394
pub use crate::intrinsics::write_bytes;
395395

396396
mod metadata;
397-
pub(crate) use metadata::PtrRepr;
398397
#[unstable(feature = "ptr_metadata", issue = "81513")]
399398
pub use metadata::{from_raw_parts, from_raw_parts_mut, metadata, DynMetadata, Pointee, Thin};
400399

library/core/src/slice/mod.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,11 @@ impl<T> [T] {
123123
#[lang = "slice_len_fn"]
124124
#[stable(feature = "rust1", since = "1.0.0")]
125125
#[rustc_const_stable(feature = "const_slice_len", since = "1.39.0")]
126+
#[rustc_allow_const_fn_unstable(ptr_metadata)]
126127
#[inline]
127128
#[must_use]
128-
// SAFETY: const sound because we transmute out the length field as a usize (which it must be)
129129
pub const fn len(&self) -> usize {
130-
// FIXME: Replace with `crate::ptr::metadata(self)` when that is const-stable.
131-
// As of this writing this causes a "Const-stable functions can only call other
132-
// const-stable functions" error.
133-
134-
// SAFETY: Accessing the value from the `PtrRepr` union is safe since *const T
135-
// and PtrComponents<T> have the same memory layouts. Only std can make this
136-
// guarantee.
137-
unsafe { crate::ptr::PtrRepr { const_ptr: self }.components.metadata }
130+
ptr::metadata(self)
138131
}
139132

140133
/// Returns `true` if the slice has a length of 0.

0 commit comments

Comments
 (0)