You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to evaluate C++ code at compile time, and use produced values in const-expr contexts Rust which is a more strict requirement than just any rvalue.
cpp!(consti32as"int"{/**/})// or if you prefer more syntax noise:cpp!(const:i32as"int"{/**/})cpp!(const -> i32as"int"{/**/})
This is going to be used in expressions rather than statements, thus return keyword and ; semicolon are not needed.
Description
Some things need to be known at compile time, such as const generics and enum values. Offloading them until run time is technically not an option. Thus, would be nice to have cpp!() macro compute C++ expressions while building and generating Rust code.
The first and primary use case for this are enums: copy-pasting values back and forth is an error-prone approach. Imagine writing this instead:
It looks cumbersome, so it might be a better idea to support such scenario natively in cpp. Maybe via an additional cpp_enum! macro in complement to existing cpp_class!?
Implementation
I can think of three ways to implement this, varying by complexity level and overhead. Depending on range of supported data types, developing wire format might be required. But let's start with simple integer and floating types for now.
Compile every const macro as a separate binary with its own main(). Communicate through stdout. Invoke separate binaries one by one.
Pros: simple and straightforward.
Cons: building and linking tens (probably hundreds) of micro-programs gonna take forever and a lot of space.
Compile one binary with a switch in main() that chooses which const expression to return. Communicate through stdout, invoke one by one with different --const <name>.
Pros: only one binary, re-uses existing patterns.
Cons: Requires knowing all cpp! invocations in advance before substituting any of those. (I believe, this was implemented?)
Single binary outputs everything at once.
Pros: Fast.
Const: Complex. Requires some wire format, at least to differentiate between expressions (which is which). Could be implemented with JSON Lines or just a JSON object/map.
All this would've been much easier if Rust would allow us to call FFI code within recently introduced inline const blocks. But that's an entirely different topic worth of RFC on its own.
Concerns
It is not clear as to what to do in case of cross compilation. Can it impose problems for such implementation? We need to compile and run code just as Cargo does that with build.rs. But unlike rustup/cargo, C++ toolchain is probably fixed for the target triple, so resulting binaries might not even run on a build machine.
Diagnostics is probably going to be like in the rest of cpp crate: panic if any of this fails, preferably with reasonable error messages. Compile time evaluation of C++ code, however, introduces new type of errors.
Where to put unsafe keyword and whether it is needed at all? In line with Rust function definitions, I suppose it would be cpp!(const unsafe ...).
In Rust adding const qualifier to a function does not change its further syntax, but cpp! removes arguments list, return keyword and ; semicolon.
I think for the enum use case, the best is to use the bindgen tool.
But apart from that, this can still be useful.
For the implementation, it kind of sounds logical that this should be added in the metadata array, and the cpp! macro could just look for it. That limits us to usize though, but it could similarly be extended for other types.
(ie: Don't create a binary for each one, that would be too much overhead. We would just add some constexpr in the metadata)
Regarding your concerns:
It is not clear as to what to do in case of cross compilation.
Not a problem, this is just a constexpr and it will be evaluated at compile time on the host machine following the rules of constexpr
Diagnostics is probably going to be like in the rest of cpp crate: panic if any of this fails, preferably with reasonable error messages. Compile time evaluation of C++ code, however, introduces new type of errors.
It is not a new type of error. It is simply a C++ compilation error as any other.
These in itself could be improved, but that's another topic.
Where to put unsafe keyword and whether it is needed at all?
unsafe is not required since it is purely compile-time.
In Rust adding const qualifier to a function does not change its further syntax, but cpp! removes arguments list, return keyword and ; semicolon.
Summary
I want to evaluate C++ code at compile time, and use produced values in const-expr contexts Rust which is a more strict requirement than just any rvalue.
This is going to be used in expressions rather than statements, thus
return
keyword and;
semicolon are not needed.Description
Some things need to be known at compile time, such as const generics and enum values. Offloading them until run time is technically not an option. Thus, would be nice to have
cpp!()
macro compute C++ expressions while building and generating Rust code.The first and primary use case for this are enums: copy-pasting values back and forth is an error-prone approach. Imagine writing this instead:
It looks cumbersome, so it might be a better idea to support such scenario natively in
cpp
. Maybe via an additionalcpp_enum!
macro in complement to existingcpp_class!
?Implementation
I can think of three ways to implement this, varying by complexity level and overhead. Depending on range of supported data types, developing wire format might be required. But let's start with simple integer and floating types for now.
const
macro as a separate binary with its ownmain()
. Communicate through stdout. Invoke separate binaries one by one.main()
that chooses whichconst
expression to return. Communicate through stdout, invoke one by one with different--const <name>
.cpp!
invocations in advance before substituting any of those. (I believe, this was implemented?)All this would've been much easier if Rust would allow us to call FFI code within recently introduced inline const blocks. But that's an entirely different topic worth of RFC on its own.
Concerns
cpp
crate: panic if any of this fails, preferably with reasonable error messages. Compile time evaluation of C++ code, however, introduces new type of errors.unsafe
keyword and whether it is needed at all? In line with Rust function definitions, I suppose it would becpp!(const unsafe ...)
.const
qualifier to a function does not change its further syntax, butcpp!
removes arguments list,return
keyword and;
semicolon.Alternatives
Shared Types chapter in CXX crate: https://cxx.rs/shared.html.
The text was updated successfully, but these errors were encountered: