Skip to content
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

Could IntoPrimitive::into() be a const function? #148

Open
DavidAntliff opened this issue May 16, 2024 · 1 comment
Open

Could IntoPrimitive::into() be a const function? #148

DavidAntliff opened this issue May 16, 2024 · 1 comment

Comments

@DavidAntliff
Copy link

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:

#[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.

@illicitonion
Copy link
Owner

See #147 which has some discussion about this and an in-progress implementation :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants