Closed chrysn closed 3 years ago
I stand slightly corrected: The macro is not in std but in the cstr
crate -- but either way, it'd be good to have here right away.
It's not enough to just add a trailing zero, you must also ensure that there are no interior nul bytes in the string. So you still have to use the safe from_bytes_with_nul
function.
I'd be happy to accept a PR to add a proc macro sub-crate to cstr_core
which contains a port of the cstr!
macro from the cstr
crate.
I was unaware that no-interior-nulls is a guarantee that CStr gives (the stripped down copy I'm maintaining in-tree until this is resolved and I can just switch over doesn't need them).
The proc macro appears to be a bit tricky to get to run in no_std.
The approach I'm exploring is to have a const
function that'd do the checking. In first experiments, I got that down to zero-cost in the good case -- meaning that adding an assert!($crate::CStr::is_cstr(a.as_bytes()));
to the macro does not make the release mode binary any larger. (The non-release mode gets shorter by a lot, as in my test example it panics early in main; making it raise at build time would be nice but can later be added when const_panic
feature is stable).
The check function employed is necessarily different from the checks done in the safe conversion; until all involved is const, that makes sense. (Const function writing is a bit awkward still, but IMO still more readable than a proc macro).
Would that approach work for you? If so, I can work it into this crate and make a PR out of it.
The std cstr has a convenient macro that allows in-place creation of C strings, a la
This should be easy to do with a macro like
which I'm so far using in the riot-sys crate (but would prefer to migrate to something off-the-shelf).
I can shape it into a PR (but not right now), but if there's a good reason not to do this, please let me (or whoever would next pick this up faster) know.