wg-async icon indicating copy to clipboard operation
wg-async copied to clipboard

Asynchronous drops

Open YohDeadfall opened this issue 4 years ago • 1 comments

Brief summary

As a C# developer I really enjoy async disposals. The idea behind them is to have something like RAII, but non-blocking which is very important in case of handling IO (files, network connections, etc.).

The following example demonstrates usage automatic non-blocking buffer flushing of a stream and closing handles:

await using var stream = new SomeStream();
await stream.WriteAsync(/* data */);

Without using asynchronous disposal the code might block the current thread till the operation completes while it would be much better to schedule the operation using io_uring or completion ports, and reuse the current thread for other workload.

The idea can be useful not only for IO heavy applications, but also for cases when some action must be executed on a dedicated thread or in background and later the execution should continue on the original thread. A good example where it might be useful is UI: the main thread is for user interface updates and input handling, while any background for long running operations.

Optional details

  • (Optional) Which character(s) would be the best fit and why?
    • [x] Alan: the experienced "GC'd language" developer, new to Rust
    • [ ] Grace: the systems programming expert, new to Rust
    • [ ] Niklaus: new programmer from an unconventional background
    • [ ] Barbara: the experienced Rust developer
  • (Optional) Which project(s) would be the best fit and why?
    • Any which requires efficient thread utilisaion.
  • (Optional) What are the key points or morals to emphasize?
    • Write some morals here.

YohDeadfall avatar Apr 27 '21 16:04 YohDeadfall

See also here for a discussion of where async Drop could be very useful to solve an existing problem: https://internals.rust-lang.org/t/async-await-the-challenges-besides-syntax-cancellation/10287/2

ericsampson avatar Sep 25 '21 02:09 ericsampson