async fn persist(&self) -> io::Result<()> {
        let (persist, snapshot): (Persist, Vec<u8>) = {
            let raft = self.inner.lock().unwrap();
            (
                Persist {
                    term: raft.state.term,
                    logs: raft.logs.clone(),
                    vote_for: raft.state.vote_for,
                    last_include_index: raft.state.last_included_index,
                    last_include_term: raft.state.last_included_term,
                },
                raft.snapshot.clone(),
            )
        };
        // TODO implement snapshot
        let state = bincode::serialize(&persist).unwrap();

        // you need to store persistent state in file "state"
        // and store snapshot in file "snapshot".
        // DO NOT change the file names.
        let file = fs::File::create("state").await?;
        file.write_all_at(&state, 0).await?;
        // make sure data is flushed to the disk,
        // otherwise data will be lost on power fail.
        file.sync_all().await?;

        let file = fs::File::create("snapshot").await?;
        file.write_all_at(&snapshot, 0).await?;
        file.sync_all().await?;
        Ok(())
    }
/// save Raft's persistent state to stable storage,
/// where it can later be retrieved after a crash and restart.
/// see paper's Figure 2 for a description of what should be persistent.
save Raft's persistent state to stable storage,
where it can later be retrieved after a crash and restart.
see paper's Figure 2 for a description of what should be persistent.
save Raft ' s persistent state to stable storage where it can later be retrieved after a crash and restart . see paper ' s Figure 2 for a description of what should be persistent .
async fn persist(&self) -> io::Result<()> {
        let (persist, snapshot): (Persist, Vec<u8>) = {
            let raft = self.inner.lock().unwrap();
            (
                Persist {
                    term: raft.state.term,
                    logs: raft.logs.clone(),
                    vote_for: raft.state.vote_for,
                    last_include_index: raft.state.last_included_index,
                    last_include_term: raft.state.last_included_term,
                },
                raft.snapshot.clone(),
            )
        };
        // TODO implement snapshot
        let state = bincode::serialize(&persist).unwrap();
        // you need to store persistent state in file "state"
        // and store snapshot in file "snapshot".
        // DO NOT change the file names.
        let file = fs::File::create("state").await?;
        file.write_all_at(&state, 0).await?;
        // make sure data is flushed to the disk,
        // otherwise data will be lost on power fail.
        file.sync_all().await?;
        let file = fs::File::create("snapshot").await?;
        file.write_all_at(&snapshot, 0).await?;
        file.sync_all().await?;
        Ok(())
    }
async fn persist ( & self ) -> io :: Result < ( ) > { let ( persist , snapshot ) : ( Persist , Vec < u8 > ) = { let raft = self . inner . lock ( ) . unwrap ( ) ; ( Persist { term : raft . state . term , logs : raft . logs . clone ( ) , vote_for : raft . state . vote_for , last_include_index : raft . state . last_included_index , last_include_term : raft . state . last_included_term , } , raft . snapshot . clone ( ) , ) } ; // TODO implement snapshot let state = bincode :: serialize ( & persist ) . unwrap ( ) ; // you need to store persistent state in file "state" // and store snapshot in file "snapshot". // DO NOT change the file names. let file = fs :: File :: create ( " " ) . await ? ; file . write_all_at ( & state , 0 ) . await ? ; // make sure data is flushed to the disk, // otherwise data will be lost on power fail. file . sync_all ( ) . await ? ; let file = fs :: File :: create ( " " ) . await ? ; file . write_all_at ( & snapshot , 0 ) . await ? ; file . sync_all ( ) . await ? ; Ok ( ( ) ) }
save Raft's persistent state to stable storage,
where it can later be retrieved after a crash and restart.
save Raft ' s persistent state to stable storage where it can later be retrieved after a crash and restart .
&self
&self