Using futures::select macro with async functions

I can't seem to figure out how to a do a thing...

I have an async function that takes a mutable reference:

async fn do_thing(foo: &mut Foo)

I want to call it using futures::select!, but I also want to mutate its argument in a separate branch.

futures::select! {
    x = do_thing(&mut foo) => { ... },
    y = something_else() => {
        foo.mutate();
    },
}

This should be safe since select allows you to use the same variable mutably in separate branches. The problem is that I can't call an async fn directly as an argument to select because select requires it's arguments to be Unpin. So I can try pinning the async call separately like this:

let do_thing_future = do_thing(&mut foo);
pin_mut!(do_thing_future);
futures::select! {
    x = do_thing_future => { ... },
    y = something_else() => {
        foo.mutate();
    },
}

But now do_thing_future borrows foo for the duration of the select, so I can no longer mutate it in the other branch. Another option might be to use Box::pin on the async call:

futures::select! {
    x = Box::pin(do_thing(&mut foo)) => { ... },
    y = something_else() => {
        foo.mutate();
    },
}

But this also borrows foo for the duration of the select. From what I understand (though I could be wrong) I can't wrap &mut foo in anything that requires a destructor if I want the borrow checker to be flexible.

So are there any other options here? Is there some way to make this work?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.