reflex/storage/mmap/
config.rs

1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2/// Memory-mapping mode.
3pub enum MmapMode {
4    /// Read-only mapping.
5    ReadOnly,
6    /// Read/write mapping.
7    ReadWrite,
8    /// Copy-on-write mapping (writes do not hit disk).
9    CopyOnWrite,
10}
11
12#[derive(Debug, Clone)]
13/// Options for creating a memory map.
14pub struct MmapConfig {
15    /// Mapping mode.
16    pub mode: MmapMode,
17    /// If true, ask the OS to populate pages eagerly.
18    pub populate: bool,
19    /// Optional file offset (bytes).
20    pub offset: Option<u64>,
21    /// Optional length (bytes).
22    pub len: Option<usize>,
23}
24
25impl Default for MmapConfig {
26    fn default() -> Self {
27        Self {
28            mode: MmapMode::ReadOnly,
29            populate: false,
30            offset: None,
31            len: None,
32        }
33    }
34}
35
36impl MmapConfig {
37    /// Read-only config.
38    pub fn read_only() -> Self {
39        Self {
40            mode: MmapMode::ReadOnly,
41            ..Default::default()
42        }
43    }
44
45    /// Read/write config.
46    pub fn read_write() -> Self {
47        Self {
48            mode: MmapMode::ReadWrite,
49            ..Default::default()
50        }
51    }
52
53    /// Copy-on-write config.
54    pub fn copy_on_write() -> Self {
55        Self {
56            mode: MmapMode::CopyOnWrite,
57            ..Default::default()
58        }
59    }
60
61    /// Enables eager page population.
62    pub fn with_populate(mut self) -> Self {
63        self.populate = true;
64        self
65    }
66
67    /// Sets an offset (bytes).
68    pub fn with_offset(mut self, offset: u64) -> Self {
69        self.offset = Some(offset);
70        self
71    }
72
73    /// Sets a length (bytes).
74    pub fn with_len(mut self, len: usize) -> Self {
75        self.len = Some(len);
76        self
77    }
78}