wral/
entry.rs

1use arbitrary::Arbitrary;
2use mkit::Cborize;
3
4use std::{
5    cmp,
6    fmt::{self, Display},
7    result,
8};
9
10/// Single Op-entry in Write-ahead-log.
11#[derive(Debug, Clone, Default, Cborize, Arbitrary)]
12pub struct Entry {
13    // Index seqno for this entry. This will be monotonically
14    // increasing number.
15    seqno: u64,
16    // Operation to be logged.
17    op: Vec<u8>,
18}
19
20impl Eq for Entry {}
21
22impl PartialEq for Entry {
23    fn eq(&self, other: &Self) -> bool {
24        self.seqno.eq(&other.seqno)
25    }
26}
27
28impl Display for Entry {
29    fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
30        write!(f, "entry<seqno:{}>", self.seqno)
31    }
32}
33
34impl PartialOrd for Entry {
35    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
36        Some(self.cmp(other))
37    }
38}
39
40impl Ord for Entry {
41    fn cmp(&self, other: &Self) -> cmp::Ordering {
42        self.seqno.cmp(&other.seqno)
43    }
44}
45
46impl Entry {
47    const ID: u32 = 0x0;
48
49    #[inline]
50    pub fn new(seqno: u64, op: Vec<u8>) -> Entry {
51        Entry { seqno, op }
52    }
53
54    #[inline]
55    pub fn to_seqno(&self) -> u64 {
56        self.seqno
57    }
58
59    #[inline]
60    pub fn unwrap(self) -> (u64, Vec<u8>) {
61        (self.seqno, self.op)
62    }
63}
64
65#[cfg(test)]
66#[path = "entry_test.rs"]
67mod entry_test;