4 use crate::convert::From;
10 /// A specialized [`Result`] type for I/O operations.
12 /// This type is broadly used across [`std::io`] for any operation which may
15 /// This typedef is generally used to avoid writing out [`io::Error`] directly and
16 /// is otherwise a direct mapping to [`Result`].
18 /// While usual Rust style is to import types directly, aliases of [`Result`]
19 /// often are not, to make it easier to distinguish between them. [`Result`] is
20 /// generally assumed to be [`std::result::Result`][`Result`], and so users of this alias
21 /// will generally use `io::Result` instead of shadowing the [prelude]'s import
22 /// of [`std::result::Result`][`Result`].
24 /// [`std::io`]: crate::io
25 /// [`io::Error`]: Error
26 /// [`Result`]: crate::result::Result
27 /// [prelude]: crate::prelude
31 /// A convenience function that bubbles an `io::Result` to its caller:
36 /// fn get_string() -> io::Result<String> {
37 /// let mut buffer = String::new();
39 /// io::stdin().read_line(&mut buffer)?;
44 #[stable(feature = "rust1", since = "1.0.0")]
45 pub type Result<T> = result::Result<T, Error>;
47 /// The error type for I/O operations of the [`Read`], [`Write`], [`Seek`], and
48 /// associated traits.
50 /// Errors mostly originate from the underlying OS, but custom instances of
51 /// `Error` can be created with crafted error messages and a particular value of
54 /// [`Read`]: crate::io::Read
55 /// [`Write`]: crate::io::Write
56 /// [`Seek`]: crate::io::Seek
57 #[stable(feature = "rust1", since = "1.0.0")]
62 #[stable(feature = "rust1", since = "1.0.0")]
63 impl fmt::Debug for Error {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 fmt::Debug::fmt(&self.repr, f)
72 // &str is a fat pointer, but &&str is a thin pointer.
73 SimpleMessage(ErrorKind, &'static &'static str),
80 error: Box<dyn error::Error + Send + Sync>,
83 /// A list specifying general categories of I/O error.
85 /// This list is intended to grow over time and it is not recommended to
86 /// exhaustively match against it.
88 /// It is used with the [`io::Error`] type.
90 /// [`io::Error`]: Error
91 #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
92 #[stable(feature = "rust1", since = "1.0.0")]
96 /// An entity was not found, often a file.
97 #[stable(feature = "rust1", since = "1.0.0")]
99 /// The operation lacked the necessary privileges to complete.
100 #[stable(feature = "rust1", since = "1.0.0")]
102 /// The connection was refused by the remote server.
103 #[stable(feature = "rust1", since = "1.0.0")]
105 /// The connection was reset by the remote server.
106 #[stable(feature = "rust1", since = "1.0.0")]
108 /// The remote host is not reachable.
109 #[unstable(feature = "io_error_more", issue = "86442")]
111 /// The network containing the remote host is not reachable.
112 #[unstable(feature = "io_error_more", issue = "86442")]
114 /// The connection was aborted (terminated) by the remote server.
115 #[stable(feature = "rust1", since = "1.0.0")]
117 /// The network operation failed because it was not connected yet.
118 #[stable(feature = "rust1", since = "1.0.0")]
120 /// A socket address could not be bound because the address is already in
122 #[stable(feature = "rust1", since = "1.0.0")]
124 /// A nonexistent interface was requested or the requested address was not
126 #[stable(feature = "rust1", since = "1.0.0")]
128 /// The system's networking is down.
129 #[unstable(feature = "io_error_more", issue = "86442")]
131 /// The operation failed because a pipe was closed.
132 #[stable(feature = "rust1", since = "1.0.0")]
134 /// An entity already exists, often a file.
135 #[stable(feature = "rust1", since = "1.0.0")]
137 /// The operation needs to block to complete, but the blocking operation was
138 /// requested to not occur.
139 #[stable(feature = "rust1", since = "1.0.0")]
141 /// A filesystem object is, unexpectedly, not a directory.
143 /// For example, a filesystem path was specified where one of the intermediate directory
144 /// components was, in fact, a plain file.
145 #[unstable(feature = "io_error_more", issue = "86442")]
147 /// The filesystem object is, unexpectedly, a directory.
149 /// A directory was specified when a non-directory was expected.
150 #[unstable(feature = "io_error_more", issue = "86442")]
152 /// A non-empty directory was specified where an empty directory was expected.
153 #[unstable(feature = "io_error_more", issue = "86442")]
155 /// The filesystem or storage medium is read-only, but a write operation was attempted.
156 #[unstable(feature = "io_error_more", issue = "86442")]
158 /// Loop in the filesystem or IO subsystem; often, too many levels of symbolic links.
160 /// There was a loop (or excessively long chain) resolving a filesystem object
161 /// or file IO object.
163 /// On Unix this is usually the result of a symbolic link loop; or, of exceeding the
164 /// system-specific limit on the depth of symlink traversal.
165 #[unstable(feature = "io_error_more", issue = "86442")]
167 /// Stale network file handle.
169 /// With some network filesystems, notably NFS, an open file (or directory) can be invalidated
170 /// by problems with the network or server.
171 #[unstable(feature = "io_error_more", issue = "86442")]
172 StaleNetworkFileHandle,
173 /// A parameter was incorrect.
174 #[stable(feature = "rust1", since = "1.0.0")]
176 /// Data not valid for the operation were encountered.
178 /// Unlike [`InvalidInput`], this typically means that the operation
179 /// parameters were valid, however the error was caused by malformed
182 /// For example, a function that reads a file into a string will error with
183 /// `InvalidData` if the file's contents are not valid UTF-8.
185 /// [`InvalidInput`]: ErrorKind::InvalidInput
186 #[stable(feature = "io_invalid_data", since = "1.2.0")]
188 /// The I/O operation's timeout expired, causing it to be canceled.
189 #[stable(feature = "rust1", since = "1.0.0")]
191 /// An error returned when an operation could not be completed because a
192 /// call to [`write`] returned [`Ok(0)`].
194 /// This typically means that an operation could only succeed if it wrote a
195 /// particular number of bytes but only a smaller number of bytes could be
198 /// [`write`]: crate::io::Write::write
200 #[stable(feature = "rust1", since = "1.0.0")]
202 /// The underlying storage (typically, a filesystem) is full.
204 /// This does not include out of quota errors.
205 #[unstable(feature = "io_error_more", issue = "86442")]
207 /// Seek on unseekable file.
209 /// Seeking was attempted on an open file handle which is not suitable for seeking - for
210 /// example, on Unix, a named pipe opened with `File::open`.
211 #[unstable(feature = "io_error_more", issue = "86442")]
213 /// Filesystem quota was exceeded.
214 #[unstable(feature = "io_error_more", issue = "86442")]
215 FilesystemQuotaExceeded,
216 /// File larger than allowed or supported.
218 /// This might arise from a hard limit of the underlying filesystem or file access API, or from
219 /// an administratively imposed resource limitation. Simple disk full, and out of quota, have
220 /// their own errors.
221 #[unstable(feature = "io_error_more", issue = "86442")]
223 /// Resource is busy.
224 #[unstable(feature = "io_error_more", issue = "86442")]
226 /// Executable file is busy.
228 /// An attempt was made to write to a file which is also in use as a running program. (Not all
229 /// operating systems detect this situation.)
230 #[unstable(feature = "io_error_more", issue = "86442")]
232 /// Deadlock (avoided).
234 /// A file locking operation would result in deadlock. This situation is typically detected, if
235 /// at all, on a best-effort basis.
236 #[unstable(feature = "io_error_more", issue = "86442")]
238 /// Cross-device or cross-filesystem (hard) link or rename.
239 #[unstable(feature = "io_error_more", issue = "86442")]
241 /// Too many (hard) links to the same filesystem object.
243 /// The filesystem does not support making so many hardlinks to the same file.
244 #[unstable(feature = "io_error_more", issue = "86442")]
246 /// Filename too long.
248 /// The limit might be from the underlying filesystem or API, or an administratively imposed
250 #[unstable(feature = "io_error_more", issue = "86442")]
252 /// Program argument list too long.
254 /// When trying to run an external program, a system or process limit on the size of the
255 /// arguments would have been exceeded.
256 #[unstable(feature = "io_error_more", issue = "86442")]
258 /// This operation was interrupted.
260 /// Interrupted operations can typically be retried.
261 #[stable(feature = "rust1", since = "1.0.0")]
264 /// This operation is unsupported on this platform.
266 /// This means that the operation can never succeed.
267 #[stable(feature = "unsupported_error", since = "1.53.0")]
270 // ErrorKinds which are primarily categorisations for OS error
271 // codes should be added above.
273 /// An error returned when an operation could not be completed because an
274 /// "end of file" was reached prematurely.
276 /// This typically means that an operation could only succeed if it read a
277 /// particular number of bytes but only a smaller number of bytes could be
279 #[stable(feature = "read_exact", since = "1.6.0")]
282 /// An operation could not be completed, because it failed
283 /// to allocate enough memory.
284 #[stable(feature = "out_of_memory_error", since = "1.54.0")]
287 // "Unusual" error kinds which do not correspond simply to (sets
288 // of) OS error codes, should be added just above this comment.
289 // `Other` and `Uncategorised` should remain at the end:
291 /// A custom error that does not fall under any other I/O error kind.
293 /// This can be used to construct your own [`Error`]s that do not match any
296 /// This [`ErrorKind`] is not used by the standard library.
298 /// Errors from the standard library that do not fall under any of the I/O
299 /// error kinds cannot be `match`ed on, and will only match a wildcard (`_`) pattern.
300 /// New [`ErrorKind`]s might be added in the future for some of those.
301 #[stable(feature = "rust1", since = "1.0.0")]
304 /// Any I/O error from the standard library that's not part of this list.
306 /// Errors that are `Uncategorized` now may move to a different or a new
307 /// [`ErrorKind`] variant in the future. It is not recommended to match
308 /// an error against `Uncategorized`; use a wildcard match (`_`) instead.
309 #[unstable(feature = "io_error_uncategorized", issue = "none")]
315 pub(crate) fn as_str(&self) -> &'static str {
317 // Strictly alphabetical, please. (Sadly rustfmt cannot do this yet.)
319 AddrInUse => "address in use",
320 AddrNotAvailable => "address not available",
321 AlreadyExists => "entity already exists",
322 ArgumentListTooLong => "argument list too long",
323 BrokenPipe => "broken pipe",
324 ConnectionAborted => "connection aborted",
325 ConnectionRefused => "connection refused",
326 ConnectionReset => "connection reset",
327 CrossesDevices => "cross-device link or rename",
328 Deadlock => "deadlock",
329 DirectoryNotEmpty => "directory not empty",
330 ExecutableFileBusy => "executable file busy",
331 FileTooLarge => "file too large",
332 FilenameTooLong => "filename too long",
333 FilesystemLoop => "filesystem loop or indirection limit (e.g. symlink loop)",
334 FilesystemQuotaExceeded => "filesystem quota exceeded",
335 HostUnreachable => "host unreachable",
336 Interrupted => "operation interrupted",
337 InvalidData => "invalid data",
338 InvalidInput => "invalid input parameter",
339 IsADirectory => "is a directory",
340 NetworkDown => "network down",
341 NetworkUnreachable => "network unreachable",
342 NotADirectory => "not a directory",
343 NotConnected => "not connected",
344 NotFound => "entity not found",
345 NotSeekable => "seek on unseekable file",
346 Other => "other error",
347 OutOfMemory => "out of memory",
348 PermissionDenied => "permission denied",
349 ReadOnlyFilesystem => "read-only filesystem or storage medium",
350 ResourceBusy => "resource busy",
351 StaleNetworkFileHandle => "stale network file handle",
352 StorageFull => "no storage space",
353 TimedOut => "timed out",
354 TooManyLinks => "too many links",
355 Uncategorized => "uncategorized error",
356 UnexpectedEof => "unexpected end of file",
357 Unsupported => "unsupported",
358 WouldBlock => "operation would block",
359 WriteZero => "write zero",
364 #[stable(feature = "io_errorkind_display", since = "1.60.0")]
365 impl fmt::Display for ErrorKind {
366 /// Shows a human-readable description of the `ErrorKind`.
368 /// This is similar to `impl Display for Error`, but doesn't require first converting to Error.
372 /// use std::io::ErrorKind;
373 /// assert_eq!("entity not found", ErrorKind::NotFound.to_string());
375 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
376 fmt.write_str(self.as_str())
380 /// Intended for use for errors not exposed to the user, where allocating onto
381 /// the heap (for normal construction via Error::new) is too costly.
382 #[stable(feature = "io_error_from_errorkind", since = "1.14.0")]
383 impl From<ErrorKind> for Error {
384 /// Converts an [`ErrorKind`] into an [`Error`].
386 /// This conversion creates a new error with a simple representation of error kind.
391 /// use std::io::{Error, ErrorKind};
393 /// let not_found = ErrorKind::NotFound;
394 /// let error = Error::from(not_found);
395 /// assert_eq!("entity not found", format!("{}", error));
398 fn from(kind: ErrorKind) -> Error {
399 Error { repr: Repr::Simple(kind) }
404 /// Creates a new I/O error from a known kind of error as well as an
405 /// arbitrary error payload.
407 /// This function is used to generically create I/O errors which do not
408 /// originate from the OS itself. The `error` argument is an arbitrary
409 /// payload which will be contained in this [`Error`].
411 /// If no extra payload is required, use the `From` conversion from
417 /// use std::io::{Error, ErrorKind};
419 /// // errors can be created from strings
420 /// let custom_error = Error::new(ErrorKind::Other, "oh no!");
422 /// // errors can also be created from other errors
423 /// let custom_error2 = Error::new(ErrorKind::Interrupted, custom_error);
425 /// // creating an error without payload
426 /// let eof_error = Error::from(ErrorKind::UnexpectedEof);
428 #[stable(feature = "rust1", since = "1.0.0")]
429 pub fn new<E>(kind: ErrorKind, error: E) -> Error
431 E: Into<Box<dyn error::Error + Send + Sync>>,
433 Self::_new(kind, error.into())
436 /// Creates a new I/O error from an arbitrary error payload.
438 /// This function is used to generically create I/O errors which do not
439 /// originate from the OS itself. It is a shortcut for [`Error::new`]
440 /// with [`ErrorKind::Other`].
445 /// #![feature(io_error_other)]
447 /// use std::io::Error;
449 /// // errors can be created from strings
450 /// let custom_error = Error::other("oh no!");
452 /// // errors can also be created from other errors
453 /// let custom_error2 = Error::other(custom_error);
455 #[unstable(feature = "io_error_other", issue = "91946")]
456 pub fn other<E>(error: E) -> Error
458 E: Into<Box<dyn error::Error + Send + Sync>>,
460 Self::_new(ErrorKind::Other, error.into())
463 fn _new(kind: ErrorKind, error: Box<dyn error::Error + Send + Sync>) -> Error {
464 Error { repr: Repr::Custom(Box::new(Custom { kind, error })) }
467 /// Creates a new I/O error from a known kind of error as well as a
468 /// constant message.
470 /// This function does not allocate.
472 /// This function should maybe change to
473 /// `new_const<const MSG: &'static str>(kind: ErrorKind)`
474 /// in the future, when const generics allow that.
476 pub(crate) const fn new_const(kind: ErrorKind, message: &'static &'static str) -> Error {
477 Self { repr: Repr::SimpleMessage(kind, message) }
480 /// Returns an error representing the last OS error which occurred.
482 /// This function reads the value of `errno` for the target platform (e.g.
483 /// `GetLastError` on Windows) and will return a corresponding instance of
484 /// [`Error`] for the error code.
486 /// This should be called immediately after a call to a platform function,
487 /// otherwise the state of the error value is indeterminate. In particular,
488 /// other standard library functions may call platform functions that may
489 /// (or may not) reset the error value even if they succeed.
494 /// use std::io::Error;
496 /// let os_error = Error::last_os_error();
497 /// println!("last OS error: {:?}", os_error);
499 #[stable(feature = "rust1", since = "1.0.0")]
502 pub fn last_os_error() -> Error {
503 Error::from_raw_os_error(sys::os::errno() as i32)
506 /// Creates a new instance of an [`Error`] from a particular OS error code.
513 /// # if cfg!(target_os = "linux") {
516 /// let error = io::Error::from_raw_os_error(22);
517 /// assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
524 /// # if cfg!(windows) {
527 /// let error = io::Error::from_raw_os_error(10022);
528 /// assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
531 #[stable(feature = "rust1", since = "1.0.0")]
534 pub fn from_raw_os_error(code: i32) -> Error {
535 Error { repr: Repr::Os(code) }
538 /// Returns the OS error that this error represents (if any).
540 /// If this [`Error`] was constructed via [`last_os_error`] or
541 /// [`from_raw_os_error`], then this function will return [`Some`], otherwise
542 /// it will return [`None`].
544 /// [`last_os_error`]: Error::last_os_error
545 /// [`from_raw_os_error`]: Error::from_raw_os_error
550 /// use std::io::{Error, ErrorKind};
552 /// fn print_os_error(err: &Error) {
553 /// if let Some(raw_os_err) = err.raw_os_error() {
554 /// println!("raw OS error: {:?}", raw_os_err);
556 /// println!("Not an OS error");
561 /// // Will print "raw OS error: ...".
562 /// print_os_error(&Error::last_os_error());
563 /// // Will print "Not an OS error".
564 /// print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
567 #[stable(feature = "rust1", since = "1.0.0")]
570 pub fn raw_os_error(&self) -> Option<i32> {
572 Repr::Os(i) => Some(i),
573 Repr::Custom(..) => None,
574 Repr::Simple(..) => None,
575 Repr::SimpleMessage(..) => None,
579 /// Returns a reference to the inner error wrapped by this error (if any).
581 /// If this [`Error`] was constructed via [`new`] then this function will
582 /// return [`Some`], otherwise it will return [`None`].
584 /// [`new`]: Error::new
589 /// use std::io::{Error, ErrorKind};
591 /// fn print_error(err: &Error) {
592 /// if let Some(inner_err) = err.get_ref() {
593 /// println!("Inner error: {:?}", inner_err);
595 /// println!("No inner error");
600 /// // Will print "No inner error".
601 /// print_error(&Error::last_os_error());
602 /// // Will print "Inner error: ...".
603 /// print_error(&Error::new(ErrorKind::Other, "oh no!"));
606 #[stable(feature = "io_error_inner", since = "1.3.0")]
609 pub fn get_ref(&self) -> Option<&(dyn error::Error + Send + Sync + 'static)> {
611 Repr::Os(..) => None,
612 Repr::Simple(..) => None,
613 Repr::SimpleMessage(..) => None,
614 Repr::Custom(ref c) => Some(&*c.error),
618 /// Returns a mutable reference to the inner error wrapped by this error
621 /// If this [`Error`] was constructed via [`new`] then this function will
622 /// return [`Some`], otherwise it will return [`None`].
624 /// [`new`]: Error::new
629 /// use std::io::{Error, ErrorKind};
630 /// use std::{error, fmt};
631 /// use std::fmt::Display;
639 /// fn new() -> MyError {
641 /// v: "oh no!".to_string()
645 /// fn change_message(&mut self, new_message: &str) {
646 /// self.v = new_message.to_string();
650 /// impl error::Error for MyError {}
652 /// impl Display for MyError {
653 /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
654 /// write!(f, "MyError: {}", &self.v)
658 /// fn change_error(mut err: Error) -> Error {
659 /// if let Some(inner_err) = err.get_mut() {
660 /// inner_err.downcast_mut::<MyError>().unwrap().change_message("I've been changed!");
665 /// fn print_error(err: &Error) {
666 /// if let Some(inner_err) = err.get_ref() {
667 /// println!("Inner error: {}", inner_err);
669 /// println!("No inner error");
674 /// // Will print "No inner error".
675 /// print_error(&change_error(Error::last_os_error()));
676 /// // Will print "Inner error: ...".
677 /// print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
680 #[stable(feature = "io_error_inner", since = "1.3.0")]
683 pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Send + Sync + 'static)> {
685 Repr::Os(..) => None,
686 Repr::Simple(..) => None,
687 Repr::SimpleMessage(..) => None,
688 Repr::Custom(ref mut c) => Some(&mut *c.error),
692 /// Consumes the `Error`, returning its inner error (if any).
694 /// If this [`Error`] was constructed via [`new`] then this function will
695 /// return [`Some`], otherwise it will return [`None`].
697 /// [`new`]: Error::new
702 /// use std::io::{Error, ErrorKind};
704 /// fn print_error(err: Error) {
705 /// if let Some(inner_err) = err.into_inner() {
706 /// println!("Inner error: {}", inner_err);
708 /// println!("No inner error");
713 /// // Will print "No inner error".
714 /// print_error(Error::last_os_error());
715 /// // Will print "Inner error: ...".
716 /// print_error(Error::new(ErrorKind::Other, "oh no!"));
719 #[stable(feature = "io_error_inner", since = "1.3.0")]
720 #[must_use = "`self` will be dropped if the result is not used"]
722 pub fn into_inner(self) -> Option<Box<dyn error::Error + Send + Sync>> {
724 Repr::Os(..) => None,
725 Repr::Simple(..) => None,
726 Repr::SimpleMessage(..) => None,
727 Repr::Custom(c) => Some(c.error),
731 /// Returns the corresponding [`ErrorKind`] for this error.
736 /// use std::io::{Error, ErrorKind};
738 /// fn print_error(err: Error) {
739 /// println!("{:?}", err.kind());
743 /// // Will print "Uncategorized".
744 /// print_error(Error::last_os_error());
745 /// // Will print "AddrInUse".
746 /// print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
749 #[stable(feature = "rust1", since = "1.0.0")]
752 pub fn kind(&self) -> ErrorKind {
754 Repr::Os(code) => sys::decode_error_kind(code),
755 Repr::Custom(ref c) => c.kind,
756 Repr::Simple(kind) => kind,
757 Repr::SimpleMessage(kind, _) => kind,
762 impl fmt::Debug for Repr {
763 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
765 Repr::Os(code) => fmt
767 .field("code", &code)
768 .field("kind", &sys::decode_error_kind(code))
769 .field("message", &sys::os::error_string(code))
771 Repr::Custom(ref c) => fmt::Debug::fmt(&c, fmt),
772 Repr::Simple(kind) => fmt.debug_tuple("Kind").field(&kind).finish(),
773 Repr::SimpleMessage(kind, &message) => {
774 fmt.debug_struct("Error").field("kind", &kind).field("message", &message).finish()
780 #[stable(feature = "rust1", since = "1.0.0")]
781 impl fmt::Display for Error {
782 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
785 let detail = sys::os::error_string(code);
786 write!(fmt, "{} (os error {})", detail, code)
788 Repr::Custom(ref c) => c.error.fmt(fmt),
789 Repr::Simple(kind) => write!(fmt, "{}", kind.as_str()),
790 Repr::SimpleMessage(_, &msg) => msg.fmt(fmt),
795 #[stable(feature = "rust1", since = "1.0.0")]
796 impl error::Error for Error {
797 #[allow(deprecated, deprecated_in_future)]
798 fn description(&self) -> &str {
800 Repr::Os(..) | Repr::Simple(..) => self.kind().as_str(),
801 Repr::SimpleMessage(_, &msg) => msg,
802 Repr::Custom(ref c) => c.error.description(),
807 fn cause(&self) -> Option<&dyn error::Error> {
809 Repr::Os(..) => None,
810 Repr::Simple(..) => None,
811 Repr::SimpleMessage(..) => None,
812 Repr::Custom(ref c) => c.error.cause(),
816 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
818 Repr::Os(..) => None,
819 Repr::Simple(..) => None,
820 Repr::SimpleMessage(..) => None,
821 Repr::Custom(ref c) => c.error.source(),
826 fn _assert_error_is_sync_send() {
827 fn _is_sync_send<T: Sync + Send>() {}
828 _is_sync_send::<Error>();