Skip to content

Commit

Permalink
Reintroduce Ref lifetime type parameter (#3448)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Jan 16, 2025
1 parent 68b029e commit 021257c
Show file tree
Hide file tree
Showing 258 changed files with 5,278 additions and 5,305 deletions.
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/types/cpp_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ fn write_param(writer: &Writer, ty: &Type, param: Param) -> TokenStream {
} else if ty.is_copyable() {
quote! { #name: #type_name, }
} else {
quote! { #name: windows_core::Ref<#type_name>, }
quote! { #name: windows_core::Ref<'_, #type_name>, }
}
}
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/types/cpp_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ fn write_produce_type(writer: &Writer, ty: &Type, param: Param) -> TokenStream {

if !param.flags().contains(ParamAttributes::Out) && ty.is_interface() {
let type_name = ty.write_name(writer);
quote! { #name: windows_core::Ref<#type_name>, }
quote! { #name: windows_core::Ref<'_, #type_name>, }
} else if param.flags().contains(ParamAttributes::Out) && ty.deref().is_interface() {
let type_name = ty.deref().write_name(writer);
quote! { #name: windows_core::OutRef<'_, #type_name>, }
Expand Down
7 changes: 2 additions & 5 deletions crates/libs/bindgen/src/types/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,9 @@ impl Method {
quote! { &[#default_type] }
} else if p.0.is_primitive() {
quote! { #default_type }
} else if p.0.is_interface() {
} else if p.0.is_interface() || matches!(&p.0, Type::Param(_)) {
let type_name = p.0.write_name(writer);
quote! { windows_core::Ref<#type_name> }
} else if matches!(&p.0, Type::Param(_)) {
let type_name = p.0.write_name(writer);
quote! { <#type_name as windows_core::Type<#type_name>>::Ref }
quote! { windows_core::Ref<'_, #type_name> }
} else {
quote! { &#default_type }
}
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/core/src/param_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<T: Type<T>> ParamValue<T> {
}
}

pub fn borrow(&self) -> Ref<T> {
pub fn borrow(&self) -> Ref<'_, T> {
unsafe { transmute_copy(&self.abi()) }
}
}
12 changes: 9 additions & 3 deletions crates/libs/core/src/ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use core::mem::transmute;

/// A borrowed type with the same memory layout as the type itself that can be used to construct ABI-compatible function signatures.
#[repr(transparent)]
pub struct Ref<T: Type<T>>(T::Abi);
pub struct Ref<'a, T: Type<T>>(T::Abi, core::marker::PhantomData<&'a T>);

impl<T: Type<T>> Ref<T> {
impl<T: Type<T>> Ref<'_, T> {
/// Returns `true` if the argument is null.
pub fn is_null(&self) -> bool {
T::is_null(&self.0)
Expand Down Expand Up @@ -46,9 +46,15 @@ impl<T: Type<T>> Ref<T> {
}
}

impl<T: Type<T>> core::ops::Deref for Ref<T> {
impl<T: Type<T>> core::ops::Deref for Ref<'_, T> {
type Target = T::Default;
fn deref(&self) -> &Self::Target {
unsafe { transmute(&self.0) }
}
}

impl<'a, T: Type<T>> From<&'a T::Default> for Ref<'a, T> {
fn from(from: &'a T::Default) -> Self {
unsafe { core::mem::transmute_copy(from) }
}
}
8 changes: 0 additions & 8 deletions crates/libs/core/src/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,19 @@ pub struct CopyType;
#[doc(hidden)]
pub trait Type<T: TypeKind, C = <T as TypeKind>::TypeKind>: TypeKind + Sized + Clone {
type Abi;
type Ref;
type Default;

fn is_null(abi: &Self::Abi) -> bool;
unsafe fn assume_init_ref(abi: &Self::Abi) -> &Self;
unsafe fn from_abi(abi: Self::Abi) -> Result<Self>;
fn from_default(default: &Self::Default) -> Result<Self>;

fn deref(param: &Self::Ref) -> &Self::Default {
unsafe { core::mem::transmute(param) }
}
}

impl<T> Type<T, InterfaceType> for T
where
T: TypeKind<TypeKind = InterfaceType> + Clone,
{
type Abi = *mut core::ffi::c_void;
type Ref = Ref<T>;
type Default = Option<Self>;

fn is_null(abi: &Self::Abi) -> bool {
Expand Down Expand Up @@ -66,7 +60,6 @@ where
T: TypeKind<TypeKind = CloneType> + Clone,
{
type Abi = core::mem::MaybeUninit<Self>;
type Ref = Ref<T>;
type Default = Self;

fn is_null(_: &Self::Abi) -> bool {
Expand All @@ -91,7 +84,6 @@ where
T: TypeKind<TypeKind = CopyType> + Clone,
{
type Abi = Self;
type Ref = Self;
type Default = Self;

fn is_null(_: &Self::Abi) -> bool {
Expand Down
26 changes: 13 additions & 13 deletions crates/libs/windows/src/Windows/ApplicationModel/Background/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ impl windows_core::RuntimeType for BackgroundTaskCanceledEventHandler {
const SIGNATURE: windows_core::imp::ConstBuffer = windows_core::imp::ConstBuffer::for_interface::<Self>();
}
impl BackgroundTaskCanceledEventHandler {
pub fn new<F: FnMut(windows_core::Ref<IBackgroundTaskInstance>, BackgroundTaskCancellationReason) -> windows_core::Result<()> + Send + 'static>(invoke: F) -> Self {
pub fn new<F: FnMut(windows_core::Ref<'_, IBackgroundTaskInstance>, BackgroundTaskCancellationReason) -> windows_core::Result<()> + Send + 'static>(invoke: F) -> Self {
let com = BackgroundTaskCanceledEventHandlerBox { vtable: &BackgroundTaskCanceledEventHandlerBox::<F>::VTABLE, count: windows_core::imp::RefCount::new(1), invoke };
unsafe { core::mem::transmute(windows_core::imp::Box::new(com)) }
}
Expand All @@ -600,12 +600,12 @@ pub struct BackgroundTaskCanceledEventHandler_Vtbl {
Invoke: unsafe extern "system" fn(this: *mut core::ffi::c_void, sender: *mut core::ffi::c_void, reason: BackgroundTaskCancellationReason) -> windows_core::HRESULT,
}
#[repr(C)]
struct BackgroundTaskCanceledEventHandlerBox<F: FnMut(windows_core::Ref<IBackgroundTaskInstance>, BackgroundTaskCancellationReason) -> windows_core::Result<()> + Send + 'static> {
struct BackgroundTaskCanceledEventHandlerBox<F: FnMut(windows_core::Ref<'_, IBackgroundTaskInstance>, BackgroundTaskCancellationReason) -> windows_core::Result<()> + Send + 'static> {
vtable: *const BackgroundTaskCanceledEventHandler_Vtbl,
invoke: F,
count: windows_core::imp::RefCount,
}
impl<F: FnMut(windows_core::Ref<IBackgroundTaskInstance>, BackgroundTaskCancellationReason) -> windows_core::Result<()> + Send + 'static> BackgroundTaskCanceledEventHandlerBox<F> {
impl<F: FnMut(windows_core::Ref<'_, IBackgroundTaskInstance>, BackgroundTaskCancellationReason) -> windows_core::Result<()> + Send + 'static> BackgroundTaskCanceledEventHandlerBox<F> {
const VTABLE: BackgroundTaskCanceledEventHandler_Vtbl = BackgroundTaskCanceledEventHandler_Vtbl { base__: windows_core::IUnknown_Vtbl { QueryInterface: Self::QueryInterface, AddRef: Self::AddRef, Release: Self::Release }, Invoke: Self::Invoke };
unsafe extern "system" fn QueryInterface(this: *mut core::ffi::c_void, iid: *const windows_core::GUID, interface: *mut *mut core::ffi::c_void) -> windows_core::HRESULT {
unsafe {
Expand Down Expand Up @@ -702,7 +702,7 @@ impl windows_core::RuntimeType for BackgroundTaskCompletedEventHandler {
const SIGNATURE: windows_core::imp::ConstBuffer = windows_core::imp::ConstBuffer::for_interface::<Self>();
}
impl BackgroundTaskCompletedEventHandler {
pub fn new<F: FnMut(windows_core::Ref<BackgroundTaskRegistration>, windows_core::Ref<BackgroundTaskCompletedEventArgs>) -> windows_core::Result<()> + Send + 'static>(invoke: F) -> Self {
pub fn new<F: FnMut(windows_core::Ref<'_, BackgroundTaskRegistration>, windows_core::Ref<'_, BackgroundTaskCompletedEventArgs>) -> windows_core::Result<()> + Send + 'static>(invoke: F) -> Self {
let com = BackgroundTaskCompletedEventHandlerBox { vtable: &BackgroundTaskCompletedEventHandlerBox::<F>::VTABLE, count: windows_core::imp::RefCount::new(1), invoke };
unsafe { core::mem::transmute(windows_core::imp::Box::new(com)) }
}
Expand All @@ -721,12 +721,12 @@ pub struct BackgroundTaskCompletedEventHandler_Vtbl {
Invoke: unsafe extern "system" fn(this: *mut core::ffi::c_void, sender: *mut core::ffi::c_void, args: *mut core::ffi::c_void) -> windows_core::HRESULT,
}
#[repr(C)]
struct BackgroundTaskCompletedEventHandlerBox<F: FnMut(windows_core::Ref<BackgroundTaskRegistration>, windows_core::Ref<BackgroundTaskCompletedEventArgs>) -> windows_core::Result<()> + Send + 'static> {
struct BackgroundTaskCompletedEventHandlerBox<F: FnMut(windows_core::Ref<'_, BackgroundTaskRegistration>, windows_core::Ref<'_, BackgroundTaskCompletedEventArgs>) -> windows_core::Result<()> + Send + 'static> {
vtable: *const BackgroundTaskCompletedEventHandler_Vtbl,
invoke: F,
count: windows_core::imp::RefCount,
}
impl<F: FnMut(windows_core::Ref<BackgroundTaskRegistration>, windows_core::Ref<BackgroundTaskCompletedEventArgs>) -> windows_core::Result<()> + Send + 'static> BackgroundTaskCompletedEventHandlerBox<F> {
impl<F: FnMut(windows_core::Ref<'_, BackgroundTaskRegistration>, windows_core::Ref<'_, BackgroundTaskCompletedEventArgs>) -> windows_core::Result<()> + Send + 'static> BackgroundTaskCompletedEventHandlerBox<F> {
const VTABLE: BackgroundTaskCompletedEventHandler_Vtbl = BackgroundTaskCompletedEventHandler_Vtbl { base__: windows_core::IUnknown_Vtbl { QueryInterface: Self::QueryInterface, AddRef: Self::AddRef, Release: Self::Release }, Invoke: Self::Invoke };
unsafe extern "system" fn QueryInterface(this: *mut core::ffi::c_void, iid: *const windows_core::GUID, interface: *mut *mut core::ffi::c_void) -> windows_core::HRESULT {
unsafe {
Expand Down Expand Up @@ -825,7 +825,7 @@ impl windows_core::RuntimeType for BackgroundTaskProgressEventHandler {
const SIGNATURE: windows_core::imp::ConstBuffer = windows_core::imp::ConstBuffer::for_interface::<Self>();
}
impl BackgroundTaskProgressEventHandler {
pub fn new<F: FnMut(windows_core::Ref<BackgroundTaskRegistration>, windows_core::Ref<BackgroundTaskProgressEventArgs>) -> windows_core::Result<()> + Send + 'static>(invoke: F) -> Self {
pub fn new<F: FnMut(windows_core::Ref<'_, BackgroundTaskRegistration>, windows_core::Ref<'_, BackgroundTaskProgressEventArgs>) -> windows_core::Result<()> + Send + 'static>(invoke: F) -> Self {
let com = BackgroundTaskProgressEventHandlerBox { vtable: &BackgroundTaskProgressEventHandlerBox::<F>::VTABLE, count: windows_core::imp::RefCount::new(1), invoke };
unsafe { core::mem::transmute(windows_core::imp::Box::new(com)) }
}
Expand All @@ -844,12 +844,12 @@ pub struct BackgroundTaskProgressEventHandler_Vtbl {
Invoke: unsafe extern "system" fn(this: *mut core::ffi::c_void, sender: *mut core::ffi::c_void, args: *mut core::ffi::c_void) -> windows_core::HRESULT,
}
#[repr(C)]
struct BackgroundTaskProgressEventHandlerBox<F: FnMut(windows_core::Ref<BackgroundTaskRegistration>, windows_core::Ref<BackgroundTaskProgressEventArgs>) -> windows_core::Result<()> + Send + 'static> {
struct BackgroundTaskProgressEventHandlerBox<F: FnMut(windows_core::Ref<'_, BackgroundTaskRegistration>, windows_core::Ref<'_, BackgroundTaskProgressEventArgs>) -> windows_core::Result<()> + Send + 'static> {
vtable: *const BackgroundTaskProgressEventHandler_Vtbl,
invoke: F,
count: windows_core::imp::RefCount,
}
impl<F: FnMut(windows_core::Ref<BackgroundTaskRegistration>, windows_core::Ref<BackgroundTaskProgressEventArgs>) -> windows_core::Result<()> + Send + 'static> BackgroundTaskProgressEventHandlerBox<F> {
impl<F: FnMut(windows_core::Ref<'_, BackgroundTaskRegistration>, windows_core::Ref<'_, BackgroundTaskProgressEventArgs>) -> windows_core::Result<()> + Send + 'static> BackgroundTaskProgressEventHandlerBox<F> {
const VTABLE: BackgroundTaskProgressEventHandler_Vtbl = BackgroundTaskProgressEventHandler_Vtbl { base__: windows_core::IUnknown_Vtbl { QueryInterface: Self::QueryInterface, AddRef: Self::AddRef, Release: Self::Release }, Invoke: Self::Invoke };
unsafe extern "system" fn QueryInterface(this: *mut core::ffi::c_void, iid: *const windows_core::GUID, interface: *mut *mut core::ffi::c_void) -> windows_core::HRESULT {
unsafe {
Expand Down Expand Up @@ -2245,7 +2245,7 @@ impl windows_core::RuntimeName for IBackgroundTask {
const NAME: &'static str = "Windows.ApplicationModel.Background.IBackgroundTask";
}
pub trait IBackgroundTask_Impl: windows_core::IUnknownImpl {
fn Run(&self, taskInstance: windows_core::Ref<IBackgroundTaskInstance>) -> windows_core::Result<()>;
fn Run(&self, taskInstance: windows_core::Ref<'_, IBackgroundTaskInstance>) -> windows_core::Result<()>;
}
impl IBackgroundTask_Vtbl {
pub const fn new<Identity: IBackgroundTask_Impl, const OFFSET: isize>() -> Self {
Expand Down Expand Up @@ -2436,7 +2436,7 @@ pub trait IBackgroundTaskInstance_Impl: windows_core::IUnknownImpl {
fn Progress(&self) -> windows_core::Result<u32>;
fn SetProgress(&self, value: u32) -> windows_core::Result<()>;
fn TriggerDetails(&self) -> windows_core::Result<windows_core::IInspectable>;
fn Canceled(&self, cancelHandler: windows_core::Ref<BackgroundTaskCanceledEventHandler>) -> windows_core::Result<i64>;
fn Canceled(&self, cancelHandler: windows_core::Ref<'_, BackgroundTaskCanceledEventHandler>) -> windows_core::Result<i64>;
fn RemoveCanceled(&self, cookie: i64) -> windows_core::Result<()>;
fn SuspendedCount(&self) -> windows_core::Result<u32>;
fn GetDeferral(&self) -> windows_core::Result<BackgroundTaskDeferral>;
Expand Down Expand Up @@ -2864,9 +2864,9 @@ impl windows_core::RuntimeName for IBackgroundTaskRegistration {
pub trait IBackgroundTaskRegistration_Impl: windows_core::IUnknownImpl {
fn TaskId(&self) -> windows_core::Result<windows_core::GUID>;
fn Name(&self) -> windows_core::Result<windows_core::HSTRING>;
fn Progress(&self, handler: windows_core::Ref<BackgroundTaskProgressEventHandler>) -> windows_core::Result<i64>;
fn Progress(&self, handler: windows_core::Ref<'_, BackgroundTaskProgressEventHandler>) -> windows_core::Result<i64>;
fn RemoveProgress(&self, cookie: i64) -> windows_core::Result<()>;
fn Completed(&self, handler: windows_core::Ref<BackgroundTaskCompletedEventHandler>) -> windows_core::Result<i64>;
fn Completed(&self, handler: windows_core::Ref<'_, BackgroundTaskCompletedEventHandler>) -> windows_core::Result<i64>;
fn RemoveCompleted(&self, cookie: i64) -> windows_core::Result<()>;
fn Unregister(&self, cancelTask: bool) -> windows_core::Result<()>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4952,7 +4952,7 @@ impl windows_core::RuntimeName for IContactInstantMessageFieldFactory {
pub trait IContactInstantMessageFieldFactory_Impl: windows_core::IUnknownImpl {
fn CreateInstantMessage_Default(&self, userName: &windows_core::HSTRING) -> windows_core::Result<ContactInstantMessageField>;
fn CreateInstantMessage_Category(&self, userName: &windows_core::HSTRING, category: ContactFieldCategory) -> windows_core::Result<ContactInstantMessageField>;
fn CreateInstantMessage_All(&self, userName: &windows_core::HSTRING, category: ContactFieldCategory, service: &windows_core::HSTRING, displayText: &windows_core::HSTRING, verb: windows_core::Ref<super::super::Foundation::Uri>) -> windows_core::Result<ContactInstantMessageField>;
fn CreateInstantMessage_All(&self, userName: &windows_core::HSTRING, category: ContactFieldCategory, service: &windows_core::HSTRING, displayText: &windows_core::HSTRING, verb: windows_core::Ref<'_, super::super::Foundation::Uri>) -> windows_core::Result<ContactInstantMessageField>;
}
impl IContactInstantMessageFieldFactory_Vtbl {
pub const fn new<Identity: IContactInstantMessageFieldFactory_Impl, const OFFSET: isize>() -> Self {
Expand Down
6 changes: 3 additions & 3 deletions crates/libs/windows/src/Windows/ApplicationModel/Core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ impl windows_core::RuntimeName for ICoreApplicationUnhandledError {
const NAME: &'static str = "Windows.ApplicationModel.Core.ICoreApplicationUnhandledError";
}
pub trait ICoreApplicationUnhandledError_Impl: windows_core::IUnknownImpl {
fn UnhandledErrorDetected(&self, handler: windows_core::Ref<super::super::Foundation::EventHandler<UnhandledErrorDetectedEventArgs>>) -> windows_core::Result<i64>;
fn UnhandledErrorDetected(&self, handler: windows_core::Ref<'_, super::super::Foundation::EventHandler<UnhandledErrorDetectedEventArgs>>) -> windows_core::Result<i64>;
fn RemoveUnhandledErrorDetected(&self, token: i64) -> windows_core::Result<()>;
}
impl ICoreApplicationUnhandledError_Vtbl {
Expand Down Expand Up @@ -836,8 +836,8 @@ impl windows_core::RuntimeName for IFrameworkView {
}
#[cfg(feature = "UI_Core")]
pub trait IFrameworkView_Impl: windows_core::IUnknownImpl {
fn Initialize(&self, applicationView: windows_core::Ref<CoreApplicationView>) -> windows_core::Result<()>;
fn SetWindow(&self, window: windows_core::Ref<super::super::UI::Core::CoreWindow>) -> windows_core::Result<()>;
fn Initialize(&self, applicationView: windows_core::Ref<'_, CoreApplicationView>) -> windows_core::Result<()>;
fn SetWindow(&self, window: windows_core::Ref<'_, super::super::UI::Core::CoreWindow>) -> windows_core::Result<()>;
fn Load(&self, entryPoint: &windows_core::HSTRING) -> windows_core::Result<()>;
fn Run(&self) -> windows_core::Result<()>;
fn Uninitialize(&self) -> windows_core::Result<()>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,10 @@ impl windows_core::RuntimeName for ICoreDropOperationTarget {
const NAME: &'static str = "Windows.ApplicationModel.DataTransfer.DragDrop.Core.ICoreDropOperationTarget";
}
pub trait ICoreDropOperationTarget_Impl: windows_core::IUnknownImpl {
fn EnterAsync(&self, dragInfo: windows_core::Ref<CoreDragInfo>, dragUIOverride: windows_core::Ref<CoreDragUIOverride>) -> windows_core::Result<super::super::super::super::Foundation::IAsyncOperation<super::super::DataPackageOperation>>;
fn OverAsync(&self, dragInfo: windows_core::Ref<CoreDragInfo>, dragUIOverride: windows_core::Ref<CoreDragUIOverride>) -> windows_core::Result<super::super::super::super::Foundation::IAsyncOperation<super::super::DataPackageOperation>>;
fn LeaveAsync(&self, dragInfo: windows_core::Ref<CoreDragInfo>) -> windows_core::Result<super::super::super::super::Foundation::IAsyncAction>;
fn DropAsync(&self, dragInfo: windows_core::Ref<CoreDragInfo>) -> windows_core::Result<super::super::super::super::Foundation::IAsyncOperation<super::super::DataPackageOperation>>;
fn EnterAsync(&self, dragInfo: windows_core::Ref<'_, CoreDragInfo>, dragUIOverride: windows_core::Ref<'_, CoreDragUIOverride>) -> windows_core::Result<super::super::super::super::Foundation::IAsyncOperation<super::super::DataPackageOperation>>;
fn OverAsync(&self, dragInfo: windows_core::Ref<'_, CoreDragInfo>, dragUIOverride: windows_core::Ref<'_, CoreDragUIOverride>) -> windows_core::Result<super::super::super::super::Foundation::IAsyncOperation<super::super::DataPackageOperation>>;
fn LeaveAsync(&self, dragInfo: windows_core::Ref<'_, CoreDragInfo>) -> windows_core::Result<super::super::super::super::Foundation::IAsyncAction>;
fn DropAsync(&self, dragInfo: windows_core::Ref<'_, CoreDragInfo>) -> windows_core::Result<super::super::super::super::Foundation::IAsyncOperation<super::super::DataPackageOperation>>;
}
impl ICoreDropOperationTarget_Vtbl {
pub const fn new<Identity: ICoreDropOperationTarget_Impl, const OFFSET: isize>() -> Self {
Expand Down
Loading

0 comments on commit 021257c

Please sign in to comment.