Skip to content

Commit 0b37bb2

Browse files
committed
Redefine ErrorKind::Other and stop using it in std.
1 parent cdbe288 commit 0b37bb2

File tree

24 files changed

+89
-75
lines changed

24 files changed

+89
-75
lines changed

library/std/src/fs.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,7 @@ impl OpenOptions {
880880
/// This function will return an error under a number of different
881881
/// circumstances. Some of these error conditions are listed here, together
882882
/// with their [`io::ErrorKind`]. The mapping to [`io::ErrorKind`]s is not
883-
/// part of the compatibility contract of the function, especially the
884-
/// [`Other`] kind might change to more specific kinds in the future.
883+
/// part of the compatibility contract of the function.
885884
///
886885
/// * [`NotFound`]: The specified file does not exist and neither `create`
887886
/// or `create_new` is set.
@@ -895,9 +894,11 @@ impl OpenOptions {
895894
/// exists.
896895
/// * [`InvalidInput`]: Invalid combinations of open options (truncate
897896
/// without write access, no access mode set, etc.).
898-
/// * [`Other`]: One of the directory components of the specified file path
897+
///
898+
/// The following errors don't match any existing [`io::ErrorKind`] at the moment:
899+
/// * One of the directory components of the specified file path
899900
/// was not, in fact, a directory.
900-
/// * [`Other`]: Filesystem-level errors: full disk, write permission
901+
/// * Filesystem-level errors: full disk, write permission
901902
/// requested on a read-only file system, exceeded disk quota, too many
902903
/// open files, too long filename, too many symbolic links in the
903904
/// specified path (Unix-like systems only), etc.
@@ -913,7 +914,6 @@ impl OpenOptions {
913914
/// [`AlreadyExists`]: io::ErrorKind::AlreadyExists
914915
/// [`InvalidInput`]: io::ErrorKind::InvalidInput
915916
/// [`NotFound`]: io::ErrorKind::NotFound
916-
/// [`Other`]: io::ErrorKind::Other
917917
/// [`PermissionDenied`]: io::ErrorKind::PermissionDenied
918918
#[stable(feature = "rust1", since = "1.0.0")]
919919
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
@@ -2190,7 +2190,7 @@ impl DirBuilder {
21902190
Some(p) => self.create_dir_all(p)?,
21912191
None => {
21922192
return Err(io::Error::new_const(
2193-
io::ErrorKind::Other,
2193+
io::ErrorKind::Unknown,
21942194
&"failed to create whole tree",
21952195
));
21962196
}

library/std/src/fs/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,7 @@ fn metadata_access_times() {
13291329
match (a.created(), b.created()) {
13301330
(Ok(t1), Ok(t2)) => assert!(t1 <= t2),
13311331
(Err(e1), Err(e2))
1332-
if e1.kind() == ErrorKind::Other && e2.kind() == ErrorKind::Other
1332+
if e1.kind() == ErrorKind::Unknown && e2.kind() == ErrorKind::Unknown
13331333
|| e1.kind() == ErrorKind::Unsupported
13341334
&& e2.kind() == ErrorKind::Unsupported => {}
13351335
(a, b) => {

library/std/src/io/error.rs

+24-10
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,6 @@ pub enum ErrorKind {
163163
/// Interrupted operations can typically be retried.
164164
#[stable(feature = "rust1", since = "1.0.0")]
165165
Interrupted,
166-
/// Any I/O error not part of this list.
167-
///
168-
/// Errors that are `Other` now may move to a different or a new
169-
/// [`ErrorKind`] variant in the future. It is not recommended to match
170-
/// an error against `Other` and to expect any additional characteristics,
171-
/// e.g., a specific [`Error::raw_os_error`] return value.
172-
#[stable(feature = "rust1", since = "1.0.0")]
173-
Other,
174166

175167
/// An error returned when an operation could not be completed because an
176168
/// "end of file" was reached prematurely.
@@ -180,6 +172,18 @@ pub enum ErrorKind {
180172
/// read.
181173
#[stable(feature = "read_exact", since = "1.6.0")]
182174
UnexpectedEof,
175+
/// A custom error that does not fall under any other I/O error kind.
176+
///
177+
/// This can be used to construct your own [`Error`]s that do not match any
178+
/// [`ErrorKind`].
179+
///
180+
/// This [`ErrorKind`] is not used by the standard library.
181+
///
182+
/// Errors from the standard library that do not fall under any of the I/O
183+
/// error kinds cannot be `match`ed on, and will only match a wildcard (`_`) pattern.
184+
/// New [`ErrorKind`]s might be added in the future for some of those.
185+
#[stable(feature = "rust1", since = "1.0.0")]
186+
Other,
183187

184188
/// This operation is unsupported on this platform.
185189
///
@@ -191,6 +195,15 @@ pub enum ErrorKind {
191195
/// to allocate enough memory.
192196
#[stable(feature = "out_of_memory_error", since = "1.54.0")]
193197
OutOfMemory,
198+
199+
/// Any I/O error from the standard library that's not part of this list.
200+
///
201+
/// Errors that are `Unknown` now may move to a different or a new
202+
/// [`ErrorKind`] variant in the future. It is not recommended to match
203+
/// an error against `Unknown`; use a wildcard match (`_`) instead.
204+
#[unstable(feature = "io_error_unknown", issue = "none")]
205+
#[doc(hidden)]
206+
Unknown,
194207
}
195208

196209
impl ErrorKind {
@@ -212,10 +225,11 @@ impl ErrorKind {
212225
ErrorKind::TimedOut => "timed out",
213226
ErrorKind::WriteZero => "write zero",
214227
ErrorKind::Interrupted => "operation interrupted",
215-
ErrorKind::Other => "other os error",
216228
ErrorKind::UnexpectedEof => "unexpected end of file",
217229
ErrorKind::Unsupported => "unsupported",
218230
ErrorKind::OutOfMemory => "out of memory",
231+
ErrorKind::Other => "other error",
232+
ErrorKind::Unknown => "other os error",
219233
}
220234
}
221235
}
@@ -538,7 +552,7 @@ impl Error {
538552
/// }
539553
///
540554
/// fn main() {
541-
/// // Will print "Other".
555+
/// // Will print "Unknown".
542556
/// print_error(Error::last_os_error());
543557
/// // Will print "AddrInUse".
544558
/// print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));

library/std/src/io/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,7 @@ pub trait Write {
15921592
if output.error.is_err() {
15931593
output.error
15941594
} else {
1595-
Err(Error::new_const(ErrorKind::Other, &"formatter error"))
1595+
Err(Error::new_const(ErrorKind::Unknown, &"formatter error"))
15961596
}
15971597
}
15981598
}

library/std/src/net/tcp/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ fn double_bind() {
342342
Err(e) => {
343343
assert!(
344344
e.kind() == ErrorKind::ConnectionRefused
345-
|| e.kind() == ErrorKind::Other
345+
|| e.kind() == ErrorKind::Unknown
346346
|| e.kind() == ErrorKind::AddrInUse,
347347
"unknown error: {} {:?}",
348348
e,

library/std/src/os/wasi/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -532,5 +532,5 @@ pub fn symlink_path<P: AsRef<Path>, U: AsRef<Path>>(old_path: P, new_path: U) ->
532532
}
533533

534534
fn osstr2str(f: &OsStr) -> io::Result<&str> {
535-
f.to_str().ok_or_else(|| io::Error::new_const(io::ErrorKind::Other, &"input must be utf-8"))
535+
f.to_str().ok_or_else(|| io::Error::new_const(io::ErrorKind::Unknown, &"input must be utf-8"))
536536
}

library/std/src/process.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1658,8 +1658,7 @@ impl Child {
16581658
/// Forces the child process to exit. If the child has already exited, an [`InvalidInput`]
16591659
/// error is returned.
16601660
///
1661-
/// The mapping to [`ErrorKind`]s is not part of the compatibility contract of the function,
1662-
/// especially the [`Other`] kind might change to more specific kinds in the future.
1661+
/// The mapping to [`ErrorKind`]s is not part of the compatibility contract of the function.
16631662
///
16641663
/// This is equivalent to sending a SIGKILL on Unix platforms.
16651664
///
@@ -1680,7 +1679,6 @@ impl Child {
16801679
///
16811680
/// [`ErrorKind`]: io::ErrorKind
16821681
/// [`InvalidInput`]: io::ErrorKind::InvalidInput
1683-
/// [`Other`]: io::ErrorKind::Other
16841682
#[stable(feature = "process", since = "1.0.0")]
16851683
pub fn kill(&mut self) -> io::Result<()> {
16861684
self.handle.kill()

library/std/src/sys/hermit/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
149149
x if x == 1 as i32 => ErrorKind::PermissionDenied,
150150
x if x == 32 as i32 => ErrorKind::BrokenPipe,
151151
x if x == 110 as i32 => ErrorKind::TimedOut,
152-
_ => ErrorKind::Other,
152+
_ => ErrorKind::Unknown,
153153
}
154154
}
155155

library/std/src/sys/hermit/net.rs

+22-21
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::time::Duration;
1515
pub fn init() -> io::Result<()> {
1616
if abi::network_init() < 0 {
1717
return Err(io::Error::new_const(
18-
ErrorKind::Other,
18+
ErrorKind::Unknown,
1919
&"Unable to initialize network interface",
2020
));
2121
}
@@ -51,7 +51,7 @@ impl TcpStream {
5151
match abi::tcpstream::connect(addr.ip().to_string().as_bytes(), addr.port(), None) {
5252
Ok(handle) => Ok(TcpStream(Arc::new(Socket(handle)))),
5353
_ => Err(io::Error::new_const(
54-
ErrorKind::Other,
54+
ErrorKind::Unknown,
5555
&"Unable to initiate a connection on a socket",
5656
)),
5757
}
@@ -65,44 +65,44 @@ impl TcpStream {
6565
) {
6666
Ok(handle) => Ok(TcpStream(Arc::new(Socket(handle)))),
6767
_ => Err(io::Error::new_const(
68-
ErrorKind::Other,
68+
ErrorKind::Unknown,
6969
&"Unable to initiate a connection on a socket",
7070
)),
7171
}
7272
}
7373

7474
pub fn set_read_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
7575
abi::tcpstream::set_read_timeout(*self.0.as_inner(), duration.map(|d| d.as_millis() as u64))
76-
.map_err(|_| io::Error::new_const(ErrorKind::Other, &"Unable to set timeout value"))
76+
.map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"Unable to set timeout value"))
7777
}
7878

7979
pub fn set_write_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
8080
abi::tcpstream::set_write_timeout(
8181
*self.0.as_inner(),
8282
duration.map(|d| d.as_millis() as u64),
8383
)
84-
.map_err(|_| io::Error::new_const(ErrorKind::Other, &"Unable to set timeout value"))
84+
.map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"Unable to set timeout value"))
8585
}
8686

8787
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
8888
let duration = abi::tcpstream::get_read_timeout(*self.0.as_inner()).map_err(|_| {
89-
io::Error::new_const(ErrorKind::Other, &"Unable to determine timeout value")
89+
io::Error::new_const(ErrorKind::Unknown, &"Unable to determine timeout value")
9090
})?;
9191

9292
Ok(duration.map(|d| Duration::from_millis(d)))
9393
}
9494

9595
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
9696
let duration = abi::tcpstream::get_write_timeout(*self.0.as_inner()).map_err(|_| {
97-
io::Error::new_const(ErrorKind::Other, &"Unable to determine timeout value")
97+
io::Error::new_const(ErrorKind::Unknown, &"Unable to determine timeout value")
9898
})?;
9999

100100
Ok(duration.map(|d| Duration::from_millis(d)))
101101
}
102102

103103
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
104104
abi::tcpstream::peek(*self.0.as_inner(), buf)
105-
.map_err(|_| io::Error::new_const(ErrorKind::Other, &"set_nodelay failed"))
105+
.map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"set_nodelay failed"))
106106
}
107107

108108
pub fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
@@ -113,8 +113,9 @@ impl TcpStream {
113113
let mut size: usize = 0;
114114

115115
for i in ioslice.iter_mut() {
116-
let ret = abi::tcpstream::read(*self.0.as_inner(), &mut i[0..])
117-
.map_err(|_| io::Error::new_const(ErrorKind::Other, &"Unable to read on socket"))?;
116+
let ret = abi::tcpstream::read(*self.0.as_inner(), &mut i[0..]).map_err(|_| {
117+
io::Error::new_const(ErrorKind::Unknown, &"Unable to read on socket")
118+
})?;
118119

119120
if ret != 0 {
120121
size += ret;
@@ -138,7 +139,7 @@ impl TcpStream {
138139

139140
for i in ioslice.iter() {
140141
size += abi::tcpstream::write(*self.0.as_inner(), i).map_err(|_| {
141-
io::Error::new_const(ErrorKind::Other, &"Unable to write on socket")
142+
io::Error::new_const(ErrorKind::Unknown, &"Unable to write on socket")
142143
})?;
143144
}
144145

@@ -152,13 +153,13 @@ impl TcpStream {
152153

153154
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
154155
let (ipaddr, port) = abi::tcpstream::peer_addr(*self.0.as_inner())
155-
.map_err(|_| io::Error::new_const(ErrorKind::Other, &"peer_addr failed"))?;
156+
.map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"peer_addr failed"))?;
156157

157158
let saddr = match ipaddr {
158159
Ipv4(ref addr) => SocketAddr::new(IpAddr::V4(Ipv4Addr::from(addr.0)), port),
159160
Ipv6(ref addr) => SocketAddr::new(IpAddr::V6(Ipv6Addr::from(addr.0)), port),
160161
_ => {
161-
return Err(io::Error::new_const(ErrorKind::Other, &"peer_addr failed"));
162+
return Err(io::Error::new_const(ErrorKind::Unknown, &"peer_addr failed"));
162163
}
163164
};
164165

@@ -171,7 +172,7 @@ impl TcpStream {
171172

172173
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
173174
abi::tcpstream::shutdown(*self.0.as_inner(), how as i32)
174-
.map_err(|_| io::Error::new_const(ErrorKind::Other, &"unable to shutdown socket"))
175+
.map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"unable to shutdown socket"))
175176
}
176177

177178
pub fn duplicate(&self) -> io::Result<TcpStream> {
@@ -180,22 +181,22 @@ impl TcpStream {
180181

181182
pub fn set_nodelay(&self, mode: bool) -> io::Result<()> {
182183
abi::tcpstream::set_nodelay(*self.0.as_inner(), mode)
183-
.map_err(|_| io::Error::new_const(ErrorKind::Other, &"set_nodelay failed"))
184+
.map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"set_nodelay failed"))
184185
}
185186

186187
pub fn nodelay(&self) -> io::Result<bool> {
187188
abi::tcpstream::nodelay(*self.0.as_inner())
188-
.map_err(|_| io::Error::new_const(ErrorKind::Other, &"nodelay failed"))
189+
.map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"nodelay failed"))
189190
}
190191

