From b01f5f4f5174825b16b931f7c57db2dadaafbb9b Mon Sep 17 00:00:00 2001 From: Arlie Davis Date: Fri, 17 May 2024 11:48:49 -0700 Subject: [PATCH] no_std for registry --- crates/libs/bindgen/src/rust/com_methods.rs | 2 +- crates/libs/bindgen/src/rust/functions.rs | 2 +- crates/libs/bindgen/src/rust/interfaces.rs | 2 +- crates/libs/core/src/com_object.rs | 2 +- crates/libs/core/src/imp/generic_factory.rs | 2 +- crates/libs/core/src/inspectable.rs | 2 +- crates/libs/core/src/interface.rs | 2 +- crates/libs/core/src/lib.rs | 1 - crates/libs/metadata/src/file.rs | 2 +- crates/libs/registry/Cargo.toml | 4 +++ crates/libs/registry/src/key.rs | 36 +++++++++------------ crates/libs/registry/src/key_iterator.rs | 29 +++++++++-------- crates/libs/registry/src/lib.rs | 9 +++++- crates/libs/registry/src/value.rs | 2 ++ crates/libs/registry/src/value_iterator.rs | 27 +++++++++------- crates/tests/no_std/Cargo.toml | 4 +++ crates/tests/no_std/src/lib.rs | 3 ++ crates/tests/standalone/src/b_calendar.rs | 6 ++-- crates/tests/standalone/src/b_stringable.rs | 2 +- crates/tests/standalone/src/b_uri.rs | 10 +++--- 20 files changed, 83 insertions(+), 66 deletions(-) diff --git a/crates/libs/bindgen/src/rust/com_methods.rs b/crates/libs/bindgen/src/rust/com_methods.rs index 51202e2908..fa6b3aebed 100644 --- a/crates/libs/bindgen/src/rust/com_methods.rs +++ b/crates/libs/bindgen/src/rust/com_methods.rs @@ -31,7 +31,7 @@ pub fn writer(writer: &Writer, def: metadata::TypeDef, kind: metadata::Interface quote! { #features pub unsafe fn #name<#generics>(&self, #params) -> windows_core::Result #where_clause { - let mut result__ = std::ptr::null_mut(); + let mut result__ = core::ptr::null_mut(); (windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self), #args).and_then(||windows_core::Type::from_abi(result__)) } } diff --git a/crates/libs/bindgen/src/rust/functions.rs b/crates/libs/bindgen/src/rust/functions.rs index 7aada04a32..8af4d6ae33 100644 --- a/crates/libs/bindgen/src/rust/functions.rs +++ b/crates/libs/bindgen/src/rust/functions.rs @@ -57,7 +57,7 @@ fn gen_win_function(writer: &Writer, namespace: &str, def: metadata::MethodDef) #[inline] pub unsafe fn #name<#generics>(#params) -> windows_core::Result #where_clause { #link - let mut result__ = std::ptr::null_mut(); + let mut result__ = core::ptr::null_mut(); #name(#args).and_then(||windows_core::Type::from_abi(result__)) } } diff --git a/crates/libs/bindgen/src/rust/interfaces.rs b/crates/libs/bindgen/src/rust/interfaces.rs index 1cb185b5b1..9f35000980 100644 --- a/crates/libs/bindgen/src/rust/interfaces.rs +++ b/crates/libs/bindgen/src/rust/interfaces.rs @@ -97,7 +97,7 @@ fn gen_win_interface(writer: &Writer, def: metadata::TypeDef) -> TokenStream { tokens.combine("e! { #features - impl<#constraints> std::ops::Deref for #ident { + impl<#constraints> core::ops::Deref for #ident { type Target = #base; fn deref(&self) -> &Self::Target { unsafe { core::mem::transmute(self) } diff --git a/crates/libs/core/src/com_object.rs b/crates/libs/core/src/com_object.rs index 09597844bc..4b2c23960d 100644 --- a/crates/libs/core/src/com_object.rs +++ b/crates/libs/core/src/com_object.rs @@ -1,5 +1,5 @@ -use crate::{AsImpl, IUnknown, IUnknownImpl, Interface, InterfaceRef}; use crate::imp::Box; +use crate::{AsImpl, IUnknown, IUnknownImpl, Interface, InterfaceRef}; use core::borrow::Borrow; use core::ops::Deref; use core::ptr::NonNull; diff --git a/crates/libs/core/src/imp/generic_factory.rs b/crates/libs/core/src/imp/generic_factory.rs index 047940a17d..75e9a58bcc 100644 --- a/crates/libs/core/src/imp/generic_factory.rs +++ b/crates/libs/core/src/imp/generic_factory.rs @@ -1,6 +1,6 @@ use crate::Interface; use core::ffi::c_void; -use core::mem::{zeroed, transmute_copy}; +use core::mem::{transmute_copy, zeroed}; // A streamlined version of the IActivationFactory interface used by WinRT class factories used internally by the windows crate // to simplify code generation. Components should implement the `IActivationFactory` interface published by the windows crate. diff --git a/crates/libs/core/src/inspectable.rs b/crates/libs/core/src/inspectable.rs index d40c821977..bd61b95e1e 100644 --- a/crates/libs/core/src/inspectable.rs +++ b/crates/libs/core/src/inspectable.rs @@ -1,7 +1,7 @@ use super::*; +use core::ffi::c_void; use core::mem::{transmute, transmute_copy}; use core::ptr::null_mut; -use core::ffi::c_void; /// A WinRT object that may be used as a polymorphic stand-in for any WinRT class, interface, or boxed value. /// [`IInspectable`] represents the diff --git a/crates/libs/core/src/interface.rs b/crates/libs/core/src/interface.rs index 4929960918..c10991b9a7 100644 --- a/crates/libs/core/src/interface.rs +++ b/crates/libs/core/src/interface.rs @@ -1,7 +1,7 @@ use super::*; -use core::mem::{transmute_copy, forget}; use core::ffi::c_void; use core::marker::PhantomData; +use core::mem::{forget, transmute_copy}; use core::ptr::NonNull; /// Provides low-level access to an interface vtable. diff --git a/crates/libs/core/src/lib.rs b/crates/libs/core/src/lib.rs index b7fc9cfd13..70d97d9c52 100644 --- a/crates/libs/core/src/lib.rs +++ b/crates/libs/core/src/lib.rs @@ -11,7 +11,6 @@ extern crate self as windows_core; #[macro_use] extern crate alloc; -// use alloc::vec; use alloc::{boxed::Box, string::String, vec::Vec}; diff --git a/crates/libs/metadata/src/file.rs b/crates/libs/metadata/src/file.rs index 1bc4e10328..3838c39a8f 100644 --- a/crates/libs/metadata/src/file.rs +++ b/crates/libs/metadata/src/file.rs @@ -382,7 +382,7 @@ impl View for [u8] { unsafe { let mut data = std::mem::MaybeUninit::zeroed().assume_init(); - std::ptr::copy_nonoverlapping(self[offset..].as_ptr(), &mut data as *mut T as *mut u8, std::mem::size_of::()); + core::ptr::copy_nonoverlapping(self[offset..].as_ptr(), &mut data as *mut T as *mut u8, std::mem::size_of::()); Some(data) } } diff --git a/crates/libs/registry/Cargo.toml b/crates/libs/registry/Cargo.toml index 182b880a90..375d109e8d 100644 --- a/crates/libs/registry/Cargo.toml +++ b/crates/libs/registry/Cargo.toml @@ -11,6 +11,10 @@ readme = "readme.md" categories = ["os::windows-apis"] exclude = ["tests"] +[features] +default = [] +std = [] + [lints] workspace = true diff --git a/crates/libs/registry/src/key.rs b/crates/libs/registry/src/key.rs index d1988220ca..d62e24a05e 100644 --- a/crates/libs/registry/src/key.rs +++ b/crates/libs/registry/src/key.rs @@ -1,4 +1,5 @@ use super::*; +use core::ptr::{null, null_mut}; /// A registry key. #[repr(transparent)] @@ -15,12 +16,12 @@ impl Key { self.0, pcwstr(path).as_ptr(), 0, - std::ptr::null(), + null(), REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, - std::ptr::null(), + null(), &mut handle, - std::ptr::null_mut(), + null_mut(), ) }; @@ -115,14 +116,7 @@ impl Key { let mut len = 0; let result = unsafe { - RegQueryValueExW( - self.0, - name.as_ptr(), - std::ptr::null(), - &mut ty, - std::ptr::null_mut(), - &mut len, - ) + RegQueryValueExW(self.0, name.as_ptr(), null(), &mut ty, null_mut(), &mut len) }; win32_error(result)?; @@ -135,8 +129,8 @@ impl Key { RegQueryValueExW( self.0, name.as_ptr(), - std::ptr::null(), - std::ptr::null_mut(), + null(), + null_mut(), &mut value as *mut _ as _, &mut len, ) @@ -152,8 +146,8 @@ impl Key { RegQueryValueExW( self.0, name.as_ptr(), - std::ptr::null(), - std::ptr::null_mut(), + null(), + null_mut(), &mut value as *mut _ as _, &mut len, ) @@ -169,8 +163,8 @@ impl Key { RegQueryValueExW( self.0, name.as_ptr(), - std::ptr::null(), - std::ptr::null_mut(), + null(), + null_mut(), value.as_mut_ptr() as _, &mut len, ) @@ -186,8 +180,8 @@ impl Key { RegQueryValueExW( self.0, name.as_ptr(), - std::ptr::null(), - std::ptr::null_mut(), + null(), + null_mut(), value.as_mut_ptr() as _, &mut len, ) @@ -209,8 +203,8 @@ impl Key { RegQueryValueExW( self.0, name.as_ptr(), - std::ptr::null(), - std::ptr::null_mut(), + null(), + null_mut(), value.as_mut_ptr() as _, &mut len, ) diff --git a/crates/libs/registry/src/key_iterator.rs b/crates/libs/registry/src/key_iterator.rs index 367218da87..70d728e2b8 100644 --- a/crates/libs/registry/src/key_iterator.rs +++ b/crates/libs/registry/src/key_iterator.rs @@ -1,9 +1,10 @@ use super::*; +use core::ptr::{null, null_mut}; /// An iterator of registry key names. pub struct KeyIterator<'a> { key: &'a Key, - range: std::ops::Range, + range: core::ops::Range, name: Vec, } @@ -15,17 +16,17 @@ impl<'a> KeyIterator<'a> { let result = unsafe { RegQueryInfoKeyW( key.0, - std::ptr::null_mut(), - std::ptr::null_mut(), - std::ptr::null_mut(), + null_mut(), + null_mut(), + null_mut(), &mut count, &mut max_len, - std::ptr::null_mut(), - std::ptr::null_mut(), - std::ptr::null_mut(), - std::ptr::null_mut(), - std::ptr::null_mut(), - std::ptr::null_mut(), + null_mut(), + null_mut(), + null_mut(), + null_mut(), + null_mut(), + null_mut(), ) }; @@ -50,10 +51,10 @@ impl<'a> Iterator for KeyIterator<'a> { index as u32, self.name.as_mut_ptr(), &mut len, - std::ptr::null(), - std::ptr::null_mut(), - std::ptr::null_mut(), - std::ptr::null_mut(), + null(), + null_mut(), + null_mut(), + null_mut(), ) }; diff --git a/crates/libs/registry/src/lib.rs b/crates/libs/registry/src/lib.rs index af9059d15c..aad2cb3a9a 100644 --- a/crates/libs/registry/src/lib.rs +++ b/crates/libs/registry/src/lib.rs @@ -2,6 +2,13 @@ Learn more about Rust for Windows here: */ +#![cfg_attr(all(not(test), not(feature = "std")), no_std)] + +#[macro_use] +extern crate alloc; + +use alloc::{string::String, vec::Vec}; + mod bindings; use bindings::*; @@ -39,7 +46,7 @@ fn pcwstr>(value: T) -> Vec { value .as_ref() .encode_utf16() - .chain(std::iter::once(0)) + .chain(core::iter::once(0)) .collect() } diff --git a/crates/libs/registry/src/value.rs b/crates/libs/registry/src/value.rs index ba25e0cccf..eb196d2463 100644 --- a/crates/libs/registry/src/value.rs +++ b/crates/libs/registry/src/value.rs @@ -1,3 +1,5 @@ +use super::*; + /// A registry value. #[derive(Clone, PartialEq, Eq, Debug)] pub enum Value { diff --git a/crates/libs/registry/src/value_iterator.rs b/crates/libs/registry/src/value_iterator.rs index 1f4b25f5b3..81b4b1afd9 100644 --- a/crates/libs/registry/src/value_iterator.rs +++ b/crates/libs/registry/src/value_iterator.rs @@ -1,9 +1,10 @@ use super::*; +use core::ptr::null_mut; /// An iterator of registry values. pub struct ValueIterator<'a> { key: &'a Key, - range: std::ops::Range, + range: core::ops::Range, name: Vec, value: Vec, } @@ -17,17 +18,17 @@ impl<'a> ValueIterator<'a> { let result = unsafe { RegQueryInfoKeyW( key.0, - std::ptr::null_mut(), - std::ptr::null_mut(), - std::ptr::null_mut(), - std::ptr::null_mut(), - std::ptr::null_mut(), - std::ptr::null_mut(), + null_mut(), + null_mut(), + null_mut(), + null_mut(), + null_mut(), + null_mut(), &mut count, &mut name_max_len, &mut value_max_len, - std::ptr::null_mut(), - std::ptr::null_mut(), + null_mut(), + null_mut(), ) }; @@ -55,7 +56,7 @@ impl<'a> Iterator for ValueIterator<'a> { index as u32, self.name.as_mut_ptr(), &mut name_len, - std::ptr::null(), + core::ptr::null(), &mut ty, self.value.as_mut_ptr(), &mut value_len, @@ -79,7 +80,8 @@ impl<'a> Iterator for ValueIterator<'a> { Value::String(String::new()) } else { let value = unsafe { - std::slice::from_raw_parts( + // TODO: Vec does not guarantee alignment for u16 + core::slice::from_raw_parts( self.value.as_ptr() as *const u16, value_len as usize / 2, ) @@ -93,7 +95,8 @@ impl<'a> Iterator for ValueIterator<'a> { Value::MultiString(vec![]) } else { let value = unsafe { - std::slice::from_raw_parts( + // TODO: Vec does not guarantee alignment for u16 + core::slice::from_raw_parts( self.value.as_ptr() as *const u16, value_len as usize / 2, ) diff --git a/crates/tests/no_std/Cargo.toml b/crates/tests/no_std/Cargo.toml index 1b2fa98456..8c84e170c8 100644 --- a/crates/tests/no_std/Cargo.toml +++ b/crates/tests/no_std/Cargo.toml @@ -12,6 +12,10 @@ path = "../../libs/windows" default-features = false features = ["implement"] +[dependencies.windows-registry] +path = "../../libs/registry" +default-features = false + [dependencies.windows-result] path = "../../libs/result" default-features = false diff --git a/crates/tests/no_std/src/lib.rs b/crates/tests/no_std/src/lib.rs index fcb9210040..b105cd9faf 100644 --- a/crates/tests/no_std/src/lib.rs +++ b/crates/tests/no_std/src/lib.rs @@ -1,4 +1,7 @@ //! Test for `#![no_std]` crates. +//! +//! Compiling this crate verifies that the Windows crates can be compiled with their "std" +//! feature disabled. #![no_std] diff --git a/crates/tests/standalone/src/b_calendar.rs b/crates/tests/standalone/src/b_calendar.rs index 37ad502d92..2299a6181d 100644 --- a/crates/tests/standalone/src/b_calendar.rs +++ b/crates/tests/standalone/src/b_calendar.rs @@ -1629,7 +1629,7 @@ pub struct ICalendarFactory2_Vtbl { pub struct IIterable(windows_core::IUnknown, core::marker::PhantomData) where T: windows_core::RuntimeType + 'static; -impl std::ops::Deref for IIterable { +impl core::ops::Deref for IIterable { type Target = windows_core::IInspectable; fn deref(&self) -> &Self::Target { unsafe { core::mem::transmute(self) } @@ -1702,7 +1702,7 @@ where pub struct IIterator(windows_core::IUnknown, core::marker::PhantomData) where T: windows_core::RuntimeType + 'static; -impl std::ops::Deref for IIterator { +impl core::ops::Deref for IIterator { type Target = windows_core::IInspectable; fn deref(&self) -> &Self::Target { unsafe { core::mem::transmute(self) } @@ -1849,7 +1849,7 @@ pub struct ITimeZoneOnCalendar_Vtbl { pub struct IVectorView(windows_core::IUnknown, core::marker::PhantomData) where T: windows_core::RuntimeType + 'static; -impl std::ops::Deref for IVectorView { +impl core::ops::Deref for IVectorView { type Target = windows_core::IInspectable; fn deref(&self) -> &Self::Target { unsafe { core::mem::transmute(self) } diff --git a/crates/tests/standalone/src/b_stringable.rs b/crates/tests/standalone/src/b_stringable.rs index 9f41423ea0..6e1c9abe27 100644 --- a/crates/tests/standalone/src/b_stringable.rs +++ b/crates/tests/standalone/src/b_stringable.rs @@ -10,7 +10,7 @@ windows_core::imp::define_interface!( IStringable_Vtbl, 0x96369f54_8eb6_48f0_abce_c1b211e627c3 ); -impl std::ops::Deref for IStringable { +impl core::ops::Deref for IStringable { type Target = windows_core::IInspectable; fn deref(&self) -> &Self::Target { unsafe { core::mem::transmute(self) } diff --git a/crates/tests/standalone/src/b_uri.rs b/crates/tests/standalone/src/b_uri.rs index 3745ecc965..a7db2d2d02 100644 --- a/crates/tests/standalone/src/b_uri.rs +++ b/crates/tests/standalone/src/b_uri.rs @@ -10,7 +10,7 @@ pub struct IIterable(windows_core::IUnknown, core::marker::PhantomData) where T: windows_core::RuntimeType + 'static; -impl std::ops::Deref for IIterable { +impl core::ops::Deref for IIterable { type Target = windows_core::IInspectable; fn deref(&self) -> &Self::Target { unsafe { core::mem::transmute(self) } @@ -83,7 +83,7 @@ where pub struct IIterator(windows_core::IUnknown, core::marker::PhantomData) where T: windows_core::RuntimeType + 'static; -impl std::ops::Deref for IIterator { +impl core::ops::Deref for IIterator { type Target = windows_core::IInspectable; fn deref(&self) -> &Self::Target { unsafe { core::mem::transmute(self) } @@ -200,7 +200,7 @@ windows_core::imp::define_interface!( IStringable_Vtbl, 0x96369f54_8eb6_48f0_abce_c1b211e627c3 ); -impl std::ops::Deref for IStringable { +impl core::ops::Deref for IStringable { type Target = windows_core::IInspectable; fn deref(&self) -> &Self::Target { unsafe { core::mem::transmute(self) } @@ -387,7 +387,7 @@ pub struct IUriRuntimeClassWithAbsoluteCanonicalUri_Vtbl { pub struct IVectorView(windows_core::IUnknown, core::marker::PhantomData) where T: windows_core::RuntimeType + 'static; -impl std::ops::Deref for IVectorView { +impl core::ops::Deref for IVectorView { type Target = windows_core::IInspectable; fn deref(&self) -> &Self::Target { unsafe { core::mem::transmute(self) } @@ -558,7 +558,7 @@ windows_core::imp::define_interface!( IWwwFormUrlDecoderEntry_Vtbl, 0x125e7431_f678_4e8e_b670_20a9b06c512d ); -impl std::ops::Deref for IWwwFormUrlDecoderEntry { +impl core::ops::Deref for IWwwFormUrlDecoderEntry { type Target = windows_core::IInspectable; fn deref(&self) -> &Self::Target { unsafe { core::mem::transmute(self) }