Skip to content

Commit

Permalink
no_std for registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Arlie Davis committed May 17, 2024
1 parent a7786b4 commit b01f5f4
Show file tree
Hide file tree
Showing 20 changed files with 83 additions and 66 deletions.
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/rust/com_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> #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__))
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/rust/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> #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__))
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/rust/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn gen_win_interface(writer: &Writer, def: metadata::TypeDef) -> TokenStream {

tokens.combine(&quote! {
#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) }
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/core/src/com_object.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/core/src/imp/generic_factory.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/core/src/inspectable.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/core/src/interface.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion crates/libs/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
2 changes: 1 addition & 1 deletion crates/libs/metadata/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<T>());
core::ptr::copy_nonoverlapping(self[offset..].as_ptr(), &mut data as *mut T as *mut u8, std::mem::size_of::<T>());
Some(data)
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/libs/registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ readme = "readme.md"
categories = ["os::windows-apis"]
exclude = ["tests"]

[features]
default = []
std = []

[lints]
workspace = true

Expand Down
36 changes: 15 additions & 21 deletions crates/libs/registry/src/key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use core::ptr::{null, null_mut};

/// A registry key.
#[repr(transparent)]
Expand All @@ -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(),
)
};

Expand Down Expand Up @@ -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)?;
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand Down
29 changes: 15 additions & 14 deletions crates/libs/registry/src/key_iterator.rs
Original file line number Diff line number Diff line change
@@ -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<usize>,
range: core::ops::Range<usize>,
name: Vec<u16>,
}

Expand All @@ -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(),
)
};

Expand All @@ -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(),
)
};

Expand Down
9 changes: 8 additions & 1 deletion crates/libs/registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
Learn more about Rust for Windows here: <https://github.com/microsoft/windows-rs>
*/

#![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::*;

Expand Down Expand Up @@ -39,7 +46,7 @@ fn pcwstr<T: AsRef<str>>(value: T) -> Vec<u16> {
value
.as_ref()
.encode_utf16()
.chain(std::iter::once(0))
.chain(core::iter::once(0))
.collect()
}

Expand Down
2 changes: 2 additions & 0 deletions crates/libs/registry/src/value.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::*;

/// A registry value.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Value {
Expand Down
27 changes: 15 additions & 12 deletions crates/libs/registry/src/value_iterator.rs
Original file line number Diff line number Diff line change
@@ -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<usize>,
range: core::ops::Range<usize>,
name: Vec<u16>,
value: Vec<u8>,
}
Expand All @@ -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(),
)
};

Expand Down Expand Up @@ -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,
Expand All @@ -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<u8> does not guarantee alignment for u16
core::slice::from_raw_parts(
self.value.as_ptr() as *const u16,
value_len as usize / 2,
)
Expand All @@ -93,7 +95,8 @@ impl<'a> Iterator for ValueIterator<'a> {
Value::MultiString(vec![])
} else {
let value = unsafe {
std::slice::from_raw_parts(
// TODO: Vec<u8> does not guarantee alignment for u16
core::slice::from_raw_parts(
self.value.as_ptr() as *const u16,
value_len as usize / 2,
)
Expand Down
4 changes: 4 additions & 0 deletions crates/tests/no_std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions crates/tests/no_std/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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]

Expand Down
6 changes: 3 additions & 3 deletions crates/tests/standalone/src/b_calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,7 @@ pub struct ICalendarFactory2_Vtbl {
pub struct IIterable<T>(windows_core::IUnknown, core::marker::PhantomData<T>)
where
T: windows_core::RuntimeType + 'static;
impl<T: windows_core::RuntimeType + 'static> std::ops::Deref for IIterable<T> {
impl<T: windows_core::RuntimeType + 'static> core::ops::Deref for IIterable<T> {
type Target = windows_core::IInspectable;
fn deref(&self) -> &Self::Target {
unsafe { core::mem::transmute(self) }
Expand Down Expand Up @@ -1702,7 +1702,7 @@ where
pub struct IIterator<T>(windows_core::IUnknown, core::marker::PhantomData<T>)
where
T: windows_core::RuntimeType + 'static;
impl<T: windows_core::RuntimeType + 'static> std::ops::Deref for IIterator<T> {
impl<T: windows_core::RuntimeType + 'static> core::ops::Deref for IIterator<T> {
type Target = windows_core::IInspectable;
fn deref(&self) -> &Self::Target {
unsafe { core::mem::transmute(self) }
Expand Down Expand Up @@ -1849,7 +1849,7 @@ pub struct ITimeZoneOnCalendar_Vtbl {
pub struct IVectorView<T>(windows_core::IUnknown, core::marker::PhantomData<T>)
where
T: windows_core::RuntimeType + 'static;
impl<T: windows_core::RuntimeType + 'static> std::ops::Deref for IVectorView<T> {
impl<T: windows_core::RuntimeType + 'static> core::ops::Deref for IVectorView<T> {
type Target = windows_core::IInspectable;
fn deref(&self) -> &Self::Target {
unsafe { core::mem::transmute(self) }
Expand Down
2 changes: 1 addition & 1 deletion crates/tests/standalone/src/b_stringable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
Loading

0 comments on commit b01f5f4

Please sign in to comment.