Open DavidAntliff opened 5 months ago
Would it be possible for MyEnum.into() to be a const function?
MyEnum.into()
Then I could use it like this, for example with something like the bitfield-struct crate:
bitfield-struct
#[derive(Debug, IntoPrimitive)] #[repr(u16)] enum Opcode { Jump = 1, Halt = 2, // ... etc } #[bitfield(u32)] pub(crate) struct JumpInstruction { #[bits(16)] address: Address, #[bits(16, default=Opcode::Jump.into())] opcode: u16, }
Unfortunately, as things stand:
error[E0015]: cannot call non-const fn `<Opcode as Into<u8>>::into` in constant functions --> src/ops.rs:75:36 | 75 | #[bits(16, default=Opcode::Jump.into())] | ^^^^^^ |
Is this technically possible? The discriminants of the enum variants are static const, I assume? However, I think I read that trait function implementations cannot be const fn in stable Rust.
const fn
See https://github.com/illicitonion/num_enum/pull/147 which has some discussion about this and an in-progress implementation :)
Would it be possible for
MyEnum.into()
to be a const function?Then I could use it like this, for example with something like the
bitfield-struct
crate:Unfortunately, as things stand:
Is this technically possible? The discriminants of the enum variants are static const, I assume? However, I think I read that trait function implementations cannot be
const fn
in stable Rust.