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

Avoid over-wrapping optional callback parameters #3452

Merged
merged 3 commits into from
Jan 16, 2025

Conversation

kennykerr
Copy link
Collaborator

@kennykerr kennykerr commented Jan 16, 2025

When a parameter is both optional and a callback, it should not be wrapped in an Option.

Fixes: #3437

I have been thinking about removing the Option from the callback type itself. At that point we'd not need this workaround and we'd be able to distinguish between optional and required callback parameters, but this is a small and targeted fix to the immediate problem for the time being.

@kennykerr kennykerr requested a review from riverar January 16, 2025 17:13
Copy link
Collaborator

@riverar riverar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks OK in my testing.

One thing I observed--maybe known/intentional--is that delegate types are emitted with a Option wrapper, despite being not optional in all dependent functions. Should we drop the Optional at that site instead and then keep the existing code?

Edit: Ah you addressed this in your edit. 👍

// Metadata
unsafe HRESULT setPfnMiniPDBErrorCallback2(
  [In] void* pvContext,
  [In] PFNMINIPDBERRORCALLBACK2 pfn);
pub type PFNMINIPDBERRORCALLBACK2 = Option<
    unsafe extern "system" fn(
        pvcontext: *mut core::ffi::c_void,
        dwerrorcode: u32,
        szobjorpdb: windows_core::PCWSTR,
        szlib: windows_core::PCWSTR,
    ) -> windows_core::HRESULT,
>;

@kennykerr
Copy link
Collaborator Author

Right, I'm leaning toward making the C++ delegate type omit Option as I think that would make it easier to use those types generally and they would behave more consistently with other non-nullable types in windows-rs like interfaces that you have to explicitly wrap in Option as needed. But that's a bigger change I'll work separately.

@kennykerr kennykerr merged commit e99e11c into master Jan 16, 2025
78 checks passed
@kennykerr kennykerr deleted the optional-cpp-delegate branch January 16, 2025 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

C++ delegate parameters are already optional
2 participants