gpt4 book ai didi

enums - 将Rust枚举转换为子枚举

转载 作者:行者123 更新时间:2023-12-03 11:36:59 32 4
gpt4 key购买 nike

我正在创建std::sync::atomic::Ordering的子集:

use std::sync::atomic::Ordering;

pub enum StoreOrdering {
Relaxed,
Release,
SeqCst
}
impl Into<Ordering> for StoreOrdering {
fn into(self) -> Ordering {
match self {
Self::Relaxed => Ordering::Relaxed,
Self::Release => Ordering::Release,
Self::SeqCst => Ordering::SeqCst
}
}
}
impl std::convert::TryFrom<Ordering> for StoreOrdering {
type Error = (); // HACK
fn try_from(ord: Ordering) -> Result<Self, Self::Error> {
match ord {
Ordering::Relaxed => Ok(Self::Relaxed),
Ordering::Release => Ok(Self::Release),
Ordering::SeqCst => Ok(Self::SeqCst),
_ => Err(())
}
}
}

enum LoadOrdering {
Acquire,
Relaxed,
SeqCst
}
// ???
如您所见,现在我需要再次为 impl甚至可能为 match以及任何枚举子集用 StoreOrdering <-> LoadOrdering es编写这两个 StoreOrdering <-> LoadOrdering。如何避免这种样板?

最佳答案

Rust不像C++那样支持鸭式输入。泛型只能访问的功能由特征范围决定。
因此,任何类似鸭子类型的行为都必须使用宏来完成。
为此,您可以使用下面的给定宏。
它仅适用于简单的C样式宏。它创建枚举并自动生成到给定 super 枚举的转换。

use std::sync::atomic::Ordering;
use std::convert::TryInto;

// Create the store ordering
sub_enum!(StoreOrdering of Ordering {
Relaxed,
Release,
SeqCst
});

// Create the load ordering
sub_enum!(LoadOrdering of Ordering {
Acquire,
Relaxed,
SeqCst
});

#[macro_export]
macro_rules! sub_enum {
($sub_enum_name:ident of $super_enum_name:ty {
$($variant:ident),* $(,)?
}) => {
pub enum $sub_enum_name {
$($variant,)*
}

impl From<$sub_enum_name> for $super_enum_name {
fn from(val: $sub_enum_name) -> $super_enum_name {
match val {
$(<$sub_enum_name>::$variant => <$super_enum_name>::$variant,)*
}
}
}

impl std::convert::TryFrom<$super_enum_name> for $sub_enum_name {
type Error = ();
fn try_from(val: $super_enum_name) -> Result<Self, Self::Error> {
match val {
$(<$super_enum_name>::$variant => Ok(Self::$variant),)*
_ => Err(())
}
}
}
}
}

fn main() {
let store = StoreOrdering::SeqCst;
let general: Ordering = store.into();
let load: LoadOrdering = general.try_into().unwrap();
}
Playground link
当然,还有很多可以改进的地方。
但是,这应该可以立即解决您的问题。

关于enums - 将Rust枚举转换为子枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63937826/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com