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
//! Whole entry structure for read operations.
//!
//!
//! This struct packages the log entry header and the log entry contents
//! together for components that need information from both parts.
use crate::entry_header::LogEntryHeader;
/// A complete log entry including both header and payload data.
///
/// Used when reading log entries to provide access to both the metadata
/// (in the header) and the deserialized entry contents.
#[derive(Debug)]
pub struct WholeEntry<T> {
/// The log entry header containing metadata.
header: LogEntryHeader,
/// The deserialized log entry contents.
entry: T,
}
impl<T> WholeEntry<T> {
/// Creates a new WholeEntry from a header and entry.
pub fn new(header: LogEntryHeader, entry: T) -> Self {
WholeEntry { header, entry }
}
/// Returns a reference to the header.
pub fn header(&self) -> &LogEntryHeader {
&self.header
}
/// Returns a reference to the entry.
pub fn entry(&self) -> &T {
&self.entry
}
/// Returns a mutable reference to the entry.
pub fn entry_mut(&mut self) -> &mut T {
&mut self.entry
}
/// Consumes the WholeEntry and returns the header and entry.
pub fn into_parts(self) -> (LogEntryHeader, T) {
(self.header, self.entry)
}
/// Consumes the WholeEntry and returns just the entry.
pub fn into_entry(self) -> T {
self.entry
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::entry_type::LogEntryType;
use crate::provisional::Provisional;
#[test]
fn test_whole_entry() {
let header = LogEntryHeader::new(
LogEntryType::BIN,
100,
Provisional::No,
false,
None,
);
let entry_data = vec![1u8, 2, 3, 4, 5];
let whole = WholeEntry::new(header, entry_data.clone());
assert_eq!(whole.entry(), &entry_data);
assert_eq!(whole.header().item_size(), 100);
let (h, e) = whole.into_parts();
assert_eq!(h.item_size(), 100);
assert_eq!(e, entry_data);
}
}