191192
pub fn set_ttl(&self, tll: u32) -> io::Result<()> {
192193
abi::tcpstream::set_tll(*self.0.as_inner(), tll)
193-
.map_err(|_| io::Error::new_const(ErrorKind::Other, &"unable to set TTL"))
194+
.map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"unable to set TTL"))
194195
}
195196

196197
pub fn ttl(&self) -> io::Result<u32> {
197198
abi::tcpstream::get_tll(*self.0.as_inner())
198-
.map_err(|_| io::Error::new_const(ErrorKind::Other, &"unable to get TTL"))
199+
.map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"unable to get TTL"))
199200
}
200201

201202
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
@@ -204,7 +205,7 @@ impl TcpStream {
204205

205206
pub fn set_nonblocking(&self, mode: bool) -> io::Result<()> {
206207
abi::tcpstream::set_nonblocking(*self.0.as_inner(), mode)
207-
.map_err(|_| io::Error::new_const(ErrorKind::Other, &"unable to set blocking mode"))
208+
.map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"unable to set blocking mode"))
208209
}
209210
}
210211

@@ -230,12 +231,12 @@ impl TcpListener {
230231

231232
pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
232233
let (handle, ipaddr, port) = abi::tcplistener::accept(self.0.port())
233-
.map_err(|_| io::Error::new_const(ErrorKind::Other, &"accept failed"))?;
234+
.map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"accept failed"))?;
234235
let saddr = match ipaddr {
235236
Ipv4(ref addr) => SocketAddr::new(IpAddr::V4(Ipv4Addr::from(addr.0)), port),
236237
Ipv6(ref addr) => SocketAddr::new(IpAddr::V6(Ipv6Addr::from(addr.0)), port),
237238
_ => {
238-
return Err(io::Error::new_const(ErrorKind::Other, &"accept failed"));
239+
return Err(io::Error::new_const(ErrorKind::Unknown, &"accept failed"));
239240
}
240241
};
241242

