rodrigocfd / winsafe

Windows API and GUI in safe, idiomatic Rust.
https://crates.io/crates/winsafe
MIT License
518 stars 30 forks source link

Async IO should be marked as "unsafe" #116

Closed TomzBench closed 9 months ago

TomzBench commented 10 months ago

Hello,

I believe the async operations on file handles opened with FILE_FLAG_OVERLAPPED, should be marked as "unsafe" when the ReadFile/WriteFile family of functions are used in the overlapped mode. Because, the OVERLAPPED structure and the buffer must remain untouched and live until the async operation completes. This cannot be guaranteed by the compiler when ownership is passed to the kernel and the references are casted away, Therefore it is up to the caller to maintain this guarantee manually.

However, it is safe in synchronous mode, and it would be unfair to mark this as unsafe as collateral to when meeting the requirments used in async mode. I would suggest splitting the methods as read_file/write_file and read_file_overlapped/write_file_overlapped where the overlapped alternatives would be marked as unsafe.

rodrigocfd commented 9 months ago

Your interpretation is correct, thank you.

By now I'll just remove the OVERLAPPED parameter. This seems to be an interesting use case for async Rust.