Skip to content

Support creation of rust::Str from consteval utf8 checked string literal#1704

Open
wsxarcher wants to merge 3 commits intodtolnay:masterfrom
wsxarcher:str_utf8_nocheck_consteval
Open

Support creation of rust::Str from consteval utf8 checked string literal#1704
wsxarcher wants to merge 3 commits intodtolnay:masterfrom
wsxarcher:str_utf8_nocheck_consteval

Conversation

@wsxarcher
Copy link
Copy Markdown

@wsxarcher wsxarcher commented Apr 8, 2026

This change adds a C++20-only path for constructing rust::Str from UTF-8 string literals. The _utf8 literal validates the input at compile time using consteval function and then calls the unsafe version of constructing a str from rust to avoid double checking it (https://doc.rust-lang.org/std/str/fn.from_utf8_unchecked.html).

This is useful for creating literal Str without utf8 runtime checking overhead.

It can be especially useful in non exception code such as Chromium as extra utf8 check would have to be done outside the cxx library as a potential failure will result in abort, bringing the checks for static literals from 2 to 0.

Working example:

rust::Str var{u8"Valid👍"_utf8};

This will not compile:

rust::Str var{u8"Invalid\xff"_utf8};

@wsxarcher wsxarcher changed the title Support creation of rust::Str from consteval utf8 checked string Support creation of rust::Str from consteval utf8 checked string literal Apr 8, 2026
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.

1 participant