Skip to content

std: Stablize io::ErrorKind #23430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/librustc_back/tempdir.rs
Original file line number Diff line number Diff line change
@@ -61,12 +61,12 @@ impl TempDir {
let path = tmpdir.join(&leaf);
match fs::create_dir(&path) {
Ok(_) => return Ok(TempDir { path: Some(path) }),
Err(ref e) if e.kind() == ErrorKind::PathAlreadyExists => {}
Err(ref e) if e.kind() == ErrorKind::AlreadyExists => {}
Err(e) => return Err(e)
}
}

Err(Error::new(ErrorKind::PathAlreadyExists,
Err(Error::new(ErrorKind::AlreadyExists,
"too many temporary directories already exist",
None))
}
1 change: 0 additions & 1 deletion src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
@@ -35,7 +35,6 @@
#![feature(test)]
#![feature(unicode)]
#![feature(str_words)]
#![feature(io)]
#![feature(file_path)]
#![feature(path_ext)]
#![feature(path_relative_from)]
1 change: 0 additions & 1 deletion src/libserialize/lib.rs
Original file line number Diff line number Diff line change
@@ -31,7 +31,6 @@ Core encoding and decoding interfaces.
#![feature(collections)]
#![feature(core)]
#![feature(int_uint)]
#![feature(io)]
#![feature(old_path)]
#![feature(rustc_private)]
#![feature(staged_api)]
4 changes: 2 additions & 2 deletions src/libstd/fs/mod.rs
Original file line number Diff line number Diff line change
@@ -493,7 +493,7 @@ pub fn copy<P: AsPath, Q: AsPath>(from: P, to: Q) -> io::Result<u64> {
let from = from.as_path();
let to = to.as_path();
if !from.is_file() {
return Err(Error::new(ErrorKind::MismatchedFileTypeForOperation,
return Err(Error::new(ErrorKind::InvalidInput,
"the source path is not an existing file",
None))
}
@@ -1134,7 +1134,7 @@ mod tests {
let dir = &tmpdir.join("mkdir_error_twice");
check!(fs::create_dir(dir));
let e = fs::create_dir(dir).err().unwrap();
assert_eq!(e.kind(), ErrorKind::PathAlreadyExists);
assert_eq!(e.kind(), ErrorKind::AlreadyExists);
}

#[test]
4 changes: 2 additions & 2 deletions src/libstd/fs/tempdir.rs
Original file line number Diff line number Diff line change
@@ -68,12 +68,12 @@ impl TempDir {
let path = tmpdir.join(&leaf);
match fs::create_dir(&path) {
Ok(_) => return Ok(TempDir { path: Some(path) }),
Err(ref e) if e.kind() == ErrorKind::PathAlreadyExists => {}
Err(ref e) if e.kind() == ErrorKind::AlreadyExists => {}
Err(e) => return Err(e)
}
}

Err(Error::new(ErrorKind::PathAlreadyExists,
Err(Error::new(ErrorKind::AlreadyExists,
"too many temporary directories already exist",
None))
}
79 changes: 58 additions & 21 deletions src/libstd/io/error.rs
Original file line number Diff line number Diff line change
@@ -51,53 +51,77 @@ struct Custom {
}

/// A list specifying general categories of I/O error.
///
/// This list is intended to grow over time and it is not recommended to
/// exhaustively match against it.
#[derive(Copy, PartialEq, Eq, Clone, Debug)]
#[unstable(feature = "io",
reason = "the interaction between OS error codes and how they map to \
these names (as well as the names themselves) has not \
been thoroughly thought out")]
#[stable(feature = "rust1", since = "1.0.0")]
pub enum ErrorKind {
/// The file was not found.
FileNotFound,
/// The file permissions disallowed access to this file.
/// An entity was not found, often a file.
#[stable(feature = "rust1", since = "1.0.0")]
NotFound,
/// The operation lacked the necessary privileges to complete.
#[stable(feature = "rust1", since = "1.0.0")]
PermissionDenied,
/// The connection was refused by the remote server.
#[stable(feature = "rust1", since = "1.0.0")]
ConnectionRefused,
/// The connection was reset by the remote server.
#[stable(feature = "rust1", since = "1.0.0")]
ConnectionReset,
/// The connection was aborted (terminated) by the remote server.
#[stable(feature = "rust1", since = "1.0.0")]
ConnectionAborted,
/// The network operation failed because it was not connected yet.
#[stable(feature = "rust1", since = "1.0.0")]
NotConnected,
/// A socket address could not be bound because the address is already in
/// use elsewhere.
#[stable(feature = "rust1", since = "1.0.0")]
AddrInUse,
/// A nonexistent interface was requested or the requested address was not
/// local.
#[stable(feature = "rust1", since = "1.0.0")]
AddrNotAvailable,
/// The operation failed because a pipe was closed.
#[stable(feature = "rust1", since = "1.0.0")]
BrokenPipe,
/// A file already existed with that name.
PathAlreadyExists,
/// No file exists at that location.
PathDoesntExist,
/// The path did not specify the type of file that this operation required.
/// For example, attempting to copy a directory with the `fs::copy()`
/// operation will fail with this error.
MismatchedFileTypeForOperation,
/// The operation temporarily failed (for example, because a signal was
/// received), and retrying may succeed.
ResourceUnavailable,
/// A parameter was incorrect in a way that caused an I/O error not part of
/// this list.
/// An entity already exists, often a file.
#[stable(feature = "rust1", since = "1.0.0")]
AlreadyExists,
/// The operation needs to block to complete, but the blocking operation was
/// requested to not occur.
#[stable(feature = "rust1", since = "1.0.0")]
WouldBlock,
/// A parameter was incorrect.
#[stable(feature = "rust1", since = "1.0.0")]
InvalidInput,
/// The I/O operation's timeout expired, causing it to be canceled.
#[stable(feature = "rust1", since = "1.0.0")]
TimedOut,
/// An error returned when an operation could not be completed because a
/// call to `write` returned `Ok(0)`.
///
/// This typically means that an operation could only succeed if it wrote a
/// particular number of bytes but only a smaller number of bytes could be
/// written.
#[stable(feature = "rust1", since = "1.0.0")]
WriteZero,
/// This operation was interrupted
/// This operation was interrupted.
///
/// Interrupted operations can typically be retried.
#[stable(feature = "rust1", since = "1.0.0")]
Interrupted,
/// Any I/O error not part of this list.
#[stable(feature = "rust1", since = "1.0.0")]
Other,

/// Any I/O error not part of this list.
#[unstable(feature = "std_misc",
reason = "better expressed through extensible enums that this \
enum cannot be exhaustively matched against")]
#[doc(hidden)]
__Nonexhaustive,
}

impl Error {
@@ -134,6 +158,19 @@ impl Error {
Error { repr: Repr::Os(code) }
}

/// Returns the OS error that this error represents (if any).
///
/// If this `Error` was constructed via `last_os_error` then this function
/// will return `Some`, otherwise it will return `None`.
#[unstable(feature = "io", reason = "function was just added and the return \
type may become an abstract OS error")]
pub fn raw_os_error(&self) -> Option<i32> {
match self.repr {
Repr::Os(i) => Some(i),
Repr::Custom(..) => None,
}
}

/// Return the corresponding `ErrorKind` for this error.
#[stable(feature = "rust1", since = "1.0.0")]
pub fn kind(&self) -> ErrorKind {
13 changes: 8 additions & 5 deletions src/libstd/net/tcp.rs
Original file line number Diff line number Diff line change
@@ -273,17 +273,19 @@ mod tests {
match TcpListener::bind("1.1.1.1:9999") {
Ok(..) => panic!(),
Err(e) =>
// EADDRNOTAVAIL is mapped to ConnectionRefused
assert_eq!(e.kind(), ErrorKind::ConnectionRefused),
assert_eq!(e.kind(), ErrorKind::AddrNotAvailable),
}
}

#[test]
fn connect_error() {
match TcpStream::connect("0.0.0.0:1") {
Ok(..) => panic!(),
Err(e) => assert!((e.kind() == ErrorKind::ConnectionRefused)
|| (e.kind() == ErrorKind::InvalidInput)),
Err(e) => assert!(e.kind() == ErrorKind::ConnectionRefused ||
e.kind() == ErrorKind::InvalidInput ||
e.kind() == ErrorKind::AddrInUse ||
e.kind() == ErrorKind::AddrNotAvailable,
"bad error: {} {:?}", e, e.kind()),
}
}

@@ -535,7 +537,8 @@ mod tests {
Ok(..) => panic!(),
Err(e) => {
assert!(e.kind() == ErrorKind::ConnectionRefused ||
e.kind() == ErrorKind::Other,
e.kind() == ErrorKind::Other ||
e.kind() == ErrorKind::AddrInUse,
"unknown error: {} {:?}", e, e.kind());
}
}
2 changes: 1 addition & 1 deletion src/libstd/process.rs
Original file line number Diff line number Diff line change
@@ -668,7 +668,7 @@ mod tests {
#[test]
fn test_process_output_fail_to_start() {
match Command::new("/no-binary-by-this-name-should-exist").output() {
Err(e) => assert_eq!(e.kind(), ErrorKind::FileNotFound),
Err(e) => assert_eq!(e.kind(), ErrorKind::NotFound),
Ok(..) => panic!()
}
}
13 changes: 5 additions & 8 deletions src/libstd/sys/unix/mod.rs
Original file line number Diff line number Diff line change
@@ -139,22 +139,19 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
libc::EPIPE => ErrorKind::BrokenPipe,
libc::ENOTCONN => ErrorKind::NotConnected,
libc::ECONNABORTED => ErrorKind::ConnectionAborted,
libc::EADDRNOTAVAIL => ErrorKind::ConnectionRefused,
libc::EADDRINUSE => ErrorKind::ConnectionRefused,
libc::ENOENT => ErrorKind::FileNotFound,
libc::EISDIR => ErrorKind::InvalidInput,
libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
libc::EADDRINUSE => ErrorKind::AddrInUse,
libc::ENOENT => ErrorKind::NotFound,
libc::EINTR => ErrorKind::Interrupted,
libc::EINVAL => ErrorKind::InvalidInput,
libc::ENOTTY => ErrorKind::MismatchedFileTypeForOperation,
libc::ETIMEDOUT => ErrorKind::TimedOut,
libc::ECANCELED => ErrorKind::TimedOut,
libc::consts::os::posix88::EEXIST => ErrorKind::PathAlreadyExists,
libc::consts::os::posix88::EEXIST => ErrorKind::AlreadyExists,

// These two constants can have the same value on some systems,
// but different values on others, so we can't use a match
// clause
x if x == libc::EAGAIN || x == libc::EWOULDBLOCK =>
ErrorKind::ResourceUnavailable,
ErrorKind::WouldBlock,

_ => ErrorKind::Other,
}
14 changes: 5 additions & 9 deletions src/libstd/sys/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -149,25 +149,21 @@ pub fn decode_error_detailed(errno: i32) -> IoError {
pub fn decode_error_kind(errno: i32) -> ErrorKind {
match errno as libc::c_int {
libc::ERROR_ACCESS_DENIED => ErrorKind::PermissionDenied,
libc::ERROR_ALREADY_EXISTS => ErrorKind::PathAlreadyExists,
libc::ERROR_ALREADY_EXISTS => ErrorKind::AlreadyExists,
libc::ERROR_BROKEN_PIPE => ErrorKind::BrokenPipe,
libc::ERROR_FILE_NOT_FOUND => ErrorKind::FileNotFound,
libc::ERROR_INVALID_FUNCTION => ErrorKind::InvalidInput,
libc::ERROR_INVALID_HANDLE => ErrorKind::MismatchedFileTypeForOperation,
libc::ERROR_INVALID_NAME => ErrorKind::InvalidInput,
libc::ERROR_NOTHING_TO_TERMINATE => ErrorKind::InvalidInput,
libc::ERROR_FILE_NOT_FOUND => ErrorKind::NotFound,
libc::ERROR_NO_DATA => ErrorKind::BrokenPipe,
libc::ERROR_OPERATION_ABORTED => ErrorKind::TimedOut,

libc::WSAEACCES => ErrorKind::PermissionDenied,
libc::WSAEADDRINUSE => ErrorKind::ConnectionRefused,
libc::WSAEADDRNOTAVAIL => ErrorKind::ConnectionRefused,
libc::WSAEADDRINUSE => ErrorKind::AddrInUse,
libc::WSAEADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
libc::WSAECONNABORTED => ErrorKind::ConnectionAborted,
libc::WSAECONNREFUSED => ErrorKind::ConnectionRefused,
libc::WSAECONNRESET => ErrorKind::ConnectionReset,
libc::WSAEINVAL => ErrorKind::InvalidInput,
libc::WSAENOTCONN => ErrorKind::NotConnected,
libc::WSAEWOULDBLOCK => ErrorKind::ResourceUnavailable,
libc::WSAEWOULDBLOCK => ErrorKind::WouldBlock,

_ => ErrorKind::Other,
}