From async drop - async fn fundamentals initiative:
- Able to identify the await points that result from async cleanup if needed
This proposal focuses on that point.
Async control flow
There could be async version of control flow keywords namely areturn
, abreak
, and acontinue
. When one or more values must be asynchronously dropped then one of these keywords must be at the end of scope to identify the drop point.
Below is rewritten example from linked above document:
async fn foo(dbc: DatabaseConnection) -> io::Result<()> {
let data = match socket().read().await {
Ok(x) => x,
e => areturn e
};
match dbc.write(data).await {
Ok(_) => areturn Ok(()),
e => areturn e
};
}
Perhaps there could be also aawait
to identify that async drop may be performed on task cancelation:
async fn foo(dbc: DatabaseConnection) -> io::Result<()> {
let data = match socket().read().aawait {
Ok(x) => x,
e => areturn e
};
match dbc.write(data).aawait {
Ok(_) => areturn Ok(()),
e => areturn e
};
}
Notice that because areturn
must be explicit there's no support for ?
operator when AsyncDrop
value is moved in scope. If async drop wouldn't be used often that should be an acceptable compromise.
Async assignment
To asynchronously drop on assignment there could be aassign
keyword:
async fn assign_stream(target: &mut TlsStream, source: TlsStream) {
*target aassign source;
return // no more `AsyncDrop` implementing value in scope
}
Async move
To explicitly asynchronously drop value we could have the following syntax:
drop(amove value)
Where drop
is generic over async
keyword.
Perhaps it shouldn't be possible to move AsyncDrop
values in a different way, so:
let a = returns_async_drop_value();
let b = amove a;
vec.push(amove b);
drop(amove vec);
let c = returns_async_drop_value();
let d = amove || c;
let e = returns_async_drop_value();
e amove
.into_method_chain()
.f().g().h();
Summary
This all of course is very chaotic, based on my very naive understanding of status quo of async drop, and I have no idea how it might work under the hood if it might. I only wanted to demonstrate the idea of prepending "a" to various keywords to identify that they support async drop — the rest is made up and up to implementation.