How do I implement an async Drop in Rust?
Asked Answered
H

2

12

I have an async fn that returns a type, and want to implement Drop on that type that calls another async function. It's not clear how to do this, and I can't find anything in the docs. The most illuminating article I found is Asynchronous Destructors by withoutboats, but I don't really understand the reasoning, or where this feature is at.

Hexagonal answered 17/1, 2020 at 6:32 Comment(1)
The point of the article is precisely to said that it's not currently possible. And propose a possible solution, but AFAIK it doesn't exist yet.Shrivel
T
7

It's not clear how to do this, and I can't find anything in the docs

That's because it's not possible; there is no "async Drop". Drop must be synchronous.

See also:

Trust answered 17/1, 2020 at 14:11 Comment(0)
U
1

Since this question was originally answered, there has been movement in the repo, and there is now a (nightly-only) AsyncDrop trait that those interested in such things can use. It doesn't define an async fn you can implement (because we don't have async traits yet), but it gives you a pinned self you can fiddle with to call into your executor to do the work.

Undershirt answered 5/5 at 23:40 Comment(1)
I guess when this is stabilized, this can be marked answer.Hexagonal

© 2022 - 2024 — McMap. All rights reserved.