Skip to content

librustuv: avoid using the magic number -1 for offset #17433

Closed
@nodakai

Description

@nodakai

The signature of uv_fs_read() is

UV_EXTERN int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, uv_file file,
    void* buf, size_t length, int64_t offset, uv_fs_cb cb);

and when we pass -1 as offset, libuv discards it in favor of the intrinsic file offset.

  if (offset != -1) {
    memset(&overlapped, 0, sizeof overlapped);

    offset_.QuadPart = offset;
    overlapped.Offset = offset_.LowPart;
    overlapped.OffsetHigh = offset_.HighPart;

    overlapped_ptr = &overlapped;
  } else {
    overlapped_ptr = NULL;
  }
static ssize_t uv__fs_read(uv_fs_t* req) {
  if (req->off < 0)
    return read(req->file, req->buf, req->len);
  else
    return pread(req->file, req->buf, req->len, req->off);
}

(The same goes for write operations.)

Such an interface design is fine for libuv itself, but librustuv should present more meaningful interface such as offset: Option<u64>. Note that the role of the magic number -1 is documented nowhere in libuv (sample codes in the uvbook use it without any explanations.) That pretty much means the users of libuv are expected to go through its implementation code in C. But all the developers of Rust don't need to do so.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions