-
Notifications
You must be signed in to change notification settings - Fork 207
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
c2rust-bitfields-derive
: Support const
initialization
#1027
Comments
Changing the generated code to create |
kkysen
added a commit
to memorysafety/rav1d
that referenced
this issue
Sep 25, 2023
The few remaining cases are mostly around storing raw pointers. Since these are often just strings, these can be fixed (but are mostly in `tools/`). The other case is when a `fn run_static_initializers` is generated since the type is a `BitfieldStruct` and the setter `fn`s are not `const fn`s. I've opened immunant/c2rust#1027 for this.
kkysen
added a commit
to memorysafety/rav1d
that referenced
this issue
Sep 25, 2023
…erive(BitfieldStruct)]` with `bools_bitfield_struct!` for `const fn`s (#497) `#[derive(c2rust_bitfields::BitfieldStruct)]` doesn't generate `const fn`s (immunant/c2rust#1027), so it can't be used in a `const` block to initialize a `static`. Thus, the `static` has to made `mut` and `unsafe` and initialized at runtime. I opened immunant/c2rust#1027 and looked into making the generated `fn`s `const`, but they used a lot of `trait`s, which can't contain `const fn`s, so it seemed like non-trivial work. I also tried looking for other off-the-shelf bitfield crates, but they all either didn't support `const fn`s or still relied on `trait`s within them, and thus only could create `const fn`s with `nightly` features. Thus, I rolled my own `macro_rules!` macro, `bools_bitfield_struct!` that generates `const fn`s. The macro is specific for bitfields that are always `bool`s, which makes things a bit simpler and have a cleaner API. And it also doesn't have to worry about being `#[repr(C)]` at all, which might makes things a bit simpler, too. Overall, though, it's quite a simple macro, so it seemed much easier to do it this. Since `rav1d` only used bitfields here, I kept the macro private in this file, but if we ever need bitfields that are all `bool`s elsewhere, we can extract the macro. The `c2rust-bitfields` dependency is also now removed as it's unused.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In
rav1d
, we needconst
initialization of bitfieldstruct
s to initializationstatic
s. Otherwise, thestatic
has to bemut
, which isunsafe
to access (or else you have to pay a runtime cost). For example, see this code: https://github.com/memorysafety/rav1d/blob/4029b17b6762d3dcf75c8ac7a592666d483ba78d/src/ipred_prepare_tmpl_8.rs#L273Since
const fn
s can't have&mut
s, the setterfn
s generated can't be madeconst
, but we can addwith
setters that return an updatedstruct
, and these can be madeconst
. And we might as well make all of the gettersconst fn
s, too.It seems like the relevant code is around here:
c2rust/c2rust-bitfields-derive/src/lib.rs
Lines 233 to 258 in 0ba4903
The text was updated successfully, but these errors were encountered: