-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add core::ptr::slice_len #68169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add core::ptr::slice_len #68169
Conversation
r? @shepmaster (rust_highfive has picked a reviewer for you, use r? to override) |
(CI (tidy) will fail because I marked the placeholder issue number with a |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
No, it's not. The pointer still must not be dangling, even if the size is 0. IOW, it must not point to an allocation that has been freed, or beyond the bounds of an existing one.
Just use issue number 0, for now. |
More generally speaking, this should probably be part of a family of methods on raw slices. Besides |
I'm unsure how to go about adding the lang item required to add methods to I took a glance at the typeck inherent impls part of It'd be "nice" for these to be methods on |
@CAD97 Hmm. We ought to be able to manage inherent methods on impl MyType<u32> { } and the like. Glancing over the code it seems like most of the logic is just there to detect the correct usage of the Note that in some cases we have already the option of having "two" def-ids, for distinct crates: rust/src/librustc_typeck/coherence/inherent_impls.rs Lines 97 to 106 in c06e4ac
we could probably just add a second lang item like
It wouldn't be perfect in some sense, since the lang item
|
Ping from triage. @CAD97 any updates on this? Thanks. |
IIUC the status is that we want to attach Unfortunately, I don't have the bandwidth right now to go about adding the lang item required to add these inherent impls, so I'll close this for now. |
Uses a placeholder tracking issue currently. I'll file a tracking issue and update the commit if this is OK'd.
If I'm reading the various issue threads about raw pointer metadata correctly, the validity invariant of pointer metadata is at max that of the metadata type. For slice pointers, the metadata is typed at usize. This means that this function should be sound over all
*const [_]
pointers.This is doable stably for all but the null-ptr case by casting to
*const [()]
and making that a reference&[()]
, as[()]
has an alignment of 1 and a size of 0 no matter its length, so is valid at any location (IIUC). This just exposes a standard way of doing it without having to know to erase the type and that works for potentially-null pointers as well.cc @rust-lang/wg-unsafe-code-guidelines @RalfJung rust-lang/unsafe-code-guidelines#158 (comment)