-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
ptr: introduce len() method on raw slices #71082
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @shepmaster (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
cc @rust-lang/wg-const-eval |
One question I have is whether we can put this onto slices directly, such that |
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 |
Cc #60639 |
This would also be covered by the more general rust-lang/rfcs#2580. (Please comment there in support if you’d like!) |
I did think of this solution and actually tried it first, but as I understand it the function would have to be put in a trait then (I can't add an |
This is possible but requires significantly more work, including on the compiler side: #60639 (comment) |
Looking into it. |
I have pushed an update which implements len() as a method, also introducing the lang items needed for #60639. For now, I've left the old version in the PR as well - I'll remove those commits if we decide to go for the method. |
This is awesome! The implementation lgtm, but the library method addition should probably be signed off by a libs team member. r? @SimonSapin or someone else from @rust-lang/libs
Even so, this new function is imo reasonable to have even if at some point its body is implemented via that rfc.
My personal preference is that we should go with the inherent method approach not have a free function at all. |
I agree that since we can have the inherent method, there's no need for the non-inherent method: the inherent method is strictly better. |
Add lang items for methods on raw slices.
It is already possible to extract the pointer part of a raw slice by a simple cast, but retrieving the length is not possible without relying on the representation of the raw slice when it is not valid to convert the raw slice into a slice reference (i.e. the pointer is null or unaligned). Introduce a len() method on raw slices to add this missing feature.
258d1d7
to
2a29f8f
Compare
Right, I never intended to have both versions included, I just wanted to give more people a chance to give feedback on the original proposal before removing it. What are the next steps here? Do I open tracking issues for the unstable features, or should I wait for a review? |
@NeoRaider Yes, now that there’s rough consensus for having something like this at all please file a tracking issue and add its number to the @Centril Why did you add needs-fcp and relnotes labels? This PR only adds unstable APIs, I’d expect to see those labels in the stabilization PR. |
@SimonSapin ah woops, my mistake! |
@bors r=oli-obk,SimonSapin |
📌 Commit dfd6844 has been approved by |
Rollup of 8 pull requests Successful merges: - rust-lang#70657 (Allow `try`-blocks in places where an open delim is expected) - rust-lang#70947 (tighten CTFE safety net for accesses to globals) - rust-lang#70949 (simplify `vec!` macro) - rust-lang#71002 (fix target & runtool args order) - rust-lang#71082 (ptr: introduce len() method on raw slices) - rust-lang#71128 (Remove unused single_step flag) - rust-lang#71133 (Tighten time complexity on the doc of sort_by_key) - rust-lang#71135 (Update books) Failed merges: r? @ghost
It is already possible to extract the pointer part of a raw slice by a
simple cast, but retrieving the length is not possible without relying
on the representation of the raw slice when it is not valid to convert
the raw slice into a slice reference (i.e. the pointer is null or
unaligned).
Introduce a new function ptr::slice_len() to add this missing feature.Introduce a len() method on raw slices to add this missing feature.