Open kkysen opened 9 months ago
Changing the generated code to create const fn
s is non-trivial since it uses a lot of trait
s, and trait
s don't allow const fn
s. Thus, I rolled my own version in https://github.com/memorysafety/rav1d/pull/497, and it was actually really simple. I'm not sure why the implementation here needs trait
s at all, since the logic is pretty simple.
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: https://github.com/immunant/c2rust/blob/0ba4903f3b8050e958496420a721f3c764360bbd/c2rust-bitfields-derive/src/lib.rs#L233-L258.