-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion: CStr literals #75401
Comments
Alternative: Make CStr associated methods const. That is a lib changes only rather than a language change. |
Another alternative: Provide a |
This does not resolve the ergonomics / potential unsoundedness issue
A macro would work (and is in fact probably better as CStr is specific to |
It could take either, it just has to check that there's no 0 bytes contained within |
Yeah this can just be a proc macro in a crate that takes a string literal or byte string literal and then produces a |
I suppose so. I still think it would make sense to include it into
`std`/rustc.
I'm not familiar with writing proc-macros, unfortunately
…On Wed, 12 Aug 2020, 01:22 Lokathor, ***@***.***> wrote:
Yeah this can just be a proc macro in a crate that takes a string literal
or byte string literal and then produces a &[u8].
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#75401 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJI55IXFNDIV6ZNQYG6FWSTSAHHFBANCNFSM4P27NFHQ>
.
|
Probably something similar to this https://github.com/Lokathor/utf16_lit/blob/main/src/lib.rs#L62, but instead of recoding the bytes from |
Combining the suggestions, const { CStr::from_bytes_with_nul($input).unwrap() } no proc-macros necessary. |
Is inline |
No, but it is RFC-accepted and doesn't seem terribly hard to implement (he says as a non compiler developer). |
IIRC, Still, regardless of the macro details, I think we can all agree that this ability, while nice, doesn't need to exist directly within the compiler or standard library. It can be done as a standard user crate. So people should make the helper macro they want as a crates.io crate, and then worry about it maybe being moved into the standard library at a later date. |
The |
Well, I should clarify: you can get a const I sure don't think you'd get a new language construct into Stable faster than a proc-macro that spits out |
Haha, perhaps I should've looked on crates.io before opening this :D I think I will close this, since there doesn't seem to be any particular interest in introducing something like this to |
Note that you can't directly pass those to C, as &CStr is a fat pointer. I sometimes do macro_rules! lit_cstr {
($s:literal) {
(concat!($s, "\0").as_bytes().as_ptr() as *const ::libc::c_char)
};
} which produces a let errc = libc::sysctlbyname(
lit_cstr!("hw.cpufrequency"),
&mut out as *mut _
&mut msize,
null_mut(),
0,
); and the like. Of course, this doesn't defend against an interior NUL, but it also takes a literal (reducing the likelihood), and is still safe if it happens since it would just see the end of the str sooner. IMO
But for literals I've been using |
Is there a parallel issue for |
It is possible to convert a const rust string to a const c string at compile time now. ( |
An RFC has been accepted. Tracking issue: #105723 |
Why?
Currently, creating a
CStr
, even from a bytestring literal, is quite noisy.Furthermore, there's no way to ensure the well formed-ness of that literal at compile time¹, despite hardcoded C-strings being fairly common when creating bindings for C libraries.
How?
To address this, I would like to propose a new string literal type:
It would function nearly identical to byte-string literals, with the following key differences:
&CStr
, not&[u8]
\0
,\x00
as well as a 'physical' nul byte are forbidden)See also "Alternatives?" below.
Pros?
const
CStr
items (currently not possible on stable)Cons?
Alternatives?
CStr
const. This would still leave the burden of checking on the user.cstr!
macro taking a byte-string literal and applying the needed checks and transformations.¹ It is possible using proc-macros, though this poses different issues regarding ergonomics and stability.
² GitHub code search for
CStr::from_bytes_with_nul_unchecked
The text was updated successfully, but these errors were encountered: