Skip to content

Commit 2d796eb

Browse files
committed
Collapse Error into Errno
Now that Nix's weird error types are eliminated, there's no reason not to simply use Errno as the Error type.
1 parent 6511d02 commit 2d796eb

25 files changed

+142
-209
lines changed

src/errno.rs

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use cfg_if::cfg_if;
22
use libc::{c_int, c_void};
3+
use std::convert::TryFrom;
34
use std::{fmt, io, error};
45
use crate::{Error, Result};
56

@@ -48,6 +49,42 @@ pub fn errno() -> i32 {
4849
}
4950

5051
impl Errno {
52+
/// Convert this `Error` to an [`Errno`](enum.Errno.html).
53+
///
54+
/// # Example
55+
///
56+
/// ```
57+
/// # use nix::Error;
58+
/// # use nix::errno::Errno;
59+
/// let e = Error::from(Errno::EPERM);
60+
/// assert_eq!(Some(Errno::EPERM), e.as_errno());
61+
/// ```
62+
#[deprecated(
63+
since = "0.22.0",
64+
note = "It's a no-op now; just delete it."
65+
)]
66+
pub fn as_errno(self) -> Option<Self> {
67+
Some(self)
68+
}
69+
70+
/// Create a nix Error from a given errno
71+
#[deprecated(
72+
since = "0.22.0",
73+
note = "It's a no-op now; just delete it."
74+
)]
75+
pub fn from_errno(errno: Errno) -> Error {
76+
Error::from(errno)
77+
}
78+
79+
/// Create a new invalid argument error (`EINVAL`)
80+
#[deprecated(
81+
since = "0.22.0",
82+
note = "Use Errno::EINVAL instead"
83+
)]
84+
pub fn invalid_argument() -> Error {
85+
Errno::EINVAL
86+
}
87+
5188
pub fn last() -> Self {
5289
last()
5390
}
@@ -64,25 +101,30 @@ impl Errno {
64101
clear()
65102
}
66103

67-
pub(crate) fn result2<S: ErrnoSentinel + PartialEq<S>>(value: S)
68-
-> std::result::Result<S, Self>
69-
{
70-
if value == S::sentinel() {
71-
Err(Self::last())
72-
} else {
73-
Ok(value)
74-
}
75-
}
76-
77104
/// Returns `Ok(value)` if it does not contain the sentinel value. This
78105
/// should not be used when `-1` is not the errno sentinel value.
79106
pub fn result<S: ErrnoSentinel + PartialEq<S>>(value: S) -> Result<S> {
80107
if value == S::sentinel() {
81-
Err(Error::from(Self::last()))
108+
Err(Self::last())
82109
} else {
83110
Ok(value)
84111
}
85112
}
113+
114+
/// Backwards compatibility hack for Nix <= 0.21.0 users
115+
///
116+
/// In older versions of Nix, `Error::Sys` was an enum variant. Now it's a
117+
/// function, which is compatible with most of the former use cases of the
118+
/// enum variant. But you should use `Error(Errno::...)` instead.
119+
#[deprecated(
120+
since = "0.22.0",
121+
note = "Use Errno::... instead"
122+
)]
123+
#[allow(non_snake_case)]
124+
#[inline]
125+
pub fn Sys(errno: Errno) -> Error {
126+
errno
127+
}
86128
}
87129

88130
/// The sentinel value indicates that a function failed and more detailed
@@ -125,6 +167,16 @@ impl From<Errno> for io::Error {
125167
}
126168
}
127169

170+
impl TryFrom<io::Error> for Errno {
171+
type Error = io::Error;
172+
173+
fn try_from(ioerror: io::Error) -> std::result::Result<Self, io::Error> {
174+
ioerror.raw_os_error()
175+
.map(Errno::from_i32)
176+
.ok_or(ioerror)
177+
}
178+
}
179+
128180
fn last() -> Errno {
129181
Errno::from_i32(errno())
130182
}

src/lib.rs

Lines changed: 4 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,15 @@ pub mod unistd;
7979

8080
use libc::{c_char, PATH_MAX};
8181

82-
use std::convert::TryFrom;
83-
use std::{error, fmt, io, ptr, result};
82+
use std::{ptr, result};
8483
use std::ffi::{CStr, OsStr};
8584
use std::os::unix::ffi::OsStrExt;
8685
use std::path::{Path, PathBuf};
8786

88-
use errno::{Errno, ErrnoSentinel};
87+
use errno::Errno;
8988

9089
/// Nix Result Type
91-
pub type Result<T> = result::Result<T, Error>;
90+
pub type Result<T> = result::Result<T, Errno>;
9291

