How one can cast ref to enum value as u16 (base enum type) in Rust?

4ntoine

Given:

#[repr(u16)]
#[derive(PartialEq, Debug, Eq, Hash, Clone)]
pub enum SomeEnum {
    SomeValue1 = 1,
    SomeValue2 = 1 << 2 | Self::SomeValue1 as u16, // some bitmasks
    // ...
}

pub fn some_check(actual_value_ref: &SomeEnum, base_value: SomeEnum) -> bool {
    let actual_value_u16 = actual_value_ref.clone() as u16; // how to cast without cloning?
    let base_value_u16 = base_value as u16;
    actual_value_u16 & base_value_u16 == base_value_u16 // bitmask operation
}

How one can get value by reference for a primitive without explicit cloning? Cloning of u16 does not look like a tragedy but what's the right way to do it? Since enum is explicitly marked as #[repr(u16)] why compiler doesn't do it out-of-box?

phimuemue

To avoid explicit cloneing, I think it should suffice to derive(Copy) for SomeEnum.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I cast int to enum?

From Java

How to delete an enum type value in postgres?

From Java

Rust returning enum as generic type

From Java

Type annotations for Enum value

From Dev

Swift integer type cast to enum

From Dev

How to forbid some enum value in enum type to XML representation mapping?

From Dev

Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type

From Dev

How to cast java.lang.reflect.Type to enum?

From Dev

How do you access the elements of an complex enum type in rust?

From Dev

Given an object, which is an array of enum values, how can I get an Enum[] without knowing the enum type

From Dev

How to specify the representation type for an enum in Rust to interface with C++?

From Dev

The value passed in must be an enum base or an underlying type for an enum, such as an Int32.Parameter name: value

From Dev

How to Type check for enum instance base class?

From Dev

How to cast / assign one enum value to another enum

From Dev

How can we extend all enum type?

From Dev

How to cast a String value to an Enum value by Class?

From Dev

How can I cast <T> as Enum when using EnumToList<T>

From Dev

How to cast Enum value as a constant c#

From Dev

How to set the default value of enum type variable?

From Dev

How to set value for one enum?

From Dev

How to fix "Can't infer the SQL type to use for an instance of enum" error when inserting enum value?

From Dev

Type annotations for Enum value

From Dev

How to cast an enum ordinal as a String?

From Dev

How to cast java.lang.reflect.Type to enum?

From Dev

How do you access the elements of an complex enum type in rust?

From Dev

How to Cast Enum To Int

From Dev

How can one pass array to a constructor in an enum?

From Dev

How to display one value of enum with Thymeleaf

From Dev

How to get enum value having enum type name and enum option name

Related Related

  1. 1

    How can I cast int to enum?

  2. 2

    How to delete an enum type value in postgres?

  3. 3

    Rust returning enum as generic type

  4. 4

    Type annotations for Enum value

  5. 5

    Swift integer type cast to enum

  6. 6

    How to forbid some enum value in enum type to XML representation mapping?

  7. 7

    Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type

  8. 8

    How to cast java.lang.reflect.Type to enum?

  9. 9

    How do you access the elements of an complex enum type in rust?

  10. 10

    Given an object, which is an array of enum values, how can I get an Enum[] without knowing the enum type

  11. 11

    How to specify the representation type for an enum in Rust to interface with C++?

  12. 12

    The value passed in must be an enum base or an underlying type for an enum, such as an Int32.Parameter name: value

  13. 13

    How to Type check for enum instance base class?

  14. 14

    How to cast / assign one enum value to another enum

  15. 15

    How can we extend all enum type?

  16. 16

    How to cast a String value to an Enum value by Class?

  17. 17

    How can I cast <T> as Enum when using EnumToList<T>

  18. 18

    How to cast Enum value as a constant c#

  19. 19

    How to set the default value of enum type variable?

  20. 20

    How to set value for one enum?

  21. 21

    How to fix "Can't infer the SQL type to use for an instance of enum" error when inserting enum value?

  22. 22

    Type annotations for Enum value

  23. 23

    How to cast an enum ordinal as a String?

  24. 24

    How to cast java.lang.reflect.Type to enum?

  25. 25

    How do you access the elements of an complex enum type in rust?

  26. 26

    How to Cast Enum To Int

  27. 27

    How can one pass array to a constructor in an enum?

  28. 28

    How to display one value of enum with Thymeleaf

  29. 29

    How to get enum value having enum type name and enum option name

HotTag

Archive