Skip to content

Commit fe06fe8

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 41e4c55 commit fe06fe8

6 files changed

Lines changed: 332 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,9 +7,6 @@ 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 }
@@ -18,6 +15,11 @@ tempfile = { workspace = true }
1815
tokio = { workspace = true, features = ["macros", "net", "rt", "sync", "time"] }
1916
uuid = { workspace = true, features = ["v4"] }
2017

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

crates/fspy_shm/src/lib.rs

Lines changed: 6 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,21 @@
11
//! Platform shared-memory implementation for fspy channels.
22
3-
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
4-
use std::io;
5-
63
#[cfg(target_os = "linux")]
74
mod linux;
5+
#[cfg(target_os = "macos")]
6+
mod macos;
87
#[cfg(target_os = "windows")]
98
mod windows;
109

1110
#[cfg(target_os = "linux")]
1211
pub use linux::{CreatedShm, Shm, ShmBroker, ShmBrokerHandle, create, open};
13-
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
14-
use shared_memory::{Shmem, ShmemConf};
12+
#[cfg(target_os = "macos")]
13+
pub use macos::{CreatedShm, Shm, create, open};
1514
#[cfg(target_os = "windows")]
1615
pub use windows::{CreatedShm, Shm, create, open};
1716

18-
/// An owned shared-memory mapping.
19-
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
20-
pub struct Shm {
21-
inner: Shmem,
22-
}
23-
24-
/// A newly created shared-memory mapping and its platform service.
25-
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
26-
pub struct CreatedShm {
27-
/// The owned shared-memory mapping.
28-
pub shm: Shm,
29-
}
30-
31-
/// Creates a shared-memory mapping of `size` bytes.
32-
///
33-
/// # Errors
34-
///
35-
/// Returns an error if the platform cannot create or map the region.
36-
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
37-
pub fn create(size: usize) -> io::Result<CreatedShm> {
38-
let conf = ShmemConf::new().size(size);
39-
40-
let inner = conf.create().map_err(io::Error::other)?;
41-
Ok(CreatedShm { shm: Shm { inner } })
42-
}
43-
44-
/// Opens the shared-memory mapping identified by `id`.
45-
///
46-
/// # Errors
47-
///
48-
/// Returns an error if the mapping does not exist or cannot be mapped.
49-
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
50-
pub fn open(id: &str, size: usize) -> io::Result<Shm> {
51-
let conf = ShmemConf::new().size(size).os_id(id);
52-
53-
let inner = conf.open().map_err(io::Error::other)?;
54-
Ok(Shm { inner })
55-
}
56-
57-
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
58-
#[expect(clippy::len_without_is_empty, reason = "shared-memory mappings are always non-empty")]
59-
impl Shm {
60-
/// Returns this mapping's opaque platform identifier.
61-
#[must_use]
62-
pub fn id(&self) -> &str {
63-
self.inner.get_os_id()
64-
}
65-
66-
/// Returns the mapped length in bytes.
67-
#[must_use]
68-
pub fn len(&self) -> usize {
69-
self.inner.len()
70-
}
71-
72-
/// Returns a raw pointer to the first mapped byte.
73-
#[must_use]
74-
pub fn as_ptr(&self) -> *mut u8 {
75-
self.inner.as_ptr()
76-
}
77-
78-
/// Returns the mapped bytes as a shared slice.
79-
///
80-
/// # Safety
81-
///
82-
/// The caller must ensure that no process or thread mutates the mapping for
83-
/// the lifetime of the returned slice.
84-
#[must_use]
85-
pub unsafe fn as_slice(&self) -> &[u8] {
86-
// SAFETY: The caller upholds the same synchronization contract required by `Shmem`.
87-
unsafe { self.inner.as_slice() }
88-
}
89-
}
17+
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
18+
compile_error!("fspy_shm does not support this platform");
9019

9120
#[cfg(test)]
9221
mod tests {

0 commit comments

Comments
 (0)