Source Edit

This module implements asynchronous file reading and writing.

  1. import std/[asyncfile, asyncdispatch, os]
  2. proc main() {.async.} =
  3. var file = openAsync(getTempDir() / "foobar.txt", fmReadWrite)
  4. await file.write("test")
  5. file.setFilePos(0)
  6. let data = await file.readAll()
  7. doAssert data == "test"
  8. file.close()
  9. waitFor main()

Imports

asyncdispatch, os, winlean

Types

  1. AsyncFile = ref object

Source Edit

Procs

  1. proc close(f: AsyncFile) {....raises: [OSError], tags: [], forbids: [].}

Closes the file specified. Source Edit

  1. proc getFilePos(f: AsyncFile): int64 {....raises: [], tags: [], forbids: [].}

Retrieves the current position of the file pointer that is used to read from the specified file. The file’s first byte has the index zero. Source Edit

  1. proc getFileSize(f: AsyncFile): int64 {....raises: [OSError], tags: [], forbids: [].}

Retrieves the specified file’s size. Source Edit

  1. proc newAsyncFile(fd: AsyncFD): AsyncFile {....raises: [OSError], tags: [],
  2. forbids: [].}

Creates AsyncFile with a previously opened file descriptor fd. Source Edit

  1. proc openAsync(filename: string; mode = fmRead): AsyncFile {....raises: [OSError],
  2. tags: [], forbids: [].}

Opens a file specified by the path in filename using the specified FileMode mode asynchronously. Source Edit

  1. proc read(f: AsyncFile; size: int): Future[string] {.
  2. ...raises: [ValueError, Exception], tags: [RootEffect], forbids: [].}

Read size bytes from the specified file asynchronously starting at the current position of the file pointer. size should be greater than zero.

If the file pointer is past the end of the file then an empty string is returned.

Source Edit

  1. proc readAll(f: AsyncFile): Future[string] {....stackTrace: false,
  2. raises: [Exception, ValueError], tags: [RootEffect], forbids: [].}

Reads all data from the specified file. Source Edit

  1. proc readBuffer(f: AsyncFile; buf: pointer; size: int): Future[int] {.
  2. ...raises: [ValueError, Exception], tags: [RootEffect], forbids: [].}

Read size bytes from the specified file asynchronously starting at the current position of the file pointer.

If the file pointer is past the end of the file then zero is returned and no bytes are read into buf

Source Edit

  1. proc readLine(f: AsyncFile): Future[string] {....stackTrace: false,
  2. raises: [Exception, ValueError], tags: [RootEffect], forbids: [].}

Reads a single line from the specified file asynchronously. Source Edit

  1. proc readToStream(f: AsyncFile; fs: FutureStream[string]): owned(
  2. Future[void]) {....stackTrace: false, raises: [Exception], tags: [RootEffect],
  3. forbids: [].}

Writes data to the specified future stream as the file is read. Source Edit

  1. proc setFilePos(f: AsyncFile; pos: int64) {....raises: [], tags: [], forbids: [].}

Sets the position of the file pointer that is used for read/write operations. The file’s first byte has the index zero. Source Edit

  1. proc setFileSize(f: AsyncFile; length: int64) {....raises: [OSError], tags: [],
  2. forbids: [].}

Set a file length. Source Edit

  1. proc write(f: AsyncFile; data: string): Future[void] {....raises: [Exception],
  2. tags: [RootEffect], forbids: [].}

Writes data to the file specified asynchronously.

The returned Future will complete once all data has been written to the specified file.

Source Edit

  1. proc writeBuffer(f: AsyncFile; buf: pointer; size: int): Future[void] {.
  2. ...raises: [Exception], tags: [RootEffect], forbids: [].}

Writes size bytes from buf to the file specified asynchronously.

The returned Future will complete once all data has been written to the specified file.

Source Edit

  1. proc writeFromStream(f: AsyncFile; fs: FutureStream[string]): owned(
  2. Future[void]) {....stackTrace: false, raises: [Exception], tags: [RootEffect],
  3. forbids: [].}

Reads data from the specified future stream until it is completed. The data which is read is written to the file immediately and freed from memory.

This procedure is perfect for saving streamed data to a file without wasting memory.

Source Edit