9392
/// Nix's main error type.
9493
///
@@ -100,111 +99,7 @@ pub type Result<T> = result::Result<T, Error>;
10099
/// * Small size
101100
/// * Represents all of the system's errnos, instead of just the most common
102101
/// ones.
103-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
104-
pub struct Error(pub Errno);
105-
106-
impl Error {
107-
/// Convert this `Error` to an [`Errno`](enum.Errno.html).
108-
///
109-
/// # Example
110-
///
111-
/// ```
112-
/// # use nix::Error;
113-
/// # use nix::errno::Errno;
114-
/// let e = Error::from(Errno::EPERM);
115-
/// assert_eq!(Some(Errno::EPERM), e.as_errno());
116-
/// ```
117-
#[deprecated(
118-
since = "0.22.0",
119-
note = "Use Error::into<Errno> instead"
120-
)]
121-
pub fn as_errno(self) -> Option<Errno> {
122-
Some(self.0)
123-
}
124-
125-
/// Create a nix Error from a given errno
126-
#[deprecated(
127-
since = "0.22.0",
128-
note = "Use Error::from instead"
129-
)]
130-
pub fn from_errno(errno: Errno) -> Error {
131-
Error::from(errno)
132-
}
133-
134-
/// Get the current errno and convert it to a nix Error
135-
pub fn last() -> Error {
136-
Error::from(Errno::last())
137-
}
138-
139-
/// Create a new invalid argument error (`EINVAL`)
140-
#[deprecated(
141-
since = "0.22.0",
142-
note = "Use Error::from(Errno::EINVAL) instead"
143-
)]
144-
pub fn invalid_argument() -> Error {
145-
Error::from(Errno::EINVAL)
146-
}
147-
148-
/// Returns `Ok(value)` if it does not contain the sentinel value. This
149-
/// should not be used when `-1` is not the errno sentinel value.
150-
pub(crate) fn result<S: ErrnoSentinel + PartialEq<S>>(value: S)
151-
-> std::result::Result<S, Error>
152-
{
153-
Errno::result2(value).map_err(Self::from)
154-
}
155-
156-
/// Backwards compatibility hack for Nix <= 0.21.0 users
157-
///
158-
/// In older versions of Nix, `Error::Sys` was an enum variant. Now it's a
159-
/// function, which is compatible with most of the former use cases of the
160-
/// enum variant. But you should use `Error(Errno::...)` instead.
161-
#[deprecated(
162-
since = "0.22.0",
163-
note = "Use Error(Errno::...) instead"
164-
)]
165-
#[allow(non_snake_case)]
166-
#[inline]
167-
pub fn Sys(errno: Errno) -> Error {
168-
Error::from(errno)
169-
}
170-
}
171-
172-
impl fmt::Display for Error {
173-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
174-
write!(f, "{:?}: {}", self.0, self.0.desc())
175-
}
176-
}
177-
178-
impl error::Error for Error {}
179-
180-
impl From<Errno> for Error {
181-
fn from(errno: Errno) -> Self {
182-
Self(errno)
183-
}
184-
}
185-
186-
impl From<Error> for Errno {
187-
fn from(error: Error) -> Self {
188-
error.0
189-
}
190-
}
191-
192-
impl TryFrom<io::Error> for Error {
193-
type Error = io::Error;
194-
195-
fn try_from(ioerror: io::Error) -> std::result::Result<Self, io::Error> {
196-
ioerror.raw_os_error()
197-
.map(Errno::from_i32)
198-
.map(Error::from)
199-
.ok_or(ioerror)
200-
}
201-
}
202-
203-
impl From<Error> for io::Error {
204-
fn from(error: Error) -> Self {
205-
Self::from_raw_os_error(error.0 as i32)
206-
}
207-
}
102+
pub type Error = Errno;
208103

