I ended up with this in my application so I could assign different colors to the same variable.
trait BgColor {
fn write_to(&self, w: &mut dyn fmt::Write) -> fmt::Result;
}
impl<C> BgColor for color::Bg<C>
where
C: color::Color,
{
fn write_to(&self, w: &mut dyn fmt::Write) -> fmt::Result {
write!(w, "{}", self)
}
}
impl Display for dyn BgColor {
fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), fmt::Error> {
self.write_to(f)
}
}
// Usage:
let bg_color: Box<dyn BgColor> = if is_active {
Box::new(color::Bg(color::LightBlack))
} else {
Box::new(color::Bg(color::Black))
};