使用 Rust 的枚举作为双向查找 table

Using Rust's enum as a bi-directional lookup table

我正在编写通过 D-Bus 与 NetworkManager 交互的代码,我想使用一个 enum using as key-value like scheme。

例如,我想一次使用值 0 作为数字零,并在不同的上下文中使用此 enum:

的字符串 Unknown
pub enum NetworkManagerState {
    Unknown = 0,
    Asleep = 10,
    Disconnected = 20,
    Disconnecting = 30,
    Connecting = 40,
    ConnectedLocal = 50,
    ConnectedSite = 60,
    ConnectedGlobal = 70,
}

最干净、最惯用的方法是什么?有没有一种方法可以定义一个基于元组的枚举,使每个条目看起来像 (ConnectedSite, 60u32, "Connected") 并根据上下文使用每个元组属性。

  1. 要将枚举转换为 int,您可以使用 as i32
  2. 要将 i32 投射到您的枚举中,您可以使用 enum_primitive 箱子:

cargo.toml:

[dependencies]
enum_primitive = "*"

main.rs:

#[macro_use]
extern crate enum_primitive;
use enum_primitive::FromPrimitive;

enum_from_primitive! {
#[derive(Debug, PartialEq)]
pub enum NetworkManagerState {
    Unknown = 0,
    Asleep = 10,
    Disconnected = 20,
    Disconnecting = 30,
    Connecting = 40,
    ConnectedLocal = 50,
    ConnectedSite = 60,
    ConnectedGlobal = 70,
}
}

impl From<i32> for NetworkManagerState {
    fn from(val: i32) -> NetworkManagerState {
        NetworkManagerState::from_i32(val).expect("passed Value does not match an enum value!")
    }
}
impl From<NetworkManagerState> for i32 {
    fn from(val: NetworkManagerState) -> i32 {
        val as i32
    }
}


fn main() {
    let value_i32 = 40;
    let value_enum = NetworkManagerState::Connecting;

    // enum to i32
    println!("{:?}", value_enum as i32);

    // i32 to enum
    println!("{:?}", NetworkManagerState::from_i32(value_i32).unwrap());

    // using from trait
    let y: i32 = NetworkManagerState::Connecting.into();
    println!("{:?}", y);

    // using from trait
    let x: NetworkManagerState = 40.into();
    println!("{:?}", x);
}

使用 from 特性你可以让你的函数接受所有可以转换为你的枚举的东西:

fn fancy_function<T: Into<NetworkManagerState>>(value: T) {
    let internal: NetworkManagerState = value.into();
    println!("{:?}", internal);
}

...

fancy_function(60);
fancy_function(NetworkManagerState::Asleep);