1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Empty log entry.
//!
//!
//! Used for log entry types that need no additional data beyond the entry
//! type itself, such as checkpoint start/end markers.
use bytes::{BufMut, BytesMut};
use std::io;
use thiserror::Error;
/// Error type for empty log entry operations.
#[derive(Debug, Error)]
pub enum EmptyLogEntryError {
#[error("I/O error: {0}")]
Io(#[from] io::Error),
}
/// Empty log entry.
///
/// Contains no information - the LogEntryType in the header is sufficient.
/// A single dummy byte is written to satisfy buffer requirements in checksums
/// and file readers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EmptyLogEntry;
impl EmptyLogEntry {
/// Creates a new empty log entry.
pub fn new() -> Self {
Self
}
/// Returns the serialized size (always 1).
pub const fn log_size() -> usize {
1
}
/// Writes this entry to a buffer (writes a single dummy byte).
pub fn write_to_log(&self, buf: &mut BytesMut) {
buf.put_u8(42); // Arbitrary marker byte
}
/// Reads an entry from a buffer (consumes one byte).
pub fn read_from_log(buf: &[u8]) -> Result<Self, EmptyLogEntryError> {
if buf.is_empty() {
return Err(EmptyLogEntryError::Io(io::Error::new(
io::ErrorKind::UnexpectedEof,
"Empty buffer",
)));
}
// Just consume the byte, value doesn't matter
Ok(Self)
}
}
impl Default for EmptyLogEntry {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_empty_log_entry_roundtrip() {
let entry = EmptyLogEntry::new();
let mut buf = BytesMut::new();
entry.write_to_log(&mut buf);
assert_eq!(buf.len(), 1);
let decoded = EmptyLogEntry::read_from_log(&buf).unwrap();
assert_eq!(entry, decoded);
}
#[test]
fn test_log_size() {
assert_eq!(EmptyLogEntry::log_size(), 1);
}
#[test]
fn test_default() {
let entry = EmptyLogEntry;
assert_eq!(entry, EmptyLogEntry::new());
}
}