I am trying to implement an undo/redo system, using the undo_2 crate (I'm creating a 3d editor using bevy). I have a large data structure called Kmp
(the data of the program) which contains Section
s (wrapper for a Vec
) of other data structures, all of which implement the KmpData
trait.
pub struct Kmp {
pub header: Header,
pub ktpt: Section<Ktpt>,
pub enpt: Section<Enpt>,
pub enph: Section<Path>,
pub itpt: Section<Itpt>,
pub itph: Section<Path>,
pub ckpt: Section<Ckpt>,
pub ckph: Section<Path>,
pub gobj: Section<Gobj>,
pub poti: Section<Poti>,
pub area: Section<Area>,
pub came: Section<Came>,
pub jgpt: Section<Jgpt>,
pub cnpt: Section<Cnpt>,
pub mspt: Section<Mspt>,
pub stgi: Section<Stgi>,
}
There are 3 possible ways the data structure can be modified by the program: an entry to a section can be created, modified, or deleted. Using the undo_2 crate, I would have an enum containing these 3 possibilities, something like this:
enum UndoCommand {
// I imagine each enum variant would need to store an index to the data and the data itself
// this gives a compiler error though because KmpData can't be created into an object
Create(usize, Arc<Mutex<dyn KmpData>>),
// I imagine modify would need to store the 'before' and 'after'
Modify(usize, Arc<Mutex<dyn KmpData>>, Arc<Mutex<dyn KmpData>>),
Delete(usize, Arc<Mutex<dyn KmpData>>),
}
The enum variants need to contain the data created/modified/deleted, which could be any one of the different data structures within the Kmp. I need a way to generically store the data, and modify the kmp with that data in the undo/redo functions.
How should I structure my code to make this possible?