I have a Vec of objects, each of which have a state, which is an enum:
enum State {
Active,
Idle
}
struct Msg {
state: State,
// .. other stuff ..
}
let mut v: Vec<Msg> = Vec::new();
It would be nice to be able to wrap the Msg in the State instead:
enum State {
Active(Msg),
Idle(Msg)
}
The only reason this wasn't done is because Msg is quite large, state changes can happen very often, and I didn't want to deal with the overhead of copying Msg each time. I've started making plans to Box the Msg.
While thinking about alternatives it made me wonder if there is any way to mutate only the enum variant, but leaving its data untouched? I can't imagine this is possible (at least not in a safe manner) -- but could Rust be made to allow this to be done (if the data is of the exact same type in the variants being switched)?
When doing this, you must make sure that the field types match (or that transmuteing from one field type to the other would be safe) and that you don’t write a number that isn't any of the declared discriminants.