I agree that performing an unguaranteed transmute is far from ideal, but for some reason CStr
showcases no const
constructor (not even from_bytes_with_nul
? It's compatible with the thin pointer implementation). So the only non-UB solution right now is to use our own wrapper type that delegates the construction to runtime:
struct ImplIntoCStr<'lt>(&'lt [u8]);
const unsafe fn cstr_from_bytes_with_null (bytes: &'_ [u8])
-> ImplIntoCStr<'_>
{
/* const */ impl<'lt> Into<&'lt CStr> for ImplIntoCStr<'lt> { … }
ImplIntoCStr(bytes)
}