Cannot Convert enum to u8

I am using nothing but rmp in this case. I have tried #[repr(u8)] and adding pub to the enum.

As you guess, I am very new to rust.

enum PacketType {
    Connect,
    Ping,
    ChangeKeys {
        cipher_key: Vec<u8>,
    },
    Disconnect {
        error_code: bool,
    },
...
fn encode_connect(client_type: u8, key: Vec<u8>) -> Vec<u8> {
    let mut buf = Vec::new();

    rmp::encode::write_u8(&mut buf, PacketType::Connect as u8).unwrap();
    rmp::encode::write_bin(&mut buf, &key.into_raw_parts())
}

You can only cast an enum to an integer if no variants have any fields.

Apparently there is no safe way yet(?) to do what you want directly

https://doc.rust-lang.org/reference/items/enumerations.html#pointer-casting

You can either use the code from the example or match all variants and do it yourself.

Edit: Another example here discriminant in std::mem - Rust

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.