Skip to content

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

Closed

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.

Activity

  1. thestinger commented on Oct 2, 2014

    @thestinger
    Contributor

    libuv was removed as part of #17325 so this is no longer an issue for this repository

  2. added 2 commits that reference this issue on Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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