How do I create an enumerated type whose variants are defined by multiple boxes?
I am working on a system that generates and processes I / O events. I would like to do this by creating "filters" that handle events they understand and forward everything else to a fallback filter. For example, a box with console output can define events PrintString
and SetColor
and expose ConsoleOutputFilter
:
// pseudo-Rust for an event filter
impl<Fallback: EventFilter> EventFilter for ConsoleOutputFilter<Fallback> {
fn handle(evt: Event) {
match evt {
PrintString(str) => print!("{}", str),
SetColor(r, g, b) => { /* somehow change the text color */ },
_ => Fallback::handle(evt)
}
}
}
If it ConsoleOutputFilter
receives any other kind of event (say, an event AlertBox
defined by a GUI drawer), it simply ignores it and passes it to the next event filter in the chain.
This seems like a sane design, but I'm not sure how to determine the type Event
. At first, using an enumeration was a reasonable choice, but that would mean that I would have to define all possible events in one box, rather than splitting them into multiple packages.
The answer to a similar question suggested using object objects, but I'm not sure how I could handle the fact that each event type can have a different associated data type. I would also like to be able to serialize / deserialize events (for example, send them over the network), and this doesn't seem possible with trait objects.
In Rust, is there a way to create a type that behaves like an enum with associated data, but whose variants can be defined across multiple boxes?
source to share
No one has answered this question yet
See similar questions:
or similar: