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

Simplify derefencing Ref interface parameters #3027

Merged
merged 1 commit into from
May 10, 2024
Merged

Simplify derefencing Ref interface parameters #3027

merged 1 commit into from
May 10, 2024

Conversation

kennykerr
Copy link
Collaborator

@kennykerr kennykerr commented May 10, 2024

Building on #3025, this adds a simpler way to deal with nullable input parameters that are required but without forcing an expensive reference count bump. Here's an example of the new ok method:

#![allow(non_snake_case)]
use windows_core::*;

#[interface("09428a59-5b40-4e4c-9175-e7a78514316d")]
unsafe trait ITest: IUnknown {
    unsafe fn Print(&self) -> Result<()>;
    unsafe fn PrintOther(&self, other: Ref<ITest>) -> Result<()>;
}

#[implement(ITest)]
struct Test;

impl ITest_Impl for Test {
    unsafe fn Print(&self) -> Result<()> {
        println!("Test");
        Ok(())
    }
    unsafe fn PrintOther(&self, other: Ref<ITest>) -> Result<()> {
        other.ok()?.Print()
     // ^^^^^^^^^^^^^^^^^^^
    }
}

fn main() -> Result<()> {
    unsafe {
        let test: ITest = Test.into();
        test.Print()?;
        test.PrintOther(&test)?;
        Ok(())
    }
}

The ok method simplifies what would otherwise require something like this:

other.as_ref().ok_or(Error::from(E_POINTER))?.Print()

@kennykerr kennykerr merged commit 3da42d8 into master May 10, 2024
71 checks passed
@kennykerr kennykerr deleted the ref-ok branch May 10, 2024 22:10
mati865 pushed a commit to mati865/windows-rs that referenced this pull request Jun 15, 2024
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.

2 participants