hydro2_async_scheduler/
checkpoint_callback.rs

1// ---------------- [ File: src/checkpoint_callback.rs ]
2crate::ix!();
3
4/// A trait invoked periodically to record checkpoint data.
5/// This can write partial progress to disk, a DB, etc.
6#[async_trait]
7pub trait CheckpointCallback: Debug + Send + Sync {
8
9    async fn checkpoint(&self, completed_nodes: &[usize]) -> Result<(), NetworkError>;
10}
11
12#[derive(Debug)]
13pub struct NoOpCheckpointCallback;
14unsafe impl Send for NoOpCheckpointCallback {}
15unsafe impl Sync for NoOpCheckpointCallback {}
16
17#[async_trait]
18impl CheckpointCallback for NoOpCheckpointCallback {
19
20    async fn checkpoint(&self, completed_nodes: &[usize]) 
21        -> Result<(), NetworkError> 
22    {
23        Ok(())
24    }
25}
26
27//-----------------------------------[mock]
28pub type MockCheckpointType = Arc<AsyncMutex<Vec<Vec<usize>>>>;
29
30/// A mock checkpoint callback that records each checkpoint invocation.
31#[derive(Builder,Getters,Debug)]
32#[getset(get="pub")]
33#[builder(setter(into))]
34pub struct MockCheckpointCallback {
35    checkpoints: MockCheckpointType,
36}
37
38impl From<&MockCheckpointType> for MockCheckpointCallback {
39    fn from(x: &MockCheckpointType) -> Self {
40        Self { checkpoints: x.clone() }
41    }
42}
43
44#[async_trait]
45impl CheckpointCallback for MockCheckpointCallback {
46    async fn checkpoint(&self, completed_nodes: &[usize]) -> Result<(), NetworkError> {
47        let mut guard = self.checkpoints.lock().await;
48        guard.push(completed_nodes.to_vec());
49        Ok(())
50    }
51}