Skip to content

Commit

Permalink
Share result mapping code in windows-bindgen and reduce transmute
Browse files Browse the repository at this point in the history
… count (#3454)
  • Loading branch information
kennykerr authored Jan 17, 2025
1 parent 7172cec commit bfe7c64
Show file tree
Hide file tree
Showing 238 changed files with 2,262 additions and 2,273 deletions.
8 changes: 1 addition & 7 deletions crates/libs/bindgen/src/types/cpp_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,7 @@ impl CppFn {
ReturnHint::ResultValue => {
let where_clause = method.write_where(writer, false);
let return_type = signature.params[signature.params.len() - 1].0.deref();

let map = if !return_type.is_interface() {
quote! { map(||core::mem::transmute(result__)) }
} else {
quote! { and_then(||windows_core::Type::from_abi(result__)) }
};

let map = return_type.write_result_map();
let return_type = return_type.write_name(writer);

quote! {
Expand Down
9 changes: 1 addition & 8 deletions crates/libs/bindgen/src/types/cpp_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,7 @@ impl CppMethod {
.0
.deref();

let map = if return_type.is_copyable() {
quote! { map(||result__) }
} else if return_type.is_interface() {
quote! { and_then(||windows_core::Type::from_abi(result__)) }
} else {
quote! { map(||core::mem::transmute(result__)) }
};

let map = return_type.write_result_map();
let return_type = return_type.write_name(writer);

quote! {
Expand Down
36 changes: 15 additions & 21 deletions crates/libs/bindgen/src/types/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,32 +489,26 @@ impl Method {
if noexcept {
if self.signature.return_type.0.is_copyable() {
quote! {
let mut result__ = core::mem::zeroed();
let hresult__ = #vcall;
debug_assert!(hresult__.0 == 0);
result__ }
let mut result__ = core::mem::zeroed();
let hresult__ = #vcall;
debug_assert!(hresult__.0 == 0);
result__
}
} else {
quote! {
let mut result__ = core::mem::zeroed();
let hresult__ = #vcall;
debug_assert!(hresult__.0 == 0);
core::mem::transmute(result__) }
let mut result__ = core::mem::zeroed();
let hresult__ = #vcall;
debug_assert!(hresult__.0 == 0);
core::mem::transmute(result__)
}
}
} else if !self.signature.return_type.0.is_convertible() {
if self.signature.return_type.0.is_primitive() {
quote! {
let mut result__ = core::mem::zeroed();
#vcall
.map(||result__) }
} else {
quote! {
} else {
let map = self.signature.return_type.0.write_result_map();

quote! {
let mut result__ = core::mem::zeroed();
#vcall
.map(||core::mem::transmute(result__)) }
#vcall.#map
}
} else {
quote! { let mut result__ = core::mem::zeroed();
#vcall.and_then(|| windows_core::Type::from_abi(result__)) }
}
}
};
Expand Down
12 changes: 10 additions & 2 deletions crates/libs/bindgen/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,9 +860,7 @@ impl Type {
_ => self.clone(),
}
}
}

impl Type {
fn write_no_deps(&self, writer: &Writer) -> TokenStream {
if !writer.config.no_core {
return quote! {};
Expand Down Expand Up @@ -991,6 +989,16 @@ impl Type {
| Self::String
)
}

pub fn write_result_map(&self) -> TokenStream {
if self.is_copyable() {
quote! { map(|| result__) }
} else if self.is_convertible() {
quote! { and_then(||windows_core::Type::from_abi(result__)) }
} else {
quote! { map(|| core::mem::transmute(result__)) }
}
}
}

pub fn interface_signature(def: TypeDef, generics: &[Type]) -> String {
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/core/src/imp/com_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub unsafe fn CoCreateGuid() -> windows_core::Result<windows_core::GUID> {
windows_link::link!("ole32.dll" "system" fn CoCreateGuid(pguid : *mut windows_core::GUID) -> windows_core::HRESULT);
unsafe {
let mut result__ = core::mem::zeroed();
CoCreateGuid(&mut result__).map(|| core::mem::transmute(result__))
CoCreateGuid(&mut result__).map(|| result__)
}
}
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/windows/src/Windows/AI/MachineLearning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1572,7 +1572,7 @@ impl LearningModelDevice {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).AdapterId)(windows_core::Interface::as_raw(this), &mut result__).map(|| core::mem::transmute(result__))
(windows_core::Interface::vtable(this).AdapterId)(windows_core::Interface::as_raw(this), &mut result__).map(|| result__)
}
}
#[cfg(feature = "Graphics_DirectX_Direct3D11")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,14 @@ impl AppointmentsProviderShowTimeFrameActivatedEventArgs {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).TimeToShow)(windows_core::Interface::as_raw(this), &mut result__).map(|| core::mem::transmute(result__))
(windows_core::Interface::vtable(this).TimeToShow)(windows_core::Interface::as_raw(this), &mut result__).map(|| result__)
}
}
pub fn Duration(&self) -> windows_core::Result<super::super::Foundation::TimeSpan> {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).Duration)(windows_core::Interface::as_raw(this), &mut result__).map(|| core::mem::transmute(result__))
(windows_core::Interface::vtable(this).Duration)(windows_core::Interface::as_raw(this), &mut result__).map(|| result__)
}
}
}
Expand Down Expand Up @@ -2508,14 +2508,14 @@ impl IAppointmentsProviderShowTimeFrameActivatedEventArgs {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).TimeToShow)(windows_core::Interface::as_raw(this), &mut result__).map(|| core::mem::transmute(result__))
(windows_core::Interface::vtable(this).TimeToShow)(windows_core::Interface::as_raw(this), &mut result__).map(|| result__)
}
}
pub fn Duration(&self) -> windows_core::Result<super::super::Foundation::TimeSpan> {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).Duration)(windows_core::Interface::as_raw(this), &mut result__).map(|| core::mem::transmute(result__))
(windows_core::Interface::vtable(this).Duration)(windows_core::Interface::as_raw(this), &mut result__).map(|| result__)
}
}
pub fn Kind(&self) -> windows_core::Result<ActivationKind> {
Expand Down Expand Up @@ -5397,7 +5397,7 @@ impl IPhoneCallActivatedEventArgs {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).LineId)(windows_core::Interface::as_raw(this), &mut result__).map(|| core::mem::transmute(result__))
(windows_core::Interface::vtable(this).LineId)(windows_core::Interface::as_raw(this), &mut result__).map(|| result__)
}
}
pub fn Kind(&self) -> windows_core::Result<ActivationKind> {
Expand Down Expand Up @@ -7307,7 +7307,7 @@ impl PhoneCallActivatedEventArgs {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).LineId)(windows_core::Interface::as_raw(this), &mut result__).map(|| core::mem::transmute(result__))
(windows_core::Interface::vtable(this).LineId)(windows_core::Interface::as_raw(this), &mut result__).map(|| result__)
}
}
}
Expand Down Expand Up @@ -7849,7 +7849,7 @@ impl SplashScreen {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).ImageLocation)(windows_core::Interface::as_raw(this), &mut result__).map(|| core::mem::transmute(result__))
(windows_core::Interface::vtable(this).ImageLocation)(windows_core::Interface::as_raw(this), &mut result__).map(|| result__)
}
}
pub fn Dismissed<P0>(&self, handler: P0) -> windows_core::Result<i64>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,14 @@ impl AppointmentCalendarProposeNewTimeForMeetingRequest {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).NewStartTime)(windows_core::Interface::as_raw(this), &mut result__).map(|| core::mem::transmute(result__))
(windows_core::Interface::vtable(this).NewStartTime)(windows_core::Interface::as_raw(this), &mut result__).map(|| result__)
}
}
pub fn NewDuration(&self) -> windows_core::Result<super::super::super::Foundation::TimeSpan> {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).NewDuration)(windows_core::Interface::as_raw(this), &mut result__).map(|| core::mem::transmute(result__))
(windows_core::Interface::vtable(this).NewDuration)(windows_core::Interface::as_raw(this), &mut result__).map(|| result__)
}
}
pub fn Subject(&self) -> windows_core::Result<windows_core::HSTRING> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Appointment {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).StartTime)(windows_core::Interface::as_raw(this), &mut result__).map(|| core::mem::transmute(result__))
(windows_core::Interface::vtable(this).StartTime)(windows_core::Interface::as_raw(this), &mut result__).map(|| result__)
}
}
pub fn SetStartTime(&self, value: super::super::Foundation::DateTime) -> windows_core::Result<()> {
Expand All @@ -29,7 +29,7 @@ impl Appointment {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).Duration)(windows_core::Interface::as_raw(this), &mut result__).map(|| core::mem::transmute(result__))
(windows_core::Interface::vtable(this).Duration)(windows_core::Interface::as_raw(this), &mut result__).map(|| result__)
}
}
pub fn SetDuration(&self, value: super::super::Foundation::TimeSpan) -> windows_core::Result<()> {
Expand Down Expand Up @@ -353,7 +353,7 @@ impl AppointmentCalendar {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).DisplayColor)(windows_core::Interface::as_raw(this), &mut result__).map(|| core::mem::transmute(result__))
(windows_core::Interface::vtable(this).DisplayColor)(windows_core::Interface::as_raw(this), &mut result__).map(|| result__)
}
}
pub fn DisplayName(&self) -> windows_core::Result<windows_core::HSTRING> {
Expand Down Expand Up @@ -766,14 +766,14 @@ impl AppointmentCalendarSyncManager {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).LastSuccessfulSyncTime)(windows_core::Interface::as_raw(this), &mut result__).map(|| core::mem::transmute(result__))
(windows_core::Interface::vtable(this).LastSuccessfulSyncTime)(windows_core::Interface::as_raw(this), &mut result__).map(|| result__)
}
}
pub fn LastAttemptedSyncTime(&self) -> windows_core::Result<super::super::Foundation::DateTime> {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).LastAttemptedSyncTime)(windows_core::Interface::as_raw(this), &mut result__).map(|| core::mem::transmute(result__))
(windows_core::Interface::vtable(this).LastAttemptedSyncTime)(windows_core::Interface::as_raw(this), &mut result__).map(|| result__)
}
}
pub fn SyncAsync(&self) -> windows_core::Result<super::super::Foundation::IAsyncOperation<bool>> {
Expand Down Expand Up @@ -856,7 +856,7 @@ impl AppointmentConflictResult {
let this = self;
unsafe {
let mut result__ = core::mem::zeroed();
(windows_core::Interface::vtable(this).Date)(windows_core::Interface::as_raw(this), &mut result__).map(|| core::mem::transmute(result__))
(windows_core::Interface::vtable(this).Date)(windows_core::Interface::as_raw(this), &mut result__).map(|| result__)
}
}
}
Expand Down
Loading

0 comments on commit bfe7c64

Please sign in to comment.