-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Make [T]::len
and str::len
const fn
#50863
Conversation
cc @Centril |
A simple common use case:
|
unsafe { | ||
mem::transmute::<&[T], Repr<T>>(self).len |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
Seems solid. I don’t have review rights tho |
r? @Centril |
Hot potato passing until success r? @SimonSapin |
buf[i] = c; | ||
} | ||
buf | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make this assert some equality?
This comment has been minimized.
This comment has been minimized.
@bors r=SimonSapin,Gankro |
📌 Commit 2788f66 has been approved by |
Make `[T]::len` and `str::len` const fn r? @gankro
Rollup of 15 pull requests Successful merges: - #50846 (Add E0665) - #50849 (CheckLoopVisitor: also visit closure arguments) - #50863 (Make `[T]::len` and `str::len` const fn) - #50875 (rustdoc: use "short form" doc(cfg) printing even when combined with other conditionals) - #50913 (Fix typo in cell.rs) - #50914 (Issue #50636: Improve error diagnostic with missing commas after struct fields.) - #50931 (Inline `try_get`.) - #50932 (Optimize seen Predicate filtering.) - #50945 (Stabilize feature from_ref) - #50946 (rustc: Fix procedural macros generating lifetime tokens) - #50947 (rustdoc: set tab width in rust source blocks) - #50952 (Add the 2018 edition of the book to doc.rust-lang.org) - #50958 (Micro-optimization on PR#50697) - #50961 (Fix FileCheck finding with MSVC) - #50963 (Right-size the `VecDeque` in `coerce_unsized`.) Failed merges:
It doesn't work in this equally common case:
|
That will never work with const eval as it is setup currently. Supporting such cases (I'd call them "lazy const eval") would technically be possible but definitely requires an RFC. |
#[rustc_const_unstable(feature="const_str_as_bytes")] | ||
pub const fn as_bytes(&self) -> &[u8] { | ||
unsafe { | ||
union Slices<'a> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can/should the union be moved outside the unsafe block? it's definition isn't unsafe iirc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While that could certainly be done, there's no difference here and this seems very self-contained to me. YMMV, so feel free to open a PR about that if you want :)
move type out of unsafe block from rust-lang#50863 (comment) move the union definition outside of the unsafe block as it's definition is not unsafe
Is this likely to stabilize soon?I need either this or the layout of The hack I am currently using is(approximately): #[repr(C)]
pub struct StaticSlice<T: 'static> {
s: &'static [T],
conversion: extern fn(*const [usize;2])->RSlice<'static,T>,
}
#[repr(C)]
pub struct RSlice<'a,T>{
ptr:*const T,
len:usize,
_marker:PhantomData<&'a T>,
} Along with a static assertions that
The function pointer takes in a |
Stabilization is blocked on #51909 |
r? @gankro