use std::io;
use std::sync::mpsc::SyncSender;
use crate::RaftLog;
use crate::Types;
use crate::types::Segment;
pub trait RaftLogWriter<T: Types> {
fn save_user_data(
&mut self,
user_data: Option<T::UserData>,
) -> Result<Segment, io::Error>;
fn save_vote(&mut self, vote: T::Vote) -> Result<Segment, io::Error>;
fn append<I>(&mut self, entries: I) -> Result<Segment, io::Error>
where I: IntoIterator<Item = (T::LogId, T::LogPayload)>;
fn truncate(&mut self, index: u64) -> Result<Segment, io::Error>;
fn purge(&mut self, upto: T::LogId) -> Result<Segment, io::Error>;
fn commit(&mut self, log_id: T::LogId) -> Result<Segment, io::Error>;
fn flush(&mut self, callback: Option<T::Callback>)
-> Result<(), io::Error>;
}
#[allow(dead_code)]
pub(crate) fn blocking_flush<T>(rl: &mut RaftLog<T>) -> Result<(), io::Error>
where T: Types<Callback = SyncSender<Result<(), io::Error>>> {
let (tx, rx) = std::sync::mpsc::sync_channel(1);
rl.flush(Some(tx))?;
rx.recv().map_err(|_e| {
io::Error::other("Failed to receive flush completion")
})??;
Ok(())
}