From 66f185040a366032881c8721583b85ff33943131 Mon Sep 17 00:00:00 2001
From: Kenny Kerr <kenny@kennykerr.ca>
Date: Fri, 28 Jun 2024 14:43:40 -0500
Subject: [PATCH] Clarify support for non-Windows targets (#3135)

---
 crates/libs/core/src/imp/factory_cache.rs |  2 +-
 crates/libs/core/src/imp/mod.rs           | 11 +++--------
 crates/libs/core/src/imp/windows.rs       | 11 +++++++++++
 crates/libs/core/src/lib.rs               | 22 +++-------------------
 crates/libs/core/src/windows.rs           | 22 ++++++++++++++++++++++
 crates/libs/cppwinrt/src/lib.rs           |  2 ++
 crates/libs/registry/src/lib.rs           |  1 +
 crates/libs/version/src/lib.rs            |  1 +
 8 files changed, 44 insertions(+), 28 deletions(-)
 create mode 100644 crates/libs/core/src/imp/windows.rs
 create mode 100644 crates/libs/core/src/windows.rs

diff --git a/crates/libs/core/src/imp/factory_cache.rs b/crates/libs/core/src/imp/factory_cache.rs
index b24cb4bb6c..fd7e5fc287 100644
--- a/crates/libs/core/src/imp/factory_cache.rs
+++ b/crates/libs/core/src/imp/factory_cache.rs
@@ -127,7 +127,7 @@ where
     F: FnMut(crate::PCSTR) -> crate::Result<R>,
 {
     let suffix = b".dll\0";
-    let mut library = vec![0; path.len() + suffix.len()];
+    let mut library = alloc::vec![0; path.len() + suffix.len()];
     while let Some(pos) = path.rfind('.') {
         path = &path[..pos];
         library.truncate(path.len() + suffix.len());
diff --git a/crates/libs/core/src/imp/mod.rs b/crates/libs/core/src/imp/mod.rs
index badd497bd5..81cad6441a 100644
--- a/crates/libs/core/src/imp/mod.rs
+++ b/crates/libs/core/src/imp/mod.rs
@@ -1,21 +1,16 @@
-mod bindings;
+#[cfg(windows)]
+include!("windows.rs");
+
 mod can_into;
 mod com_bindings;
-mod factory_cache;
-mod generic_factory;
 mod ref_count;
 mod sha1;
-mod waiter;
 mod weak_ref_count;
 
-pub use bindings::*;
 pub use can_into::*;
 pub use com_bindings::*;
-pub use factory_cache::*;
-pub use generic_factory::*;
 pub use ref_count::*;
 pub use sha1::*;
-pub use waiter::*;
 pub use weak_ref_count::*;
 
 #[doc(hidden)]
diff --git a/crates/libs/core/src/imp/windows.rs b/crates/libs/core/src/imp/windows.rs
new file mode 100644
index 0000000000..e1a55ceb4d
--- /dev/null
+++ b/crates/libs/core/src/imp/windows.rs
@@ -0,0 +1,11 @@
+mod factory_cache;
+pub use factory_cache::*;
+
+mod generic_factory;
+pub use generic_factory::*;
+
+mod waiter;
+pub use waiter::*;
+
+mod bindings;
+pub use bindings::*;
diff --git a/crates/libs/core/src/lib.rs b/crates/libs/core/src/lib.rs
index 5ac18db1b5..ae2e35f2cd 100644
--- a/crates/libs/core/src/lib.rs
+++ b/crates/libs/core/src/lib.rs
@@ -10,9 +10,11 @@ Learn more about Rust for Windows here: <https://github.com/microsoft/windows-rs
 )]
 #![cfg_attr(all(not(feature = "std")), no_std)]
 
+#[cfg(windows)]
+include!("windows.rs");
+
 extern crate self as windows_core;
 
-#[macro_use]
 extern crate alloc;
 
 use alloc::boxed::Box;
@@ -20,14 +22,9 @@ use alloc::boxed::Box;
 #[doc(hidden)]
 pub mod imp;
 
-mod agile_reference;
-mod array;
 mod as_impl;
 mod com_object;
-#[cfg(feature = "std")]
-mod event;
 mod guid;
-mod handles;
 mod inspectable;
 mod interface;
 mod out_param;
@@ -40,17 +37,11 @@ mod runtime_type;
 mod scoped_interface;
 mod r#type;
 mod unknown;
-mod variant;
 mod weak;
 
-pub use agile_reference::*;
-pub use array::*;
 pub use as_impl::*;
 pub use com_object::*;
-#[cfg(feature = "std")]
-pub use event::*;
 pub use guid::*;
-pub use handles::*;
 pub use inspectable::*;
 pub use interface::*;
 pub use out_param::*;
@@ -63,15 +54,8 @@ pub use runtime_name::*;
 pub use runtime_type::*;
 pub use scoped_interface::*;
 pub use unknown::*;
-pub use variant::*;
 pub use weak::*;
 pub use windows_implement::implement;
 pub use windows_interface::interface;
 pub use windows_result::*;
 pub use windows_strings::*;
-
-/// Attempts to load the factory object for the given WinRT class.
-/// This can be used to access COM interfaces implemented on a Windows Runtime class factory.
-pub fn factory<C: RuntimeName, I: Interface>() -> Result<I> {
-    imp::factory::<C, I>()
-}
diff --git a/crates/libs/core/src/windows.rs b/crates/libs/core/src/windows.rs
new file mode 100644
index 0000000000..9b2bc0938e
--- /dev/null
+++ b/crates/libs/core/src/windows.rs
@@ -0,0 +1,22 @@
+mod agile_reference;
+pub use agile_reference::*;
+
+mod array;
+pub use array::*;
+
+#[cfg(feature = "std")]
+mod event;
+#[cfg(feature = "std")]
+pub use event::*;
+
+mod handles;
+pub use handles::*;
+
+mod variant;
+pub use variant::*;
+
+/// Attempts to load the factory object for the given WinRT class.
+/// This can be used to access COM interfaces implemented on a Windows Runtime class factory.
+pub fn factory<C: RuntimeName, I: Interface>() -> Result<I> {
+    imp::factory::<C, I>()
+}
diff --git a/crates/libs/cppwinrt/src/lib.rs b/crates/libs/cppwinrt/src/lib.rs
index 1819774a38..655ccc2ca3 100644
--- a/crates/libs/cppwinrt/src/lib.rs
+++ b/crates/libs/cppwinrt/src/lib.rs
@@ -2,6 +2,8 @@
 Learn more about Rust for Windows here: <https://github.com/microsoft/windows-rs>
 */
 
+#![cfg(windows)]
+
 const VERSION: &str = "2.0.240405.15";
 
 /// Calls the C++/WinRT compiler with the given arguments.
diff --git a/crates/libs/registry/src/lib.rs b/crates/libs/registry/src/lib.rs
index 5c647e224a..607d77a6fe 100644
--- a/crates/libs/registry/src/lib.rs
+++ b/crates/libs/registry/src/lib.rs
@@ -2,6 +2,7 @@
 Learn more about Rust for Windows here: <https://github.com/microsoft/windows-rs>
 */
 
+#![cfg(windows)]
 #![no_std]
 
 #[macro_use]
diff --git a/crates/libs/version/src/lib.rs b/crates/libs/version/src/lib.rs
index 23ba99ee09..3209fc1d72 100644
--- a/crates/libs/version/src/lib.rs
+++ b/crates/libs/version/src/lib.rs
@@ -2,6 +2,7 @@
 Learn more about Rust for Windows here: <https://github.com/microsoft/windows-rs>
 */
 
+#![cfg(windows)]
 #![cfg_attr(not(test), no_std)]
 
 mod bindings;