Skip to content

Commit bb8e877

Browse files
committed
Disable all (implicit) uses of OsStr(ing)Ext in HSTRING and PCWSTR for WSL/Linux support
On non-Windows targets - such as Linux and WSL - the `std::os::windows::ffi::OsStrExt` and `std::os::windows::ffi::OsStringExt` extension traits are not available. To support compiling and running _on_ such non-Windows targets (not just cross-compiling _from_ them, for a Windows target) anything using these traits has to be guarded in the same way using a `#[cfg(windows)]`. If needed fallbacks can be implemented manually, but that likely requires a bit more thought since wide-strings on these platforms typically have [32-bit `wchar_t` sizes, with Windows' 16-bit being the exception]. [32-bit `wchar_t` sizes, with Windows' 16-bit being the exception]: https://en.cppreference.com/w/cpp/language/types#Character%20types:~:text=wchar_t,a%20distinct%20type
1 parent 4c7ae6b commit bb8e877

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

crates/libs/windows/src/core/hstring.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl HSTRING {
4949
}
5050

5151
/// Get the contents of this `HSTRING` as a OsString.
52+
#[cfg(windows)]
5253
pub fn to_os_string(&self) -> std::ffi::OsString {
5354
std::os::windows::ffi::OsStringExt::from_wide(self.as_wide())
5455
}
@@ -168,18 +169,21 @@ impl core::convert::From<&alloc::string::String> for HSTRING {
168169
}
169170
}
170171

172+
#[cfg(windows)]
171173
impl core::convert::From<&std::ffi::OsStr> for HSTRING {
172174
fn from(value: &std::ffi::OsStr) -> Self {
173175
unsafe { Self::from_wide_iter(std::os::windows::ffi::OsStrExt::encode_wide(value), value.len() as u32) }
174176
}
175177
}
176178

179+
#[cfg(windows)]
177180
impl core::convert::From<std::ffi::OsString> for HSTRING {
178181
fn from(value: std::ffi::OsString) -> Self {
179182
value.as_os_str().into()
180183
}
181184
}
182185

186+
#[cfg(windows)]
183187
impl core::convert::From<&std::ffi::OsString> for HSTRING {
184188
fn from(value: &std::ffi::OsString) -> Self {
185189
value.as_os_str().into()
@@ -264,72 +268,84 @@ impl PartialEq<&HSTRING> for alloc::string::String {
264268
}
265269
}
266270

271+
#[cfg(windows)]
267272
impl PartialEq<std::ffi::OsString> for HSTRING {
268273
fn eq(&self, other: &std::ffi::OsString) -> bool {
269274
*self == **other
270275
}
271276
}
272277

278+
#[cfg(windows)]
273279
impl PartialEq<std::ffi::OsString> for &HSTRING {
274280
fn eq(&self, other: &std::ffi::OsString) -> bool {
275281
**self == **other
276282
}
277283
}
278284

285+
#[cfg(windows)]
279286
impl PartialEq<&std::ffi::OsString> for HSTRING {
280287
fn eq(&self, other: &&std::ffi::OsString) -> bool {
281288
*self == ***other
282289
}
283290
}
284291

292+
#[cfg(windows)]
285293
impl PartialEq<std::ffi::OsStr> for HSTRING {
286294
fn eq(&self, other: &std::ffi::OsStr) -> bool {
287295
self.as_wide().iter().copied().eq(std::os::windows::ffi::OsStrExt::encode_wide(other))
288296
}
289297
}
290298

299+
#[cfg(windows)]
291300
impl PartialEq<std::ffi::OsStr> for &HSTRING {
292301
fn eq(&self, other: &std::ffi::OsStr) -> bool {
293302
**self == *other
294303
}
295304
}
296305

306+
#[cfg(windows)]
297307
impl PartialEq<&std::ffi::OsStr> for HSTRING {
298308
fn eq(&self, other: &&std::ffi::OsStr) -> bool {
299309
*self == **other
300310
}
301311
}
302312

313+
#[cfg(windows)]
303314
impl PartialEq<HSTRING> for std::ffi::OsStr {
304315
fn eq(&self, other: &HSTRING) -> bool {
305316
*other == *self
306317
}
307318
}
308319

320+
#[cfg(windows)]
309321
impl PartialEq<HSTRING> for &std::ffi::OsStr {
310322
fn eq(&self, other: &HSTRING) -> bool {
311323
*other == **self
312324
}
313325
}
314326

327+
#[cfg(windows)]
315328
impl PartialEq<&HSTRING> for std::ffi::OsStr {
316329
fn eq(&self, other: &&HSTRING) -> bool {
317330
**other == *self
318331
}
319332
}
320333

334+
#[cfg(windows)]
321335
impl PartialEq<HSTRING> for std::ffi::OsString {
322336
fn eq(&self, other: &HSTRING) -> bool {
323337
*other == **self
324338
}
325339
}
326340

341+
#[cfg(windows)]
327342
impl PartialEq<HSTRING> for &std::ffi::OsString {
328343
fn eq(&self, other: &HSTRING) -> bool {
329344
*other == ***self
330345
}
331346
}
332347

348+
#[cfg(windows)]
333349
impl PartialEq<&HSTRING> for std::ffi::OsString {
334350
fn eq(&self, other: &&HSTRING) -> bool {
335351
**other == **self
@@ -352,12 +368,14 @@ impl core::convert::TryFrom<HSTRING> for alloc::string::String {
352368
}
353369
}
354370

371+
#[cfg(windows)]
355372
impl<'a> core::convert::From<&'a HSTRING> for std::ffi::OsString {
356373
fn from(hstring: &HSTRING) -> Self {
357374
hstring.to_os_string()
358375
}
359376
}
360377

378+
#[cfg(windows)]
361379
impl core::convert::From<HSTRING> for std::ffi::OsString {
362380
fn from(hstring: HSTRING) -> Self {
363381
Self::from(&hstring)

crates/libs/windows/src/core/pcwstr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<'a> IntoParam<'a, PCWSTR> for alloc::string::String {
5858
IntoParam::into_param(self.as_str())
5959
}
6060
}
61-
#[cfg(feature = "alloc")]
61+
#[cfg(all(windows, feature = "alloc"))]
6262
impl<'a> IntoParam<'a, PCWSTR> for &::std::ffi::OsStr {
6363
fn into_param(self) -> Param<'a, PCWSTR> {
6464
use ::std::os::windows::ffi::OsStrExt;

0 commit comments

Comments
 (0)