简体   繁体   English

迭代实现自定义特征的枚举

[英]Iterate over enums which implement a custom trait

I got a problem with Rust, I want to iterate over a vector of severals enum which implement a custom traits.我遇到了 Rust 的问题,我想迭代一个实现自定义特征的多个枚举向量。

My custom trait:我的习惯特征:

pub trait PieceFeature
    {
        fn acronym(&self) -> &str;
        fn name(&self) -> &str;
        fn color(&self) -> ConsoleColor;
        fn to_vec_box() -> Vec<Box<Self>> where Self: Sized;
    }

I got two enums, the first is Color which implement PieceFeature trait and Display trait:我有两个枚举,第一个是实现 PieceFeature 特性和 Display 特性的 Color:

#[derive(Debug, Clone, PartialEq, IntoEnumIterator)]
    pub enum Color {
        White,
        Dark,
    }

    impl PieceFeature for Color {
        fn acronym(&self) -> &str {
            match self {
                Self::White => "W",
                Self::Dark => "D",
            }
        }

        fn name(&self) -> &str {
            match self {
                Self::White => "White",
                Self::Dark => "Dark"
            }
        }

        fn color(&self) -> ConsoleColor {
            ConsoleColor::Blue
        }

        fn to_vec_box() -> Vec<Box<Color>> {
            vec![Box::new(Color::White), Box::new(Color::Dark)]
        }        
    }
    impl Display for Color {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "{}", self.color().paint(self.acronym()))
        }
    }

And I got a second (Height) which do exactly the same (I don't write the implementation, but it's exactly as previous)我得到了第二个(高度),它做的完全一样(我没有写实现,但它和以前完全一样)

 #[derive(Debug, Clone, PartialEq, IntoEnumIterator)]
    pub enum Height {
        Small,
        Tall,
    }

Now I want to dynamically loop over my enum to display a legend in my console application.现在我想动态循环我的枚举以在我的控制台应用程序中显示图例。 I tried severals things but it doesn't work as I want.我尝试了几件事,但没有按我的意愿工作。 Here is my newbie code which work:这是我的新手代码,它可以工作:

let pieces_feature: Vec<Box<dyn PieceFeature>> = vec![
            Box::new(Color::Dark),
            Box::new(Color::White),
            Box::new(Height::Small),
            Box::new(Height::Tall),
        ];`
         // some code ...
         //Draw legend
         for (i, e) in pieces_feature.into_iter().enumerate() {
            legend = format!("{} \t {}: {}", legend, e.color().paint(e.acronym()), e.name());
        }
        //....

My code works, but I'm not happy to not dynamically build my "pieces_feature" vector.我的代码有效,但我不高兴不动态构建我的“pieces_feature”向量。 As you can see, I tried to implement a custom function "to_vec_box()" but I got a error which says that Vec<Box<Self>> is not the same as Vec<Box<dyn PieceFeature>> .如您所见,我尝试实现自定义 function“to_vec_box()”,但出现错误,指出Vec<Box<Self>>Vec<Box<dyn PieceFeature>> What is the best way to achieve this?实现这一目标的最佳方法是什么?

You have a few options, like:您有几个选择,例如:

  1. Change your types to what you really want in the method itself (notice you would have to change also the trait method signature):将您的类型更改为您在方法本身中真正想要的类型(注意您还必须更改特征方法签名):
fn to_vec_box() -> Vec<Box<dyn PieceFeature>> {
    vec![Box::new(Color::White), Box::new(Color::Dark)]
}
  1. Iterate over them while changing the types on iteration:在更改迭代类型的同时迭代它们:
let features = Color::to_vec_box()
    .into_iter()
    .map(|p| p as Box<dyn PieceFeature>)
    .chain(Height::to_vec_box()
        .into_iter()
        .map(|p| p as Box<dyn PieceFeature>)
    );

for feature in features {
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM