Reputation: 31
I got a problem with Rust, I want to iterate over a vector of severals enum which implement a custom traits.
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 :
#[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.
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>>
.
What is the best way to achieve this ?
Upvotes: 1
Views: 255
Reputation: 42678
You have a few options, like:
fn to_vec_box() -> Vec<Box<dyn PieceFeature>> {
vec![Box::new(Color::White), Box::new(Color::Dark)]
}
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 {
...
}
Upvotes: 1