Skip to content

Add a non-panic API for converting raw integers to Errno. - #1738

Open
sunfishcode wants to merge 2 commits into
mainfrom
sunfishcode/try-from-os-error
Open

sunfishcode wants to merge 2 commits into
mainfrom
sunfishcode/try-from-os-error

Conversation

@sunfishcode

Copy link
Copy Markdown
Member

Add an Errno::try_from_raw_os_error function, as a non-panicking version of Errno::from_raw_os_error.

While here, fix the range check in Errno::from_raw_os_error to correctly check the full range of disallowed values.

Add an `Errno::try_from_raw_os_error` function, as a non-panicking
version of `Errno::from_raw_os_error`.

While here, fix the range check in `Errno::from_raw_os_error` to
correctly check the full range of disallowed values.
@stepancheg

Copy link
Copy Markdown

I think libc version of Errno also needs such function.

Separately, libc version of Errno should be at least NonZero for consistency.

#[inline]
pub const fn try_from_raw_os_error(raw: i32) -> Option<Self> {
// Unlike the linux_raw backend, we don't know the range of errno
// values that libc might use, so we assume any value is valid.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We know zero is not valid:

No function in this volume of POSIX.1-2024 shall set errno to 0.

https://pubs.opengroup.org/onlinepubs/9799919799/functions/errno.html

Similarly, C spec:

library functions never store 0 in errno

https://en.cppreference.com/c/error/errno

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants