Skip to content

Add async ready support#3221

Merged
kennykerr merged 6 commits intomasterfrom
async-ready
Aug 27, 2024
Merged

Add async ready support#3221
kennykerr merged 6 commits intomasterfrom
async-ready

Conversation

@kennykerr
Copy link
Copy Markdown
Collaborator

@kennykerr kennykerr commented Aug 26, 2024

Building on #3213, this update adds support for creating async implementations that are immediately ready with a value. This is analogous to https://doc.rust-lang.org/stable/std/future/fn.ready.html and an acknowledgement that there's no need to spin up a thread or switch contexts if the results are already available - its way more efficient to just return them directly.

For example, let's say you need an IAsyncOperation<i32> that results in a value of 123. You can create one as follows:

use windows::{core::*, Foundation::*};

fn main() -> Result<()> {
    let ready = IAsyncOperation::ready(Ok(123));

    assert_eq!(ready.get()?, 123);

    Ok(())
}

The get call, which blocks the calling thread until the results are available, will return immediately with the expected value.

@kennykerr

This comment was marked as resolved.

@kennykerr kennykerr merged commit 0cce46b into master Aug 27, 2024
@kennykerr kennykerr deleted the async-ready branch August 27, 2024 00:52
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.

1 participant