Skip to main content

memlink_shm/mmap/
mod.rs

1//! Cross-platform memory-mapped file abstraction with unified MmapSegment enum.
2
3#[cfg(unix)]
4mod unix;
5#[cfg(windows)]
6mod windows;
7
8#[cfg(unix)]
9pub use unix::UnixMmap;
10#[cfg(windows)]
11pub use windows::WindowsMmap;
12
13pub enum MmapSegment {
14    #[cfg(unix)]
15    Unix(UnixMmap),
16
17    #[cfg(windows)]
18    Windows(WindowsMmap),
19}
20
21impl MmapSegment {
22    pub fn create<P: AsRef<std::path::Path>>(path: P, size: usize) -> std::io::Result<Self> {
23        #[cfg(unix)]
24        {
25            UnixMmap::create(path, size).map(MmapSegment::Unix)
26        }
27
28        #[cfg(windows)]
29        {
30            WindowsMmap::create(path, size).map(MmapSegment::Windows)
31        }
32
33        #[cfg(not(any(unix, windows)))]
34        {
35            compile_error!("Unsupported platform. Only Unix (Linux/macOS) and Windows are supported.");
36        }
37    }
38
39    pub fn open<P: AsRef<std::path::Path>>(path: P, size: usize) -> std::io::Result<Self> {
40        #[cfg(unix)]
41        {
42            UnixMmap::open(path, size).map(MmapSegment::Unix)
43        }
44
45        #[cfg(windows)]
46        {
47            WindowsMmap::open(path, size).map(MmapSegment::Windows)
48        }
49
50        #[cfg(not(any(unix, windows)))]
51        {
52            compile_error!("Unsupported platform. Only Unix (Linux/macOS) and Windows are supported.");
53        }
54    }
55
56    pub fn as_slice(&self) -> &[u8] {
57        match self {
58            #[cfg(unix)]
59            MmapSegment::Unix(inner) => inner.as_slice(),
60
61            #[cfg(windows)]
62            MmapSegment::Windows(inner) => inner.as_slice(),
63        }
64    }
65
66    pub fn as_mut_slice(&mut self) -> &mut [u8] {
67        match self {
68            #[cfg(unix)]
69            MmapSegment::Unix(inner) => inner.as_mut_slice(),
70
71            #[cfg(windows)]
72            MmapSegment::Windows(inner) => inner.as_mut_slice(),
73        }
74    }
75
76    pub fn len(&self) -> usize {
77        match self {
78            #[cfg(unix)]
79            MmapSegment::Unix(inner) => inner.len(),
80
81            #[cfg(windows)]
82            MmapSegment::Windows(inner) => inner.len(),
83        }
84    }
85
86    pub fn is_empty(&self) -> bool {
87        match self {
88            #[cfg(unix)]
89            MmapSegment::Unix(inner) => inner.is_empty(),
90
91            #[cfg(windows)]
92            MmapSegment::Windows(inner) => inner.is_empty(),
93        }
94    }
95
96    pub fn flush(&self) -> std::io::Result<()> {
97        match self {
98            #[cfg(unix)]
99            MmapSegment::Unix(inner) => inner.flush(),
100
101            #[cfg(windows)]
102            MmapSegment::Windows(inner) => inner.flush(),
103        }
104    }
105
106    pub fn flush_region(&self, offset: usize, len: usize) -> std::io::Result<()> {
107        match self {
108            #[cfg(unix)]
109            MmapSegment::Unix(inner) => inner.flush_region(offset, len),
110
111            #[cfg(windows)]
112            MmapSegment::Windows(inner) => inner.flush_region(offset, len),
113        }
114    }
115}