Skip to content

Commit e524579

Browse files
committed
implement NativeString for Windows
1 parent 00ca070 commit e524579

3 files changed

Lines changed: 33 additions & 31 deletions

File tree

crates/fspy_shared/src/ipc/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ mod native_str;
33
pub mod shm_io;
44

55
use bincode::{BorrowDecode, Encode, config::Configuration};
6-
pub use native_str::NativeStr;
7-
#[cfg(unix)]
8-
pub use native_str::NativeString;
6+
pub use native_str::{NativeStr, NativeString};
97

108
pub const BINCODE_CONFIG: Configuration = bincode::config::standard();
119

crates/fspy_shared/src/ipc/native_str.rs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
#[cfg(windows)]
22
use std::ffi::OsString;
3-
#[cfg(unix)]
4-
use std::sync::Arc;
53
use std::{
64
borrow::Cow,
75
ffi::OsStr,
86
fmt::Debug,
97
path::{Path, StripPrefixError},
8+
sync::Arc,
109
};
1110

1211
use allocator_api2::alloc::Allocator;
13-
#[cfg(unix)]
14-
use bincode::Decode;
15-
use bincode::{BorrowDecode, Encode};
12+
use bincode::{BorrowDecode, Decode, Encode};
1613
use bstr::BStr;
1714

1815
/// Similar to `OsStr`, but requires zero-copy to construct from either asni or wide characters on Windows.
@@ -161,60 +158,67 @@ impl<'a> Debug for NativeStr<'a> {
161158

162159
/// Similiar to `OsString`, but can be losslessly encoded/decoded using bincode.
163160
/// `Encode`/`Decoded` implementations for `OsString` requires it to be valid UTF-8. This does not.
164-
#[cfg(unix)]
165161
#[derive(Encode, Decode, Clone, Hash)]
166162
pub struct NativeString {
163+
#[cfg(unix)]
167164
data: Arc<[u8]>,
165+
#[cfg(windows)]
166+
data: Arc<[u16]>,
168167
}
169168

170169
impl NativeString {
170+
#[cfg(unix)]
171171
pub fn as_os_str(&self) -> &OsStr {
172172
use std::os::unix::ffi::OsStrExt as _;
173173
OsStr::from_bytes(&self.data)
174174
}
175175

176+
#[cfg(windows)]
177+
pub fn to_os_string(&self) -> OsString {
178+
use std::os::windows::ffi::OsStringExt as _;
179+
OsString::from_wide(&self.data)
180+
}
181+
176182
pub fn to_cow_os_str(&self) -> Cow<'_, OsStr> {
177183
#[cfg(unix)]
178184
return Cow::Borrowed(self.as_os_str());
185+
#[cfg(windows)]
186+
return Cow::Owned(self.to_os_string());
179187
}
180188
}
181189

182-
#[cfg(unix)]
183190
impl<'a> Debug for NativeString {
184191
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
185-
<OsStr as Debug>::fmt(self.as_os_str(), f)
192+
<OsStr as Debug>::fmt(&self.to_cow_os_str(), f)
186193
}
187194
}
188195

189-
#[cfg(unix)]
190196
impl<'a> From<&'a OsStr> for NativeString {
197+
#[cfg(unix)]
191198
fn from(value: &'a OsStr) -> Self {
192-
use std::os::unix::ffi::OsStrExt;
199+
use std::os::unix::ffi::OsStrExt as _;
193200
Self { data: value.as_bytes().into() }
194201
}
195-
}
196-
#[cfg(unix)]
197-
impl<'a> From<String> for NativeString {
198-
fn from(value: String) -> Self {
199-
Self { data: value.as_bytes().into() }
202+
203+
#[cfg(windows)]
204+
fn from(value: &'a OsStr) -> Self {
205+
use std::os::windows::ffi::OsStrExt as _;
206+
Self { data: value.encode_wide().collect() }
200207
}
201208
}
202-
#[cfg(unix)]
209+
// #[cfg(unix)]
210+
// impl<'a> From<String> for NativeString {
211+
// fn from(value: String) -> Self {
212+
// Self { data: value.as_bytes().into() }
213+
// }
214+
// }
215+
203216
impl<'a> From<&'a std::path::Path> for NativeString {
204217
fn from(value: &'a std::path::Path) -> Self {
205218
value.as_os_str().into()
206219
}
207220
}
208221

209-
#[cfg(unix)]
210-
impl std::ops::Deref for NativeString {
211-
type Target = OsStr;
212-
213-
fn deref(&self) -> &Self::Target {
214-
self.as_os_str()
215-
}
216-
}
217-
218222
#[cfg(test)]
219223
mod tests {
220224
#[cfg(windows)]

crates/fspy_shared_unix/src/spawn/macos.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ pub fn handle_exec(
5252
} else if matches!(parent.as_os_str().as_bytes(), b"/bin" | b"/usr/bin") {
5353
let fixtures = &encoded_payload.payload.fixtures;
5454
if matches!(file_name.as_bytes(), b"sh" | b"bash") {
55-
command.program = fixtures.bash_path.as_bytes().into();
55+
command.program = fixtures.bash_path.as_os_str().as_bytes().into();
5656
true
5757
} else if COREUTILS_FUNCTIONS.contains(file_name.as_bytes()) {
58-
command.program = fixtures.coreutils_path.as_bytes().into();
58+
command.program = fixtures.coreutils_path.as_os_str().as_bytes().into();
5959
true
6060
} else {
6161
false
@@ -71,7 +71,7 @@ pub fn handle_exec(
7171
ensure_env(
7272
&mut command.envs,
7373
DYLD_INSERT_LIBRARIES,
74-
encoded_payload.payload.preload_path.as_bytes(),
74+
encoded_payload.payload.preload_path.as_os_str().as_bytes(),
7575
)?;
7676
ensure_env(&mut command.envs, PAYLOAD_ENV_NAME, &encoded_payload.encoded_string)?;
7777
} else {

0 commit comments

Comments
 (0)