Repository navigation
Add const String::try_from_const - #701
Conversation
4bc8b7b to
60c70a6
Compare
|
Author is me, logged in on the wrong account :) |
|
Please rename it to Please also make |
|
Thanks for the review. I made the following changes to address your feedback (each a separate commit):
|
5a84fbc to
3743d3f
Compare
sgued
left a comment
There was a problem hiding this comment.
Some small nits again sorry.
| Self { vec: Vec::new() } | ||
| } | ||
|
|
||
| /// Creates a `String` from a `&str`, returning [`None`] if `s` doesn't fit. |
There was a problem hiding this comment.
| /// Creates a `String` from a `&str`, returning [`None`] if `s` doesn't fit. | |
| /// Creates a `String` from a `&str`, returning [`None`] if it doesn't fit. |
| return None; | ||
| } | ||
|
|
||
| // SAFETY: We just checked that `s.len() <= N`. |
There was a problem hiding this comment.
| // SAFETY: We just checked that `s.len() <= N`. | |
| // SAFETY: We just checked that `s.len() <= N`, and `s.as_bytes()` is known to be valid UTF-8. |
| let src = ManuallyDrop::new(src); | ||
|
|
||
| let len: LenT = to_len_type(M); | ||
|
|
||
| let mut v = Self::new(); | ||
|
|
||
| // MaybeUninit::deref is non-const, so we just cast to it's internal value. | ||
| let src_ptr: *const T = ptr::from_ref(&src).cast(); | ||
|
|
||
| // Cast from buffer's [MaybeUninit<T>] to [T]. | ||
| let dst_ptr: *mut T = v.buffer.buffer.as_mut_ptr().cast(); | ||
|
|
||
| // SAFETY: Move/copy data from src to v's internal buffer. | ||
| // * Using src_ptr as `*const T` is safe since src's ManuallyDrop<[T; M]> is transparent. | ||
| // * Using dst_ptr as `*mut T` is safe since v's [MaybeUninit<T>; N] is an array of | ||
| // transparent types. | ||
| unsafe { ptr::copy_nonoverlapping(src_ptr, dst_ptr, M) }; | ||
| v.len = len; | ||
|
|
||
| v | ||
| // SAFETY: `M <= N` by the assertion above. `ManuallyDrop<[T; M]>` is `repr(transparent)` | ||
| // around `[T; M]`, so the pointer is valid for reading its `M` elements, and `src` is never | ||
| // used or dropped again. | ||
| unsafe { Self::from_ptr_unchecked(ptr::from_ref(&src).cast::<T>(), M) } |
There was a problem hiding this comment.
I think this is a slightly better way to do it. No pointer cast needed.
| let src = ManuallyDrop::new(src); | |
| let len: LenT = to_len_type(M); | |
| let mut v = Self::new(); | |
| // MaybeUninit::deref is non-const, so we just cast to it's internal value. | |
| let src_ptr: *const T = ptr::from_ref(&src).cast(); | |
| // Cast from buffer's [MaybeUninit<T>] to [T]. | |
| let dst_ptr: *mut T = v.buffer.buffer.as_mut_ptr().cast(); | |
| // SAFETY: Move/copy data from src to v's internal buffer. | |
| // * Using src_ptr as `*const T` is safe since src's ManuallyDrop<[T; M]> is transparent. | |
| // * Using dst_ptr as `*mut T` is safe since v's [MaybeUninit<T>; N] is an array of | |
| // transparent types. | |
| unsafe { ptr::copy_nonoverlapping(src_ptr, dst_ptr, M) }; | |
| v.len = len; | |
| v | |
| // SAFETY: `M <= N` by the assertion above. `ManuallyDrop<[T; M]>` is `repr(transparent)` | |
| // around `[T; M]`, so the pointer is valid for reading its `M` elements, and `src` is never | |
| // used or dropped again. | |
| unsafe { Self::from_ptr_unchecked(ptr::from_ref(&src).cast::<T>(), M) } | |
| // SAFETY: `M <= N` by the assertion above. `src` is forgotten without `drop` just afterwards. | |
| let this = unsafe { Self::from_ptr_unchecked(src.as_ptr(), M) }; | |
| core::mem::forget(src); | |
| this |
fb05ecc to
75b66f6
Compare
|
Please fix the formatting and it's good to go. |
Should be good now |
sgued
left a comment
There was a problem hiding this comment.
Please also squash the commits, ideally keeping the vec changes in its own commit.
| - Fixed `SortedLinkedList` Zeroize implementation. | ||
| - Fixed possible integer overflow in `VecInner::extend_from_slice`. | ||
| - Allow `bytes::BufMut::advance_mut` implementation on `VecInner` to advance to the end of the buffer. | ||
| - Added `String::try_from_const`, a const version of `String::try_from(&str)` that returns an `Option`. |
There was a problem hiding this comment.
| - Added `String::try_from_const`, a const version of `String::try_from(&str)` that returns an `Option`. |
There was a problem hiding this comment.
both done. There are two commits now. The first one with all the vector changes doesn't compile because one part is considered dead code, which is only used in the second commit.
0a9ce27 to
735e912
Compare
Related to #698 #700, #515, all on adding const
Adds
String::try_from_const, which creates aStringfrom a&strin a const context:It returns an
Option, because on stable aResultholding aStringcan't be unwrapped in const. std does the same withchar::from_u32next toTryFrom<u32> for char. The name followsQueue::split_const.I'm open to suggestions for a better function name, but I think this is reasonable. Of course it would be nice if stable support const
Results, but AFAIK that is not possible currently. For obvious compatibility reasons I chose to not change the return type of the existing function toOption.from_slice_uncheckedcould also be public API, but I chose not to so the PR is focused. Can easily change that later. Also, now we don't have to discuss the signature and name for that so much because it's not public and people won't depend.AI disclosure
As with #698 and #700, I used Claude Opus 5.5 to assist with basically everything, but as a tool, not blindly trusted.