# Pre-RFC: POSIX error numbers in \`std::os::unix\`

**URL:** <https://internals.rust-lang.org/t/pre-rfc-posix-error-numbers-in-std-unix/12852>\
**Category:** libs\
**Created:** [August 7, 2020, 12:35pm UTC](https://internals.rust-lang.org/t/pre-rfc-posix-error-numbers-in-std-unix/12852 "2020-08-07T12:35:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmillikin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jmillikin/32/4553_2.png) [@jmillikin](https://internals.rust-lang.org/u/jmillikin)\
**Post date:** [August 7, 2020, 12:35pm UTC](https://internals.rust-lang.org/t/pre-rfc-posix-error-numbers-in-std-unix/12852/1 "2020-08-07T12:35:07Z")

</div>

The POSIX standard defines a set of named "error numbers", such as `EIO` or `EPIPE`, for use in communicating error conditions from low-level OS routines. Many of these are currently exposed in `libc` as `c_int` constants, because they figure prominently in writing bindings to libc on UNIX platforms.

However, there are two problems with this solution:

- POSIX does not specify the type of these error numbers -- they are defined as C macros, which are untyped integer literals. Many traditional UNIX APIs assume that they can use any integer type for returning error numbers, which is awkward to use with a `c_int` const.
- Binaries that interact with the kernel directly (e.g. via `asm!()` syscalls) need to interpret error numbers, and should not require a dependency on `libc` to do so.

I propose adding an `ErrorNumber` struct to `std::os::unix`, which would provide limited conversion and comparison capabilities:

- `Eq` would be defined for built-in integer types (`i32`, `u64`, etc)
- `From` would be defined to convert from `ErrorNumber` to built-in integer types, and also to `std::io::Error`.
- Construction functions returning `Option<ErrorNumber>`, for construction from (1) unsigned where 0 is OK and (2) signed result codes from functions like `write(2)`.

The OS-specific integer constants would be associated values of `ErrorNumber`, yielding code like this:

```rust
use std::os::unix::ErrorNumber;

fn some_syscall() -> io::Result<()> {
  while {
    let err = some_syscall_raw();
    if err == 0 {
      return Ok(());
    }
    if err != ErrorNumber::EAGAIN {
      return Err(err.into());
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![jethrogb](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jethrogb](https://internals.rust-lang.org/u/jethrogb)\
**Post date:** [August 7, 2020, 1:20pm UTC](https://internals.rust-lang.org/t/pre-rfc-posix-error-numbers-in-std-unix/12852/2 "2020-08-07T13:20:56Z")

</div>

> [@jmillikin](#):
>
> Binaries that interact with the kernel directly (e.g. via `asm!()` syscalls) need to interpret error numbers, and should not require a dependency on `libc` to do so.

Why not?

In general, `std` should provide cross-platform abstractions for often-used primitives. I don't think it needs to provide any facilities for directly invoking syscalls with assembly, and I don't think it needs to provide POSIX definitions in general.

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [August 7, 2020, 5:20pm UTC](https://internals.rust-lang.org/t/pre-rfc-posix-error-numbers-in-std-unix/12852/3 "2020-08-07T17:20:31Z")

</div>

We already of unix specific functions in std behind `cfg!`'s, so I don't see why adding this would be a bad idea

---

<div class="post-metadata">

**Author:** ![mjbshaw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mjbshaw/32/5103_2.png) [@mjbshaw](https://internals.rust-lang.org/u/mjbshaw)\
**Post date:** [August 7, 2020, 10:09pm UTC](https://internals.rust-lang.org/t/pre-rfc-posix-error-numbers-in-std-unix/12852/4 "2020-08-07T22:09:43Z")

</div>

> [@jethrogb](#):
>
> Why not?
> 
> In general, `std` should provide cross-platform abstractions for often-used primitives. I don't think it needs to provide any facilities for directly invoking syscalls with assembly, and I don't think it needs to provide POSIX definitions in general.

[There are a number of people](https://github.com/rust-lang/rfcs/issues/2610) interested in a libc-free target on some \*nix platforms. And as Nokel81 mentions, std already exports a number of \*nix-only APIs.

---

<div class="post-metadata">

**Author:** ![jmillikin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jmillikin/32/4553_2.png) [@jmillikin](https://internals.rust-lang.org/u/jmillikin)\
**Post date:** [August 9, 2020, 12:50am UTC](https://internals.rust-lang.org/t/pre-rfc-posix-error-numbers-in-std-unix/12852/5 "2020-08-09T00:50:13Z")

</div>

rust-lang/rfcs PR: [https://github.com/rust-lang/rfcs/pull/2973](https://github.com/rust-lang/rfcs/pull/2973)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [November 7, 2020, 12:50am UTC](https://internals.rust-lang.org/t/pre-rfc-posix-error-numbers-in-std-unix/12852/6 "2020-11-07T00:50:30Z")

</div>

Closed on November 7, 2020, 12:50am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
