Skip to content

Add const String::try_from_const - #701

Open
sundew-sensing-admin wants to merge 4 commits into
rust-embedded:mainfrom
sundew-sensing:const-try-from-const
Open

sundew-sensing-admin wants to merge 4 commits into
rust-embedded:mainfrom
sundew-sensing:const-try-from-const

Conversation

@sundew-sensing-admin

@sundew-sensing-admin sundew-sensing-admin commented Nov 7, 2026 •

Copy link
Copy Markdown

Related to #698 #700, #515, all on adding const

Adds String::try_from_const, which creates a String from a &str in a const context:

static S: String<8> = String::try_from_const("abcd").expect("fits in 8 bytes");

It returns an Option, because on stable a Result holding a String can't be unwrapped in const. std does the same with char::from_u32 next to TryFrom<u32> for char. The name follows Queue::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 to Option.

from_slice_unchecked could 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.

@sundew-sensing-admin
sundew-sensing-admin marked this pull request as ready for review November 7, 2026 16:45
@surgura

surgura commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Author is me, logged in on the wrong account :)

@sgued

sgued commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Please rename it to try_from_str (this seems to match what the ecosystem does), modify the TryFrom impl to use the new function to reduce code duplication,

Please also make Vec::from_array use the new from_slice_unchecked to reduce code duplication

@surgura

surgura commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the review. I made the following changes to address your feedback (each a separate commit):

  • Renamed to try_from_str
  • TryFrom<&str> uses it (as well as FromStr, same-same)
  • Vec::from_array now shares the code with try_from_str. However, for that I needed to drop the Copy restriction, as vec doesn't have that. So I created a new function that takes a raw pointer, so I don't fake-promise to only borrow the slice, while secretly we are moving out of it. This is used by vec directly. The original helper is now just a simple wrapper around it, and string keeps the original slice safety.

`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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants