commitlog/
reader.rs

1//! Custom log reading.
2use super::message::{MessageBuf, MessageError};
3use std::fs::File;
4
5/// Trait that allows reading from a slice of the log.
6pub trait LogSliceReader {
7    /// Result type of this reader.
8    type Result: 'static;
9
10    /// Reads the slice of the file containing the message set.
11    ///
12    /// * `file` - The segment file that contains the slice of the log.
13    /// * `file_position` - The offset within the file that starts the slice.
14    /// * `bytes` - Total number of bytes, from the offset, that contains the
15    ///   message set slice.
16    fn read_from(
17        &mut self,
18        file: &File,
19        file_position: u32,
20        bytes: usize,
21    ) -> Result<Self::Result, MessageError>;
22}
23
24#[cfg(unix)]
25#[derive(Default, Copy, Clone)]
26/// Reader of the file segment into memory.
27pub struct MessageBufReader;
28
29impl LogSliceReader for MessageBufReader {
30    type Result = MessageBuf;
31
32    fn read_from(
33        &mut self,
34        file: &File,
35        file_position: u32,
36        bytes: usize,
37    ) -> Result<Self::Result, MessageError> {
38        use std::os::unix::fs::FileExt;
39
40        let mut vec = vec![0; bytes];
41        file.read_at(&mut vec, u64::from(file_position))?;
42        MessageBuf::from_bytes(vec)
43    }
44}