|
1 | 1 | //! Platform shared-memory implementation for fspy channels. |
2 | 2 |
|
3 | | -#[cfg(not(any(target_os = "linux", target_os = "windows")))] |
4 | | -use std::io; |
5 | 3 | #[cfg(target_os = "linux")] |
6 | 4 | use std::{future::Future, io, pin::Pin}; |
7 | 5 |
|
8 | 6 | #[cfg(target_os = "linux")] |
9 | 7 | mod linux; |
| 8 | +#[cfg(target_os = "macos")] |
| 9 | +mod macos; |
10 | 10 | #[cfg(target_os = "windows")] |
11 | 11 | mod windows; |
12 | 12 |
|
13 | 13 | #[cfg(target_os = "linux")] |
14 | 14 | pub use linux::{CreatedShm, Shm, create, open}; |
15 | | -#[cfg(not(any(target_os = "linux", target_os = "windows")))] |
16 | | -use shared_memory::{Shmem, ShmemConf}; |
| 15 | +#[cfg(target_os = "macos")] |
| 16 | +pub use macos::{CreatedShm, Shm, create, open}; |
17 | 17 | #[cfg(target_os = "windows")] |
18 | 18 | pub use windows::{CreatedShm, Shm, create, open}; |
19 | 19 |
|
| 20 | +#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))] |
| 21 | +compile_error!("fspy_shm does not support this platform"); |
| 22 | + |
20 | 23 | /// A Linux service future that makes a shared-memory mapping available to other processes. |
21 | 24 | #[cfg(target_os = "linux")] |
22 | 25 | pub type ShmBroker = Pin<Box<dyn Future<Output = io::Result<()>> + Send + 'static>>; |
23 | 26 |
|
24 | | -/// An owned shared-memory mapping. |
25 | | -#[cfg(not(any(target_os = "linux", target_os = "windows")))] |
26 | | -pub struct Shm { |
27 | | - inner: Shmem, |
28 | | -} |
29 | | - |
30 | | -/// A newly created shared-memory mapping and its platform service. |
31 | | -#[cfg(not(any(target_os = "linux", target_os = "windows")))] |
32 | | -pub struct CreatedShm { |
33 | | - /// The owned shared-memory mapping. |
34 | | - pub shm: Shm, |
35 | | -} |
36 | | - |
37 | | -/// Creates a shared-memory mapping of `size` bytes. |
38 | | -/// |
39 | | -/// # Errors |
40 | | -/// |
41 | | -/// Returns an error if the platform cannot create or map the region. |
42 | | -#[cfg(not(any(target_os = "linux", target_os = "windows")))] |
43 | | -pub fn create(size: usize) -> io::Result<CreatedShm> { |
44 | | - let conf = ShmemConf::new().size(size); |
45 | | - |
46 | | - let inner = conf.create().map_err(io::Error::other)?; |
47 | | - Ok(CreatedShm { shm: Shm { inner } }) |
48 | | -} |
49 | | - |
50 | | -/// Opens the shared-memory mapping identified by `id`. |
51 | | -/// |
52 | | -/// # Errors |
53 | | -/// |
54 | | -/// Returns an error if the mapping does not exist or cannot be mapped. |
55 | | -#[cfg(not(any(target_os = "linux", target_os = "windows")))] |
56 | | -pub fn open(id: &str, size: usize) -> io::Result<Shm> { |
57 | | - let conf = ShmemConf::new().size(size).os_id(id); |
58 | | - |
59 | | - let inner = conf.open().map_err(io::Error::other)?; |
60 | | - Ok(Shm { inner }) |
61 | | -} |
62 | | - |
63 | | -#[cfg(not(any(target_os = "linux", target_os = "windows")))] |
64 | | -#[expect(clippy::len_without_is_empty, reason = "shared-memory mappings are always non-empty")] |
65 | | -impl Shm { |
66 | | - /// Returns this mapping's opaque platform identifier. |
67 | | - #[must_use] |
68 | | - pub fn id(&self) -> &str { |
69 | | - self.inner.get_os_id() |
70 | | - } |
71 | | - |
72 | | - /// Returns the mapped length in bytes. |
73 | | - #[must_use] |
74 | | - pub fn len(&self) -> usize { |
75 | | - self.inner.len() |
76 | | - } |
77 | | - |
78 | | - /// Returns a raw pointer to the first mapped byte. |
79 | | - #[must_use] |
80 | | - pub fn as_ptr(&self) -> *mut u8 { |
81 | | - self.inner.as_ptr() |
82 | | - } |
83 | | - |
84 | | - /// Returns the mapped bytes as a shared slice. |
85 | | - /// |
86 | | - /// # Safety |
87 | | - /// |
88 | | - /// The caller must ensure that no process or thread mutates the mapping for |
89 | | - /// the lifetime of the returned slice. |
90 | | - #[must_use] |
91 | | - pub unsafe fn as_slice(&self) -> &[u8] { |
92 | | - // SAFETY: The caller upholds the same synchronization contract required by `Shmem`. |
93 | | - unsafe { self.inner.as_slice() } |
94 | | - } |
95 | | -} |
96 | | - |
97 | 27 | #[cfg(test)] |
98 | 28 | mod tests { |
99 | 29 | use std::{mem::align_of, process::Command}; |
|
0 commit comments