This question already has an answer here:
- How do I return an instance of a trait from a method? 3 answers
I have two enums, NormalColour
and BoldColour
, both of which implement the Colour
trait. They contain Blue
, BoldGreen
, and so on.
I'd like to return values of both of these types from the same function, treating them as though they're just a Colour
value, calling the paint
function on the result, but I can't find a way to coerce the Rust complier into doing this for me. I'd like to be able to write something like this:
pub trait Colour {
fn paint(&self, input: &str) -> String;
}
fn file_colour(stat: &io::FileStat) -> Colour {
if stat.kind == io::TypeDirectory {
Blue
} else if stat.perm & io::UserExecute == io::UserExecute {
BoldGreen
} else {
White
}
}
What type do I have to make the function return for it to work?
I'll eventually like to make more types implement Colour
, which is why I'm not interested in just turning the two enums into one big enum.