Skip to content

Commit 2b6c3de

Browse files
authored
docs: document Errno::result() in CONVENTIONS.md (#2358)
1 parent 391a364 commit 2b6c3de

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

CONVENTIONS.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,20 @@ If you want to add a test for a feature that is in `xxx.rs`, then the test shoul
135135
be put in the corresponding `test_xxx.rs` file unless you cannot do this, e.g.,
136136
the test involves private stuff and thus cannot be added outside of Nix, then
137137
it is allowed to leave the test in `xxx.rs`.
138+
139+
## Syscall/libc function error handling
140+
141+
Most syscall and libc functions return an [`ErrnoSentinel`][trait] value on error,
142+
we has a nice utility function [`Errno::result()`][util] to convert it to the
143+
Rusty `Result<T, Errno>` type, e.g., here is how `dup(2)` uses it:
144+
145+
```rs
146+
pub fn dup(oldfd: RawFd) -> Result<RawFd> {
147+
let res = unsafe { libc::dup(oldfd) };
148+
149+
Errno::result(res)
150+
}
151+
```
152+
153+
[trait]: https://docs.rs/nix/latest/nix/errno/trait.ErrnoSentinel.html
154+
[util]: https://docs.rs/nix/latest/nix/errno/enum.Errno.html#method.result

0 commit comments

Comments
 (0)