Skip to content

Commit 3b57093

Browse files
wan9chiGPT-5.6
andcommitted
refactor(fspy): own macOS shared-memory mapping
Co-authored-by: GPT-5.6 <gpt-5.6@openai.com>
1 parent daf99c3 commit 3b57093

6 files changed

Lines changed: 333 additions & 195 deletions

File tree

Cargo.lock

Lines changed: 8 additions & 112 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ serde = "1.0.219"
122122
serde_json = "1.0.140"
123123
serde_norway = "0.9.42"
124124
sha2 = "0.11.0"
125-
shared_memory = "0.12.4"
126125
shell-escape = "0.1.5"
127126
similar = "3.0.0"
128127
smallvec = { version = "2.0.0-alpha.12", features = ["std"] }

crates/fspy_shared/src/ipc/channel/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl Deref for Sender {
140140
}
141141

142142
#[cfg_attr(
143-
not(target_os = "linux"),
143+
target_os = "windows",
144144
expect(
145145
clippy::non_send_fields_in_send_ty,
146146
reason = "`Sender` holds a shared file lock that ensures there's no reader, so `shm` can be safely written to"
@@ -161,7 +161,7 @@ pub struct Receiver {
161161
}
162162

163163
#[cfg_attr(
164-
not(target_os = "linux"),
164+
target_os = "windows",
165165
expect(
166166
clippy::non_send_fields_in_send_ty,
167167
reason = "Receiver doesn't read or write `shm`. It only passes it to `ReceiverLockGuard` under the lock"

crates/fspy_shm/Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ license.workspace = true
77
publish = false
88
rust-version.workspace = true
99

10-
[target.'cfg(not(any(target_os = "linux", target_os = "windows")))'.dependencies]
11-
shared_memory = { workspace = true, features = ["logging"] }
12-
1310
[target.'cfg(target_os = "linux")'.dependencies]
1411
base64 = { workspace = true }
1512
memmap2 = { workspace = true }
1613
rustix = { workspace = true, features = ["fs", "net"] }
1714
tokio = { workspace = true, features = ["macros", "net", "rt", "time"] }
1815
uuid = { workspace = true, features = ["v4"] }
1916

17+
[target.'cfg(target_os = "macos")'.dependencies]
18+
memmap2 = { workspace = true }
19+
rustix = { workspace = true, features = ["fs", "param", "shm"] }
20+
uuid = { workspace = true, features = ["v4"] }
21+
2022
[target.'cfg(target_os = "windows")'.dependencies]
2123
base64 = { workspace = true }
2224
uuid = { workspace = true, features = ["v4"] }

crates/fspy_shm/src/lib.rs

Lines changed: 7 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,29 @@
11
//! Platform shared-memory implementation for fspy channels.
22
3-
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
4-
use std::io;
53
#[cfg(target_os = "linux")]
64
use std::{future::Future, io, pin::Pin};
75

86
#[cfg(target_os = "linux")]
97
mod linux;
8+
#[cfg(target_os = "macos")]
9+
mod macos;
1010
#[cfg(target_os = "windows")]
1111
mod windows;
1212

1313
#[cfg(target_os = "linux")]
1414
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};
1717
#[cfg(target_os = "windows")]
1818
pub use windows::{CreatedShm, Shm, create, open};
1919

20+
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
21+
compile_error!("fspy_shm does not support this platform");
22+
2023
/// A Linux service future that makes a shared-memory mapping available to other processes.
2124
#[cfg(target_os = "linux")]
2225
pub type ShmBroker = Pin<Box<dyn Future<Output = io::Result<()>> + Send + 'static>>;
2326

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-
9727
#[cfg(test)]
9828
mod tests {
9929
use std::{mem::align_of, process::Command};

0 commit comments

Comments
 (0)