grafos_jobs/work_chunk.rs
1//! Work chunk trait and chunk identifier.
2
3use alloc::vec::Vec;
4use core::fmt;
5use grafos_std::error::FabricError;
6
7/// Stable identifier for a work chunk.
8///
9/// Used as a key into [`JobOutputStore`](crate::JobOutputStore) for
10/// idempotent output capture. Two executions of the same chunk must
11/// produce the same `ChunkId`.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
13pub struct ChunkId(pub u64);
14
15impl fmt::Display for ChunkId {
16 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17 write!(f, "Chunk({})", self.0)
18 }
19}
20
21/// A serializable unit of work with a stable identity.
22///
23/// Implementations must ensure that `chunk_id()` returns the same value
24/// across serialization round-trips. The coordinator uses chunk IDs to
25/// deduplicate outputs and skip already-completed chunks on retry.
26pub trait WorkChunk {
27 /// Return a stable identifier for this chunk.
28 fn chunk_id(&self) -> ChunkId;
29
30 /// Serialize this chunk to bytes (e.g. via postcard).
31 fn to_bytes(&self) -> Vec<u8>;
32
33 /// Deserialize a chunk from bytes.
34 ///
35 /// # Errors
36 ///
37 /// Returns `FabricError::IoError(-200)` on decode failure.
38 fn from_bytes(bytes: &[u8]) -> Result<Self, FabricError>
39 where
40 Self: Sized;
41}