[libc-commits] [PATCH] D139576: [libc] move errno out of file internals

Siva Chandra via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Dec 7 23:54:19 PST 2022


sivachandra added inline comments.


================
Comment at: libc/src/__support/File/file.h:51
+  using WriteFunc = FileResult<size_t>(File *, const void *, size_t);
+  using ReadFunc = FileResult<size_t>(File *, void *, size_t);
   // The SeekFunc is expected to return the current offset of the external
----------------
For read and write operations, I think is fine with a slight modification. Instead of calling the result type `FileResult` and making it a template, just define it this way:

```
struct FileIOResult {
  size_t size;
  int error;

  constexpr FileResult(size_t s) : size(s), error(0) {}
  constexpr FileResult(size_t s, int err) : size(s), error(err) {}

  constexpr operator size_t() const { return size; }
  constexpr operator bool() const { return error != 0; }
};
```

The call sites will be like:

```
const auto result = file.write(...)
if (!result)
  errno = result.error;
return result; // OK because of operator size_t
```


================
Comment at: libc/src/__support/File/file.h:54
   // file position indicator.
-  using SeekFunc = long(File *, long, int);
-  using CloseFunc = int(File *);
-  using FlushFunc = int(File *);
+  using SeekFunc = FileResult<long>(File *, long, int);
+  using CloseFunc = FileResult<int>(File *);
----------------
This is an example of an `ErrorOr` or `StatusOr` return value.


================
Comment at: libc/src/__support/File/file.h:56
+  using CloseFunc = FileResult<int>(File *);
+  using FlushFunc = FileResult<int>(File *);
 
----------------
The close and flush operation functions can return the error value directly. Call sites will detect an error if they see a non-zero return value.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D139576/new/

https://reviews.llvm.org/D139576



More information about the libc-commits mailing list