Skip to main content

read_file_to_slice

Function read_file_to_slice 

Source
pub unsafe fn read_file_to_slice<T>(
    file_handle: &FileHandle,
    buffer_slice: &mut [T],
    overlapped: *mut OVERLAPPED,
    offset: u64,
) -> Result<bool>
Expand description

Asynchronously queue a read request from a file into a buffer slice.

Wraps the unsafe Windows API function ReadFile, making it safe to call only when the overlapped buffer remains valid and unchanged anywhere else during the entire async operation.

Returns a boolean indicating whether the read operation completed synchronously or is pending.

ยงSafety

This function is marked as unsafe because it uses raw pointers and requires the caller to ensure that the buffer slice and the overlapped buffer stay valid during the whole async operation.

SAFETY: THIS IS NOT ENTIRELY SAFE! PLEASE READ!

This function is thread safe i.e. the same file handle can be used by multiple threads to read from the file as it uses the windows ReadFile API with async mode using OVERLAPPED structure. ReadFile Function - https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfile Synchronous and Asynchronous I/O - https://learn.microsoft.com/en-us/windows/win32/FileIO/synchronous-and-asynchronous-i-o

The only caveat is read operation is followed by polling on the handle using GetQueuedCompletionStatus API. If multiple threads are submitting read requests and polling then polling will return the completion status of any of the read requests. This is because GetQueuedCompletionStatus API returns the completion status of any of the read requests that are completed.