Repository navigation
Add const String::try_from_const - #701
Open
sundew-sensing-admin wants to merge 4 commits into
Open
sundew-sensing-admin wants to merge 4 commits into
sundew-sensing-admin wants to merge 4 commits into
Conversation
sundew-sensing-admin
force-pushed
the
const-try-from-const
branch
from
November 7, 2026 16:42
4bc8b7b to
60c70a6
Compare
sundew-sensing-admin
marked this pull request as ready for review
November 7, 2026 16:45
Contributor
|
Author is me, logged in on the wrong account :) |
Contributor
|
Please rename it to Please also make |
Contributor
|
Thanks for the review. I made the following changes to address your feedback (each a separate commit):
|
`String::try_from` can't be used in const contexts, because trait methods can't be called there. `try_from_const` does the same conversion from a `&str`, but returns an `Option` instead of a `Result`. On stable, a `Result` holding a `String` can't be unwrapped in a const context, while an `Option` can. std follows the same pattern with `char::from_u32` next to `TryFrom<u32> for char`.
This removes the duplicated conversion code.
The element copy now lives in `from_ptr_unchecked`, which takes a pointer and a length and moves the elements. `from_array` uses it directly, and `from_slice_unchecked` becomes a wrapper around it for `Copy` elements. A new test checks that moved elements are dropped exactly once.
sundew-sensing-admin
force-pushed
the
const-try-from-const
branch
from
November 7, 2026 20:10
5a84fbc to
3743d3f
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.