library/std/src/sys/hermit/stdio.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl io::Write for Stdout {
4040
unsafe { len = abi::write(1, data.as_ptr() as *const u8, data.len()) }
4141

4242
if len < 0 {
43-
Err(io::Error::new_const(io::ErrorKind::Other, &"Stdout is not able to print"))
43+
Err(io::Error::new_const(io::ErrorKind::Unknown, &"Stdout is not able to print"))
4444
} else {
4545
Ok(len as usize)
4646
}
@@ -52,7 +52,7 @@ impl io::Write for Stdout {
5252
unsafe { len = abi::write(1, data.as_ptr() as *const u8, data.len()) }
5353

5454
if len < 0 {
55-
Err(io::Error::new_const(io::ErrorKind::Other, &"Stdout is not able to print"))
55+
Err(io::Error::new_const(io::ErrorKind::Unknown, &"Stdout is not able to print"))
5656
} else {
5757
Ok(len as usize)
5858
}
@@ -81,7 +81,7 @@ impl io::Write for Stderr {
8181
unsafe { len = abi::write(2, data.as_ptr() as *const u8, data.len()) }
8282

8383
if len < 0 {
84-
Err(io::Error::new_const(io::ErrorKind::Other, &"Stderr is not able to print"))
84+
Err(io::Error::new_const(io::ErrorKind::Unknown, &"Stderr is not able to print"))
8585
} else {
8686
Ok(len as usize)
8787
}
@@ -93,7 +93,7 @@ impl io::Write for Stderr {
9393
unsafe { len = abi::write(2, data.as_ptr() as *const u8, data.len()) }
9494

9595
if len < 0 {
96-
Err(io::Error::new_const(io::ErrorKind::Other, &"Stderr is not able to print"))
96+
Err(io::Error::new_const(io::ErrorKind::Unknown, &"Stderr is not able to print"))
9797
} else {
9898
Ok(len as usize)
9999
}

library/std/src/sys/hermit/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl Thread {
3737
// The thread failed to start and as a result p was not consumed. Therefore, it is
3838
// safe to reconstruct the box so that it gets deallocated.
3939
drop(Box::from_raw(p));
40-
Err(io::Error::new_const(io::ErrorKind::Other, &"Unable to create thread!"))
40+
Err(io::Error::new_const(io::ErrorKind::Unknown, &"Unable to create thread!"))
4141
} else {
4242
Ok(Thread { tid: tid })
4343
};

0 commit comments

Comments
 (0)