Skip to content

Commit

Permalink
COM rework
Browse files Browse the repository at this point in the history
  • Loading branch information
kaivol committed Sep 14, 2023
1 parent ca4be38 commit 6c3f0ca
Show file tree
Hide file tree
Showing 359 changed files with 230,839 additions and 311,217 deletions.
1 change: 1 addition & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
>> $env:GITHUB_ENV
- name: Run cargo clippy
run: |
cargo clippy -p miri_implement &&
cargo clippy -p riddle &&
cargo clippy -p sample_bits &&
cargo clippy -p sample_com_uri &&
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/miri.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: miri

on:
pull_request:
push:
branches:
- master

env:
RUSTFLAGS: -Dwarnings

jobs:
miri:
name: miri
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3
- name: Update toolchain
run: rustup update --no-self-update nightly && rustup default nightly
- name: Install miri
run: rustup component add miri
- name: Run miri
run: cargo miri test -p miri_implement
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
uses: ./.github/actions/fix-environment
- name: Test
run: >
cargo test -p miri_implement &&
cargo test -p riddle &&
cargo test -p sample_bits &&
cargo test -p sample_com_uri &&
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows-sys.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
name: Check
strategy:
matrix:
rust: [1.56.0, stable, nightly]
rust: [1.57.0, stable, nightly]
runs-on:
- windows-latest
- ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
name: Check
strategy:
matrix:
rust: [1.56.0, stable, nightly]
rust: [1.57.0, stable, nightly]
runs-on:
- windows-2019
- ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
resolver = "2"
members = [
"crates/libs/*",
"crates/miri/*",
"crates/samples/windows-sys/*",
"crates/samples/windows/*",
"crates/targets/*",
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "windows-bindgen"
version = "0.52.0"
authors = ["Microsoft"]
edition = "2021"
rust-version = "1.56"
rust-version = "1.57"
license = "MIT OR Apache-2.0"
description = "Windows metadata compiler"
repository = "https://github.com/microsoft/windows-rs"
Expand Down
8 changes: 4 additions & 4 deletions crates/libs/bindgen/src/rust/com_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub fn gen_upcall(writer: &Writer, sig: &Signature, inner: TokenStream) -> Token
let result = writer.param_name(sig.params[sig.params.len() - 1].def);

quote! {
match #inner(#(#invoke_args,)*) {
match #inner(this, #(#invoke_args,)*) {
::core::result::Result::Ok(ok__) => {
// use `core::ptr::write` since the result could be uninitialized
::core::ptr::write(#result, ::core::mem::transmute(ok__));
Expand All @@ -174,21 +174,21 @@ pub fn gen_upcall(writer: &Writer, sig: &Signature, inner: TokenStream) -> Token
let invoke_args = sig.params.iter().map(|param| gen_win32_invoke_arg(writer, param));

quote! {
#inner(#(#invoke_args,)*).into()
#inner(this, #(#invoke_args,)*).into()
}
}
SignatureKind::ReturnStruct => {
let invoke_args = sig.params.iter().map(|param| gen_win32_invoke_arg(writer, param));

quote! {
*result__ = #inner(#(#invoke_args,)*)
*result__ = #inner(this, #(#invoke_args,)*)
}
}
_ => {
let invoke_args = sig.params.iter().map(|param| gen_win32_invoke_arg(writer, param));

quote! {
#inner(#(#invoke_args,)*)
#inner(this, #(#invoke_args,)*)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/bindgen/src/rust/delegates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn gen_win_delegate(writer: &Writer, def: TypeDef) -> TokenStream {

let vtbl_signature = writer.vtbl_signature(def, generics, &signature);
let invoke = winrt_methods::writer(writer, def, generics, InterfaceKind::Default, method, &mut MethodNames::new(), &mut MethodNames::new());
let invoke_upcall = winrt_methods::gen_upcall(writer, &signature, quote! { ((*this).invoke) });
let invoke_upcall = winrt_methods::gen_upcall(writer, &signature, quote! { ((*this).invoke) }, None);

let mut tokens = quote! {
#doc
Expand Down Expand Up @@ -150,7 +150,7 @@ fn gen_win_delegate(writer: &Writer, def: TypeDef) -> TokenStream {
}

fn gen_fn_constraint(writer: &Writer, def: TypeDef, signature: &Signature) -> TokenStream {
let signature = writer.impl_signature(def, signature);
let signature = writer.impl_signature(def, signature, true);

quote! { F: FnMut #signature + ::core::marker::Send + 'static }
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,9 @@ where
T: ::windows_core::RuntimeType,
<T as ::windows_core::Type<T>>::Default: ::std::clone::Clone,
{
fn First(&self) -> ::windows_core::Result<IIterator<T>> {
unsafe {
// TODO: ideally we can do an AddRef rather than a QI here (via cast)...
// and then we can get rid of the unsafe as well.
Ok(StockIterator {
owner: self.cast()?,
current: 0.into(),
}
.into())
}
fn First(this: &Self::This) -> ::windows_core::Result<IIterator<T>> {
use windows_core::ComObjectImplExt;
Ok(StockIterator { owner: this.clone(), current: 0.into() }.into_interface())
}
}

Expand All @@ -31,7 +24,7 @@ where
T: ::windows_core::RuntimeType + 'static,
<T as ::windows_core::Type<T>>::Default: ::std::clone::Clone,
{
owner: IIterable<T>,
owner: ::windows_core::ComObject<StockIterable<T>>,
current: ::std::sync::atomic::AtomicUsize,
}

Expand All @@ -40,46 +33,40 @@ where
T: ::windows_core::RuntimeType,
<T as ::windows_core::Type<T>>::Default: ::std::clone::Clone,
{
fn Current(&self) -> ::windows_core::Result<T> {
let owner: &StockIterable<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);
fn Current(this: &Self::This) -> ::windows_core::Result<T> {
let current = this.current.load(::std::sync::atomic::Ordering::Relaxed);

if owner.values.len() > current {
T::from_default(&owner.values[current])
if this.owner.values.len() > current {
T::from_default(&this.owner.values[current])
} else {
Err(::windows_core::Error::from(::windows_core::imp::E_BOUNDS))
}
}

fn HasCurrent(&self) -> ::windows_core::Result<bool> {
let owner: &StockIterable<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);
fn HasCurrent(this: &Self::This) -> ::windows_core::Result<bool> {
let current = this.current.load(::std::sync::atomic::Ordering::Relaxed);

Ok(owner.values.len() > current)
Ok(this.owner.values.len() > current)
}

fn MoveNext(&self) -> ::windows_core::Result<bool> {
let owner: &StockIterable<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);
fn MoveNext(this: &Self::This) -> ::windows_core::Result<bool> {
let current = this.current.load(::std::sync::atomic::Ordering::Relaxed);

if current < owner.values.len() {
self.current
.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed);
if current < this.owner.values.len() {
this.current.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed);
}

Ok(owner.values.len() > current + 1)
Ok(this.owner.values.len() > current + 1)
}

fn GetMany(&self, values: &mut [T::Default]) -> ::windows_core::Result<u32> {
let owner: &StockIterable<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);
fn GetMany(this: &Self::This, values: &mut [T::Default]) -> ::windows_core::Result<u32> {
let current = this.current.load(::std::sync::atomic::Ordering::Relaxed);

let actual = std::cmp::min(owner.values.len() - current, values.len());
let actual = std::cmp::min(this.owner.values.len() - current, values.len());
let (values, _) = values.split_at_mut(actual);
values.clone_from_slice(&owner.values[current..current + actual]);
self.current
.fetch_add(actual, ::std::sync::atomic::Ordering::Relaxed);
Ok(actual as u32)
values.clone_from_slice(&this.owner.values[current..current + actual]);
this.current.fetch_add(actual, ::std::sync::atomic::Ordering::Relaxed);
Ok(actual as _)
}
}

Expand All @@ -91,6 +78,7 @@ where
type Error = ::windows_core::Error;
fn try_from(values: ::std::vec::Vec<T::Default>) -> ::windows_core::Result<Self> {
// TODO: should provide a fallible try_into or more explicit allocator
Ok(StockIterable { values }.into())
use windows_core::ComObjectImplExt;
Ok(StockIterable::<T> { values }.into_interface())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ where
<K as ::windows_core::Type<K>>::Default: std::clone::Clone + std::cmp::Ord,
<V as ::windows_core::Type<V>>::Default: std::clone::Clone,
{
fn First(&self) -> ::windows_core::Result<IIterator<IKeyValuePair<K, V>>> {
unsafe {
// TODO: ideally we can do an AddRef rather than a QI here (via cast)...
// and then we can get rid of the unsafe as well.
Ok(StockMapViewIterator::<K, V> {
_owner: self.cast()?,
current: std::sync::RwLock::new(self.map.iter()),
}
.into())
}
fn First(this: &Self::This) -> ::windows_core::Result<IIterator<IKeyValuePair<K, V>>> {
use windows_core::ComObjectImplExt;
Ok(StockMapViewIterator::<K, V> {
_owner: this.clone(),
current: std::sync::RwLock::new(this.map.iter())
}.into_interface())
}
}

Expand All @@ -36,24 +32,17 @@ where
<K as ::windows_core::Type<K>>::Default: std::clone::Clone + std::cmp::Ord,
<V as ::windows_core::Type<V>>::Default: std::clone::Clone,
{
fn Lookup(&self, key: &K::Default) -> ::windows_core::Result<V> {
let value = self
.map
.get(key)
.ok_or_else(|| ::windows_core::Error::from(::windows_core::imp::E_BOUNDS))?;
fn Lookup(this: &Self::This, key: &K::Default) -> ::windows_core::Result<V> {
let value = this.map.get(key).ok_or_else(|| ::windows_core::Error::from(::windows_core::imp::E_BOUNDS))?;
V::from_default(value)
}
fn Size(&self) -> ::windows_core::Result<u32> {
Ok(self.map.len() as u32)
fn Size(this: &Self::This) -> ::windows_core::Result<u32> {
Ok(this.map.len() as _)
}
fn HasKey(&self, key: &K::Default) -> ::windows_core::Result<bool> {
Ok(self.map.contains_key(key))
fn HasKey(this: &Self::This, key: &K::Default) -> ::windows_core::Result<bool> {
Ok(this.map.contains_key(key))
}
fn Split(
&self,
first: &mut std::option::Option<IMapView<K, V>>,
second: &mut std::option::Option<IMapView<K, V>>,
) -> ::windows_core::Result<()> {
fn Split(_: &Self::This, first: &mut std::option::Option<IMapView<K, V>>, second: &mut std::option::Option<IMapView<K, V>>) -> ::windows_core::Result<()> {
*first = None;
*second = None;
Ok(())
Expand All @@ -68,7 +57,7 @@ where
<K as ::windows_core::Type<K>>::Default: std::clone::Clone + std::cmp::Ord,
<V as ::windows_core::Type<V>>::Default: std::clone::Clone,
{
_owner: IIterable<IKeyValuePair<K, V>>,
_owner: ::windows_core::ComObject<StockMapView<K, V>>,
current: ::std::sync::RwLock<std::collections::btree_map::Iter<'a, K::Default, V::Default>>,
}

Expand All @@ -79,53 +68,45 @@ where
<K as ::windows_core::Type<K>>::Default: std::clone::Clone + std::cmp::Ord,
<V as ::windows_core::Type<V>>::Default: std::clone::Clone,
{
fn Current(&self) -> ::windows_core::Result<IKeyValuePair<K, V>> {
let mut current = self.current.read().unwrap().clone().peekable();
fn Current(this: &Self::This) -> ::windows_core::Result<IKeyValuePair<K, V>> {
let mut current = this.current.read().unwrap().clone().peekable();

if let Some((key, value)) = current.peek() {
Ok(StockKeyValuePair {
key: (*key).clone(),
value: (*value).clone(),
}
.into())
use windows_core::ComObjectImplExt;
Ok(StockKeyValuePair::<K, V> { key: (*key).clone(), value: (*value).clone() }.into_interface())
} else {
Err(::windows_core::Error::from(::windows_core::imp::E_BOUNDS))
}
}

fn HasCurrent(&self) -> ::windows_core::Result<bool> {
let mut current = self.current.read().unwrap().clone().peekable();
fn HasCurrent(this: &Self::This) -> ::windows_core::Result<bool> {
let mut current = this.current.read().unwrap().clone().peekable();

Ok(current.peek().is_some())
}

fn MoveNext(&self) -> ::windows_core::Result<bool> {
let mut current = self.current.write().unwrap();
fn MoveNext(this: &Self::This) -> ::windows_core::Result<bool> {
let mut current = this.current.write().unwrap();

current.next();
Ok(current.clone().peekable().peek().is_some())
}

fn GetMany(&self, pairs: &mut [Option<IKeyValuePair<K, V>>]) -> ::windows_core::Result<u32> {
let mut current = self.current.write().unwrap();
fn GetMany(this: &Self::This, pairs: &mut [Option<IKeyValuePair<K, V>>]) -> ::windows_core::Result<u32> {
let mut current = this.current.write().unwrap();
let mut actual = 0;

for pair in pairs {
if let Some((key, value)) = current.next() {
*pair = Some(
StockKeyValuePair {
key: (*key).clone(),
value: (*value).clone(),
}
.into(),
);
use windows_core::ComObjectImplExt;
*pair = Some(StockKeyValuePair::<K,V> { key: (*key).clone(), value: (*value).clone() }.into_interface());
actual += 1;
} else {
break;
}
}

Ok(actual)
Ok(actual as _)
}
}

Expand All @@ -148,27 +129,25 @@ where
<K as ::windows_core::Type<K>>::Default: std::clone::Clone,
<V as ::windows_core::Type<V>>::Default: std::clone::Clone,
{
fn Key(&self) -> ::windows_core::Result<K> {
K::from_default(&self.key)
fn Key(this: &Self::This) -> ::windows_core::Result<K> {
K::from_default(&this.key)
}
fn Value(&self) -> ::windows_core::Result<V> {
V::from_default(&self.value)
fn Value(this: &Self::This) -> ::windows_core::Result<V> {
V::from_default(&this.value)
}
}

impl<K, V> ::core::convert::TryFrom<std::collections::BTreeMap<K::Default, V::Default>>
for IMapView<K, V>
impl<K, V> ::core::convert::TryFrom<std::collections::BTreeMap<K::Default, V::Default>> for IMapView<K, V>
where
K: ::windows_core::RuntimeType,
V: ::windows_core::RuntimeType,
<K as ::windows_core::Type<K>>::Default: std::clone::Clone + std::cmp::Ord,
<V as ::windows_core::Type<V>>::Default: std::clone::Clone,
{
type Error = ::windows_core::Error;
fn try_from(
map: std::collections::BTreeMap<K::Default, V::Default>,
) -> ::windows_core::Result<Self> {
fn try_from(map: std::collections::BTreeMap<K::Default, V::Default>) -> ::windows_core::Result<Self> {
// TODO: should provide a fallible try_into or more explicit allocator
Ok(StockMapView { map }.into())
use windows_core::ComObjectImplExt;
Ok(StockMapView::<K, V> { map }.into_interface())
}
}
Loading

0 comments on commit 6c3f0ca

Please sign in to comment.