Rustでヒストリー機能実装

non-takuwan.hatenablog.com 一晩寝たらRustの件については解決した。

こうする。

struct Context {
    state: State,
    history_manager: HistoryManager,
}

trait Action {
    fn do_undo(state: &mut State);
}

struct HistoryManager {
    ...
    undo_list: Vec<Box<Action>>,
    ...
}

impl HistoryManager {
    pub fn do_undo(state: &mut State) {
        if let Some(mut action) = self.undo_list.pop() {
            action.do_undo(state);
        }
    }
}

fn do_undo(context: &mut Context) {
    let mut history_manager = &mut context.history_manager;
    let mut state = &mut context.state;
    history_manager.do_undo(state);
}

#[no_mangle]
pub unsafe fn c_do_undo(context: *mut Context) {
    do_undo(&mut *context);
}

Contextへ二重の&mut借用が問題になっているのではなく、ホントはHistoryManagerへの&mut借用が問題になってたんだという事に気がついた。Undo/Redo時にActionHistoryManagerを変更する必要がないので、これで構わない。

一晩かかると辛いので、パッとわかるようになりたい。 他のふたつも解決したいけど、もうちょい難しそうでこれもまた若干辛い。