209104
pub trait NixPath {
210105
fn is_empty(&self) -> bool;

src/mount/bsd.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{
2+
Error,
23
Errno,
34
NixPath,
45
Result,
@@ -99,7 +100,7 @@ libc_bitflags!(
99100
/// by `nmount(2)`.
100101
#[derive(Debug)]
101102
pub struct NmountError {
102-
errno: Errno,
103+
errno: Error,
103104
errmsg: Option<String>
104105
}
105106

@@ -109,14 +110,14 @@ impl NmountError {
109110
self.errmsg.as_deref()
110111
}
111112

112-
/// Returns the inner [`Errno`]
113-
pub fn errno(&self) -> Errno {
113+
/// Returns the inner [`Error`]
114+
pub fn error(&self) -> Error {
114115
self.errno
115116
}
116117

117-
fn new(errno: Errno, errmsg: Option<&CStr>) -> Self {
118+
fn new(error: Error, errmsg: Option<&CStr>) -> Self {
118119
Self {
119-
errno,
120+
errno: error,
120121
errmsg: errmsg.map(CStr::to_string_lossy).map(Cow::into_owned)
121122
}
122123
}

src/pty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl Drop for PtyMaster {
7070
// condition, which can cause confusing errors for future I/O
7171
// operations.
7272
let e = unistd::close(self.0);
73-
if e == Err(Error(Errno::EBADF)) {
73+
if e == Err(Errno::EBADF) {
7474
panic!("Closing an invalid file descriptor!");
7575
};
7676
}

src/sys/aio.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -983,13 +983,13 @@ impl<'a> LioCb<'a> {
983983
// aiocb is complete; collect its status and don't resubmit
984984
self.results[i] = Some(a.aio_return_unpinned());
985985
},
986-
Err(Error(Errno::EAGAIN)) => {
986+
Err(Errno::EAGAIN) => {
987987
self.list.push(a as *mut AioCb<'a> as *mut libc::aiocb);
988988
},
989-
Err(Error(Errno::EINPROGRESS)) => {
989+
Err(Errno::EINPROGRESS) => {
990990
// aiocb is was successfully queued; no need to do anything
991991
},
992-
Err(Error(Errno::EINVAL)) => panic!(
992+
Err(Errno::EINVAL) => panic!(
993993
"AioCb was never submitted, or already finalized"),
994994
_ => unreachable!()
995995
}

src/sys/ptrace/linux.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use cfg_if::cfg_if;
44
use std::{mem, ptr};
5-
use crate::{Error, Result};
5+
use crate::Result;
66
use crate::errno::Errno;
77
use libc::{self, c_void, c_long, siginfo_t};
88
use crate::unistd::Pid;
@@ -180,7 +180,7 @@ fn ptrace_peek(request: Request, pid: Pid, addr: AddressType, data: *mut c_void)
180180
libc::ptrace(request as RequestType, libc::pid_t::from(pid), addr, data)
181181
};
182182
match Errno::result(ret) {
183-
Ok(..) | Err(Error(Errno::UnknownErrno)) => Ok(ret),
183+
Ok(..) | Err(Errno::UnknownErrno) => Ok(ret),
184184
err @ Err(..) => err,
185185
}
186186
}

src/sys/signalfd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl SignalFd {
108108
match res {
109109
Ok(SIGNALFD_SIGINFO_SIZE) => Ok(Some(unsafe { mem::transmute(buffer.assume_init()) })),
110110
Ok(_) => unreachable!("partial read on signalfd"),
111-
Err(Error(Errno::EAGAIN)) => Ok(None),
111+
Err(Errno::EAGAIN) => Ok(None),
112112
Err(error) => Err(error)
113113
}
114114
}

src/sys/socket/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1572,7 +1572,7 @@ pub fn recvfrom(sockfd: RawFd, buf: &mut [u8])
15721572
&mut len as *mut socklen_t))? as usize;
15731573

15741574
match sockaddr_storage_to_addr(&addr, len as usize) {
1575-
Err(Error(Errno::ENOTCONN)) => Ok((ret, None)),
1575+
Err(Errno::ENOTCONN) => Ok((ret, None)),
15761576
Ok(addr) => Ok((ret, Some(addr))),
15771577
Err(e) => Err(e)
15781578
}

src/sys/timerfd.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
//! ```
3131
use crate::sys::time::TimeSpec;
3232
use crate::unistd::read;
33-
use crate::{errno::Errno, Error, Result};
33+
use crate::{errno::Errno, Result};
3434
use bitflags::bitflags;
3535
use libc::c_int;
3636
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
@@ -259,7 +259,7 @@ impl TimerFd {
259259
loop {
260260
if let Err(e) = read(self.fd, &mut [0u8; 8]) {
261261
match e {
262-
Error(Errno::EINTR) => continue,
262+
Errno::EINTR => continue,
263263
_ => return Err(e),
264264
}
265265
} else {
@@ -277,7 +277,7 @@ impl Drop for TimerFd {
277277
let result = Errno::result(unsafe {
278278
libc::close(self.fd)
279279
});
280-
if let Err(Error(Errno::EBADF)) = result {
280+
if let Err(Errno::EBADF) = result {
281281
panic!("close of TimerFd encountered EBADF");
282282
}
283283
}

src/unistd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ pub fn getgroups() -> Result<Vec<Gid>> {
14321432
unsafe { groups.set_len(s as usize) };
14331433
return Ok(groups);
14341434
},
1435-
Err(Error(Errno::EINVAL)) => {
1435+
Err(Errno::EINVAL) => {
14361436
// EINVAL indicates that the buffer size was too
14371437
// small, resize it up to ngroups_max as limit.
14381438
reserve_double_buffer_size(&mut groups, ngroups_max)

0 commit comments

Comments
 (0)