-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
env!
for non-string constants
#1907
Comments
I glanced at the implementation of |
On a meta note, improved |
Yes, this is precisely what I mean. |
This should simply "fall out of" CTFE improvements so there's nothing special about |
This was closed in 2018, but CTFE appears to be in now (?) and if I try this (I thought, very reasonable) code:
I get:
(I had to get the tuple crate and
Through a chain of individually reasonable technical decisions, Rust has wound up in a situation where there is no way to get compile-time access to your own version number (as set in the language's own official package manager system) as an integer. That seems "obviously" weird, and the fix— even more CTFE improvements, something like #20— does not even seem to have a current RFC, so it could be literally years more before there is progress. Is there really nothing "actionable" here? |
Making |
@mcclure a small procedural note; issues on this tracker are mostly "a place for anyone to say some thoughts on a topic that anyone else may be interested in," it doesn't play any part in the actual process of changing things in Rust. An issue here being open or closed doesn't mean anything with regards to Rust feature planning, tracking, or anything else. So, in this specific case, yes, there very may well be some things to be done, as @luser mentions, but that doesn't mean this issue particularly needs to be open or closed as part of it. |
For some background, Rust had consteval before 1.0, though if we mean What the closing message meant by "CTFE improvements" is "future additions to CTFE" (e.g. trait The whole story is a bit sad, really - miri itself is really powerful compared to what we currently let you do without e.g. de- In a sense, Rust has to do all of the implementation legwork e.g. C++ would have to (which has been done in terms of miri, and most of it years ago), but on top of that we have to design systems that let us check correct use of abstractions without breaking their boundary and exposing the insides directly (as e.g. C++ does). Also, most of the work on CTFE (and more recently, const generics as well) has been driven by unpaid volunteers, which can only be so rapid-paced and comprehensive.
Indeed it could (given everything I've just said). But it's not a significant problem since you have at least 3 options:
One last thing I'll say is that Cargo version strings don't have to be triples of integers, you can have pre-release versions, so the concept of parsing it has limited usefulness, and is best kept as an opaque string if possible. |
It has! |
I wrote the use konst::{primitive::parse_usize, result::unwrap_ctx};
pub const VERSION_MAJOR: usize = unwrap_ctx!(parse_usize(env!("CARGO_PKG_VERSION_MAJOR"))); According to the cargo reference the prerelease version of a package is separate from the patch version.
this means that parsing the patch version will give you an integer even in prerelease versions |
Make {integer}::from_str_radix constant This commit makes FromStr on integers constant so that `const x: u32 = "23".parse();` works. More practical use-case is with environment variables at build time as discussed in rust-lang/rfcs#1907. Tracking issue rust-lang#59133. ACP: rust-lang/libs-team#74
There are some useful environment variables that are or could be non-string values, like
CARGO_PKG_VERSION_*
,NUM_JOBS
, andDEP_*_*
. Currently, however, they are only exposed to Rust as constant strings, not numbers, so it is impossible to do something like:You need to parse the string into an integer and then unwrap the result, which are both non-constant operations. If there were
env!
-like macros for boolean, integral, and fractional values and characters, then it would be possible to use them in new places like constant definitions, patterns, and eventually generics. It is possible to work around this in some cases by defining functions instead that internally parse the string into the desired type and using comparisons instead of pattern matching, but this is not ideal for ergonomics and potentially efficiency.Of course, this would also be satisfied by making the appropriate standard library functions (
str::parse
,Option::unwrap
)const fn
s, which would work for even user-defined types, but the macro solution could be implemented immediately without having to change any existing APIs. These macros could also be implemented as procedural macros, but support for this feature outside of deriving traits is poor as of the time of writing.The text was updated successfully, but these errors were encountered: