Amanieu / cstr_core

Implementation of CStr and CString for no_std environments
Apache License 2.0
41 stars 17 forks source link

Make from_bytes_with_nul nightly const #23

Closed chrysn closed 2 years ago

chrysn commented 2 years ago

Filing this as an issue for initial feedback before I turn the pile of test I have around into a proper PR.

Steps needed (conditional to the nightly feature unless it happens to work on MSRV too; in increasing order of contentiousness):

Is any of that a no-go for the nightly feature, and do you have preferences on whether to accept reduced run-time performance of an unoptimized memchr or rather have a separate function name for const from_bytes_with_nul?


Use case: In the riot-wrappers crate I'm getting a name as a const byte array from C through bindgen, and no matter how hard I push LTO & co, a non-const access function doesn't eiliminate the error cases. With everything in there const, the access function can be replaced with a const value, and that pushes the panics to the build time.

Amanieu commented 2 years ago

For from_bytes_with_nul you could try using https://doc.rust-lang.org/nightly/std/intrinsics/fn.const_eval_select.html, but this is unlikely to ever be stabilized.

For your specific use case you can use from_bytes_with_nul_unchecked which is already available as a const fn with the nightly feature.

chrysn commented 2 years ago

I'm trying to avoid unchecked because there's nothing on the C side that guarantees upholding the null properties require by CStr.

The const_eval_select is nice for other applications, but I'm looking at the road toward getting a const result right from cstr_core, and const_eval is probably not suitable for here -- my impression of the nightly feature is that it's only there as long as some const stuff can not be done in stable, but will compatibly become available on stable over time.

Do I take your comment right that you'd rather not have more nightly features added at this time?

Amanieu commented 2 years ago

const_eval_select is a mechanism for using different code paths for const and non-const evaluation. The idea is to use a const-compatible version of memchr for const-eval while using an optimized version at runtime.

I'm happy to add a new nightly feature. However since const_eval_select is unlikely to be stabilized anytime soon, this means that the const version of from_bytes_with_nul will only be available on nightly Rust.

If you need this to be available on stable Rust then a separate const_from_bytes_with_nul is probably the only reasonable option.

I'm trying to avoid unchecked because there's nothing on the C side that guarantees upholding the null properties require by CStr.

You could just check it yourself with your own const version of memchr.

chrysn commented 2 years ago

Doing an own check works for the time being, but then I'd still need const to_str -- but that works with fewer problems, PR incoming.

I'd leave this open for whenever it's the right time to do from_bytes_with_nul (but understand if you prefer to close it WONTFIX FORNOW due to the memchr hassle).

Amanieu commented 2 years ago

I'm happy to accept an implementation that uses const_eval_select for a const from_bytes_with_nul.

chrysn commented 2 years ago

I'll add that then, no hurry with a release until that's done. Thanks.