Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow noexcept methods in a composable hierarchy #3262

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion crates/libs/bindgen/src/rust/winrt_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,16 @@ pub fn writer(
metadata::InterfaceKind::None
| metadata::InterfaceKind::Base
| metadata::InterfaceKind::Overridable => {
let unwrap = if noexcept {
quote! { .unwrap() }
} else {
quote! { ? }
};

quote! {
#features
pub fn #name<#generics>(&self, #params) #return_type_tokens #where_clause {
let this = &windows_core::Interface::cast::<#interface_name>(self)?;
let this = &windows_core::Interface::cast::<#interface_name>(self)#unwrap;
unsafe {
#vcall
}
Expand Down
53 changes: 24 additions & 29 deletions crates/tests/winrt/composable/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,16 @@ windows_core::imp::interface_hierarchy!(
);
windows_core::imp::required_hierarchy!(ContainerVisual, Visual);
impl ContainerVisual {
pub fn Children(&self) -> windows_core::Result<i32> {
pub fn Children(&self) -> i32 {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).Children)(
let hresult__ = (windows_core::Interface::vtable(this).Children)(
windows_core::Interface::as_raw(this),
&mut result__,
)
.map(|| result__)
);
debug_assert!(hresult__.0 == 0);
result__
}
}
pub fn Compositor(&self) -> windows_core::Result<Compositor> {
Expand Down Expand Up @@ -218,26 +219,28 @@ windows_core::imp::interface_hierarchy!(
);
windows_core::imp::required_hierarchy!(SpriteVisual, ContainerVisual, Visual);
impl SpriteVisual {
pub fn Children(&self) -> windows_core::Result<i32> {
let this = &windows_core::Interface::cast::<IContainerVisual>(self)?;
pub fn Children(&self) -> i32 {
let this = &windows_core::Interface::cast::<IContainerVisual>(self).unwrap();
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).Children)(
let hresult__ = (windows_core::Interface::vtable(this).Children)(
windows_core::Interface::as_raw(this),
&mut result__,
)
.map(|| result__)
);
debug_assert!(hresult__.0 == 0);
result__
}
}
pub fn Brush(&self) -> windows_core::Result<i32> {
pub fn Brush(&self) -> i32 {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).Brush)(
let hresult__ = (windows_core::Interface::vtable(this).Brush)(
windows_core::Interface::as_raw(this),
&mut result__,
)
.map(|| result__)
);
debug_assert!(hresult__.0 == 0);
result__
}
}
pub fn Compositor(&self) -> windows_core::Result<Compositor> {
Expand Down Expand Up @@ -351,7 +354,7 @@ impl ICompositor_Vtbl {
}
}
pub trait IContainerVisual_Impl: Sized + windows_core::IUnknownImpl {
fn Children(&self) -> windows_core::Result<i32>;
fn Children(&self) -> i32;
}
impl windows_core::RuntimeName for IContainerVisual {
const NAME: &'static str = "test_composable.IContainerVisual";
Expand All @@ -364,13 +367,9 @@ impl IContainerVisual_Vtbl {
result__: *mut i32,
) -> windows_core::HRESULT {
let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity);
match IContainerVisual_Impl::Children(this) {
Ok(ok__) => {
result__.write(core::mem::transmute_copy(&ok__));
windows_core::HRESULT(0)
}
Err(err) => err.into(),
}
let ok__ = IContainerVisual_Impl::Children(this);
result__.write(core::mem::transmute_copy(&ok__));
windows_core::HRESULT(0)
}
Self {
base__: windows_core::IInspectable_Vtbl::new::<Identity, IContainerVisual, OFFSET>(),
Expand Down Expand Up @@ -398,7 +397,7 @@ impl IContainerVisualFactory_Vtbl {
}
}
pub trait ISpriteVisual_Impl: Sized + windows_core::IUnknownImpl {
fn Brush(&self) -> windows_core::Result<i32>;
fn Brush(&self) -> i32;
}
impl windows_core::RuntimeName for ISpriteVisual {
const NAME: &'static str = "test_composable.ISpriteVisual";
Expand All @@ -410,13 +409,9 @@ impl ISpriteVisual_Vtbl {
result__: *mut i32,
) -> windows_core::HRESULT {
let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity);
match ISpriteVisual_Impl::Brush(this) {
Ok(ok__) => {
result__.write(core::mem::transmute_copy(&ok__));
windows_core::HRESULT(0)
}
Err(err) => err.into(),
}
let ok__ = ISpriteVisual_Impl::Brush(this);
result__.write(core::mem::transmute_copy(&ok__));
windows_core::HRESULT(0)
}
Self {
base__: windows_core::IInspectable_Vtbl::new::<Identity, ISpriteVisual, OFFSET>(),
Expand Down
12 changes: 6 additions & 6 deletions crates/tests/winrt/composable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ impl ContainerVisual {
}

impl bindings::IContainerVisual_Impl for ContainerVisual_Impl {
fn Children(&self) -> Result<i32> {
Ok(self.children)
fn Children(&self) -> i32 {
self.children
}
}

Expand All @@ -75,14 +75,14 @@ impl SpriteVisual {
}

impl bindings::ISpriteVisual_Impl for SpriteVisual_Impl {
fn Brush(&self) -> Result<i32> {
Ok(self.brush)
fn Brush(&self) -> i32 {
self.brush
}
}

impl bindings::IContainerVisual_Impl for SpriteVisual_Impl {
fn Children(&self) -> Result<i32> {
Ok(self.container.children)
fn Children(&self) -> i32 {
self.container.children
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/tests/winrt/composable/src/metadata.idl
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ namespace test_composable

unsealed runtimeclass ContainerVisual : Visual
{
Int32 Children {get;};
[noexcept] Int32 Children {get;};
}

runtimeclass SpriteVisual : ContainerVisual
{
Int32 Brush {get;};
[noexcept] Int32 Brush {get;};
}
}
29 changes: 16 additions & 13 deletions crates/tests/winrt/composable_client/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,16 @@ windows_core::imp::interface_hierarchy!(
);
windows_core::imp::required_hierarchy!(ContainerVisual, Visual);
impl ContainerVisual {
pub fn Children(&self) -> windows_core::Result<i32> {
pub fn Children(&self) -> i32 {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).Children)(
let hresult__ = (windows_core::Interface::vtable(this).Children)(
windows_core::Interface::as_raw(this),
&mut result__,
)
.map(|| result__)
);
debug_assert!(hresult__.0 == 0);
result__
}
}
pub fn Compositor(&self) -> windows_core::Result<Compositor> {
Expand Down Expand Up @@ -218,26 +219,28 @@ windows_core::imp::interface_hierarchy!(
);
windows_core::imp::required_hierarchy!(SpriteVisual, ContainerVisual, Visual);
impl SpriteVisual {
pub fn Children(&self) -> windows_core::Result<i32> {
let this = &windows_core::Interface::cast::<IContainerVisual>(self)?;
pub fn Children(&self) -> i32 {
let this = &windows_core::Interface::cast::<IContainerVisual>(self).unwrap();
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).Children)(
let hresult__ = (windows_core::Interface::vtable(this).Children)(
windows_core::Interface::as_raw(this),
&mut result__,
)
.map(|| result__)
);
debug_assert!(hresult__.0 == 0);
result__
}
}
pub fn Brush(&self) -> windows_core::Result<i32> {
pub fn Brush(&self) -> i32 {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).Brush)(
let hresult__ = (windows_core::Interface::vtable(this).Brush)(
windows_core::Interface::as_raw(this),
&mut result__,
)
.map(|| result__)
);
debug_assert!(hresult__.0 == 0);
result__
}
}
pub fn Compositor(&self) -> windows_core::Result<Compositor> {
Expand Down
6 changes: 3 additions & 3 deletions crates/tests/winrt/composable_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ fn test() -> Result<()> {
let compositor = Compositor::new()?;

let container = compositor.CreateContainerVisual(123)?;
assert_eq!(container.Children()?, 123);
assert_eq!(container.Children(), 123);
assert_eq!(container.Compositor()?, compositor);

let sprite = compositor.CreateSpriteVisual(456)?;
assert_eq!(sprite.Brush()?, 456);
assert_eq!(sprite.Children()?, 456 * 2);
assert_eq!(sprite.Brush(), 456);
assert_eq!(sprite.Children(), 456 * 2);
assert_eq!(sprite.Compositor()?, compositor);

Ok(())
Expand Down