Skip to main content

raft_log/chunk/
chunk_id.rs

1use std::fmt;
2use std::ops::Deref;
3
4use crate::num::format_pad_u64;
5
6/// ChunkId represents a unique identifier for a chunk based on its global
7/// offset in the log.
8///
9/// Each chunk in the log has a unique position identified by its starting
10/// offset. This offset serves as the chunk's identifier and can be used to
11/// locate and reference specific chunks within the log.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
13pub struct ChunkId(pub u64);
14
15impl fmt::Display for ChunkId {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        write!(f, "ChunkId({})", format_pad_u64(self.0))
18    }
19}
20
21impl From<u64> for ChunkId {
22    fn from(offset: u64) -> Self {
23        ChunkId(offset)
24    }
25}
26
27impl Deref for ChunkId {
28    type Target = u64;
29
30    fn deref(&self) -> &Self::Target {
31        &self.0
32    }
33}
34
35impl ChunkId {
36    /// Returns the global offset where this chunk begins in the log.
37    ///
38    /// The offset is a monotonically increasing value that represents the
39    /// absolute position of the chunk in the entire log sequence.
40    pub fn offset(&self) -> u64 {
41        self.0
42    }
43}