microsoft / windows-rs

Rust for Windows
https://kennykerr.ca/rust-getting-started/
Apache License 2.0
10.13k stars 473 forks source link

Allow windows, windows_core (and several other crates) to be compiled and used in no_std environments. #3047

Closed sivadeilra closed 1 month ago

sivadeilra commented 1 month ago

This allows many of the crates in this repo to be compiled and used in #![no_std] environments. This is a requirement for some low-level components, especially within the implementation of Windows itself.

These crates are updated: windows, windows-core, windows-registry, windows-result. All of these crates now define a new feature, std, which is enabled by default. If you disable the std feature, then the crate will be compiled with #![no_std]. Because the std features are enabled by default, existing users of these crates should not see any difference in behavior.

Nearly all of the changes are extremely simple and do not change actual codegen. These just convert std::ptr::null_mut with core::ptr::null_mut, etc.

For those cases where a crate used dynamic memory containers (Box, String, Vec), I added an explicit dependency on the alloc crate and imported the needed types. These are the same types that are re-exported from std, so this also has no effect on codegen.

In a few cases, code was using things from std that are currently only available in std, such as OsString or Mutex. In those cases, I placed the code using those items under #[cfg(feature = "std")]. Since the std feature is enabled by default, this will not break any existing users of those crates.

I also noticed a bug in passing in BasicString. It can construct a &[u16] over a null pointer, which is UB. I've fixed that bug in this PR.