As far as I understand, in Rust, every async function gets converted to a class/struct with
It means that in many situations it is important to carefully control how those objects die, especially if we want them to be dropped in certain order / gracefully.
I have read documentation of tokio, and it seems it has two things for this:
- cancellation token to notify "async functions" that they have to stop and
- they exploit
tokio::sync::mpsc::Senderobject, when they are dropped, parent knows that all its "children" has stopped.
Hope this makes sense so far.
It appears that pretty much every function (if we are talking about a stack of nested async functions), should have these two parameters:
ct: tokio_util::sync::CancellationToken, died_signal: tokio::sync::mpsc::Sender<()>
- Does it make sense to wrap these two things in a dedicated struct?
- If it makes sense, are there such structs somewhere already?
- If not, why? It looks like it is a common task...