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 /// A custom error that does not fall under any other I/O error kind.
266 /// This can be used to construct your own [`Error`]s that do not match any
269 /// This [`ErrorKind`] is not used by the standard library.
271 /// Errors from the standard library that do not fall under any of the I/O
272 /// error kinds cannot be `match`ed on, and will only match a wildcard (`_`) pattern.
273 /// New [`ErrorKind`]s might be added in the future for some of those.
274 #[stable(feature = "rust1", since = "1.0.0")]
277 /// An error returned when an operation could not be completed because an
278 /// "end of file" was reached prematurely.
280 /// This typically means that an operation could only succeed if it read a
281 /// particular number of bytes but only a smaller number of bytes could be
283 #[stable(feature = "read_exact", since = "1.6.0")]
286 /// This operation is unsupported on this platform.
288 /// This means that the operation can never succeed.
289 #[stable(feature = "unsupported_error", since = "1.53.0")]
292 /// An operation could not be completed, because it failed
293 /// to allocate enough memory.
294 #[stable(feature = "out_of_memory_error", since = "1.54.0")]
297 /// Any I/O error from the standard library that's not part of this list.
299 /// Errors that are `Uncategorized` now may move to a different or a new
300 /// [`ErrorKind`] variant in the future. It is not recommended to match
301 /// an error against `Uncategorized`; use a wildcard match (`_`) instead.
302 #[unstable(feature = "io_error_uncategorized", issue = "none")]
308 pub(crate) fn as_str(&self) -> &'
static str {
311 AddrInUse
=> "address in use",
312 AddrNotAvailable
=> "address not available",
313 AlreadyExists
=> "entity already exists",
314 ArgumentListTooLong
=> "argument list too long",
315 BrokenPipe
=> "broken pipe",
316 ResourceBusy
=> "resource busy",
317 ConnectionAborted
=> "connection aborted",
318 ConnectionRefused
=> "connection refused",
319 ConnectionReset
=> "connection reset",
320 CrossesDevices
=> "cross-device link or rename",
321 Deadlock
=> "deadlock",
322 DirectoryNotEmpty
=> "directory not empty",
323 ExecutableFileBusy
=> "executable file busy",
324 FilenameTooLong
=> "filename too long",
325 FilesystemQuotaExceeded
=> "filesystem quota exceeded",
326 FileTooLarge
=> "file too large",
327 HostUnreachable
=> "host unreachable",
328 Interrupted
=> "operation interrupted",
329 InvalidData
=> "invalid data",
330 InvalidInput
=> "invalid input parameter",
331 IsADirectory
=> "is a directory",
332 NetworkDown
=> "network down",
333 NetworkUnreachable
=> "network unreachable",
334 NotADirectory
=> "not a directory",
335 StorageFull
=> "no storage space",
336 NotConnected
=> "not connected",
337 NotFound
=> "entity not found",
338 Other
=> "other error",
339 OutOfMemory
=> "out of memory",
340 PermissionDenied
=> "permission denied",
341 ReadOnlyFilesystem
=> "read-only filesystem or storage medium",
342 StaleNetworkFileHandle
=> "stale network file handle",
343 FilesystemLoop
=> "filesystem loop or indirection limit (e.g. symlink loop)",
344 NotSeekable
=> "seek on unseekable file",
345 TimedOut
=> "timed out",
346 TooManyLinks
=> "too many links",
347 Uncategorized
=> "uncategorized error",
348 UnexpectedEof
=> "unexpected end of file",
349 Unsupported
=> "unsupported",
350 WouldBlock
=> "operation would block",
351 WriteZero
=> "write zero",
356 /// Intended for use for errors not exposed to the user, where allocating onto
357 /// the heap (for normal construction via Error::new) is too costly.
358 #[stable(feature = "io_error_from_errorkind", since = "1.14.0")]
359 impl From
<ErrorKind
> for Error
{
360 /// Converts an [`ErrorKind`] into an [`Error`].
362 /// This conversion allocates a new error with a simple representation of error kind.
367 /// use std::io::{Error, ErrorKind};
369 /// let not_found = ErrorKind::NotFound;
370 /// let error = Error::from(not_found);
371 /// assert_eq!("entity not found", format!("{}", error));
374 fn from(kind
: ErrorKind
) -> Error
{
375 Error { repr: Repr::Simple(kind) }
380 /// Creates a new I/O error from a known kind of error as well as an
381 /// arbitrary error payload.
383 /// This function is used to generically create I/O errors which do not
384 /// originate from the OS itself. The `error` argument is an arbitrary
385 /// payload which will be contained in this [`Error`].
390 /// use std::io::{Error, ErrorKind};
392 /// // errors can be created from strings
393 /// let custom_error = Error::new(ErrorKind::Other, "oh no!");
395 /// // errors can also be created from other errors
396 /// let custom_error2 = Error::new(ErrorKind::Interrupted, custom_error);
398 #[stable(feature = "rust1", since = "1.0.0")]
399 pub fn new
<E
>(kind
: ErrorKind
, error
: E
) -> Error
401 E
: Into
<Box
<dyn error
::Error
+ Send
+ Sync
>>,
403 Self::_new(kind
, error
.into())
406 fn _new(kind
: ErrorKind
, error
: Box
<dyn error
::Error
+ Send
+ Sync
>) -> Error
{
407 Error { repr: Repr::Custom(Box::new(Custom { kind, error }
)) }
410 /// Creates a new I/O error from a known kind of error as well as a
411 /// constant message.
413 /// This function does not allocate.
415 /// This function should maybe change to
416 /// `new_const<const MSG: &'static str>(kind: ErrorKind)`
417 /// in the future, when const generics allow that.
419 pub(crate) const fn new_const(kind
: ErrorKind
, message
: &'
static &'
static str) -> Error
{
420 Self { repr: Repr::SimpleMessage(kind, message) }
423 /// Returns an error representing the last OS error which occurred.
425 /// This function reads the value of `errno` for the target platform (e.g.
426 /// `GetLastError` on Windows) and will return a corresponding instance of
427 /// [`Error`] for the error code.
432 /// use std::io::Error;
434 /// println!("last OS error: {:?}", Error::last_os_error());
436 #[stable(feature = "rust1", since = "1.0.0")]
438 pub fn last_os_error() -> Error
{
439 Error
::from_raw_os_error(sys
::os
::errno() as i32)
442 /// Creates a new instance of an [`Error`] from a particular OS error code.
449 /// # if cfg!(target_os = "linux") {
452 /// let error = io::Error::from_raw_os_error(22);
453 /// assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
460 /// # if cfg!(windows) {
463 /// let error = io::Error::from_raw_os_error(10022);
464 /// assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
467 #[stable(feature = "rust1", since = "1.0.0")]
469 pub fn from_raw_os_error(code
: i32) -> Error
{
470 Error { repr: Repr::Os(code) }
473 /// Returns the OS error that this error represents (if any).
475 /// If this [`Error`] was constructed via [`last_os_error`] or
476 /// [`from_raw_os_error`], then this function will return [`Some`], otherwise
477 /// it will return [`None`].
479 /// [`last_os_error`]: Error::last_os_error
480 /// [`from_raw_os_error`]: Error::from_raw_os_error
485 /// use std::io::{Error, ErrorKind};
487 /// fn print_os_error(err: &Error) {
488 /// if let Some(raw_os_err) = err.raw_os_error() {
489 /// println!("raw OS error: {:?}", raw_os_err);
491 /// println!("Not an OS error");
496 /// // Will print "raw OS error: ...".
497 /// print_os_error(&Error::last_os_error());
498 /// // Will print "Not an OS error".
499 /// print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
502 #[stable(feature = "rust1", since = "1.0.0")]
504 pub fn raw_os_error(&self) -> Option
<i32> {
506 Repr
::Os(i
) => Some(i
),
507 Repr
::Custom(..) => None
,
508 Repr
::Simple(..) => None
,
509 Repr
::SimpleMessage(..) => None
,
513 /// Returns a reference to the inner error wrapped by this error (if any).
515 /// If this [`Error`] was constructed via [`new`] then this function will
516 /// return [`Some`], otherwise it will return [`None`].
518 /// [`new`]: Error::new
523 /// use std::io::{Error, ErrorKind};
525 /// fn print_error(err: &Error) {
526 /// if let Some(inner_err) = err.get_ref() {
527 /// println!("Inner error: {:?}", inner_err);
529 /// println!("No inner error");
534 /// // Will print "No inner error".
535 /// print_error(&Error::last_os_error());
536 /// // Will print "Inner error: ...".
537 /// print_error(&Error::new(ErrorKind::Other, "oh no!"));
540 #[stable(feature = "io_error_inner", since = "1.3.0")]
542 pub fn get_ref(&self) -> Option
<&(dyn error
::Error
+ Send
+ Sync
+ '
static)> {
544 Repr
::Os(..) => None
,
545 Repr
::Simple(..) => None
,
546 Repr
::SimpleMessage(..) => None
,
547 Repr
::Custom(ref c
) => Some(&*c
.error
),
551 /// Returns a mutable reference to the inner error wrapped by this error
554 /// If this [`Error`] was constructed via [`new`] then this function will
555 /// return [`Some`], otherwise it will return [`None`].
557 /// [`new`]: Error::new
562 /// use std::io::{Error, ErrorKind};
563 /// use std::{error, fmt};
564 /// use std::fmt::Display;
572 /// fn new() -> MyError {
574 /// v: "oh no!".to_string()
578 /// fn change_message(&mut self, new_message: &str) {
579 /// self.v = new_message.to_string();
583 /// impl error::Error for MyError {}
585 /// impl Display for MyError {
586 /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
587 /// write!(f, "MyError: {}", &self.v)
591 /// fn change_error(mut err: Error) -> Error {
592 /// if let Some(inner_err) = err.get_mut() {
593 /// inner_err.downcast_mut::<MyError>().unwrap().change_message("I've been changed!");
598 /// fn print_error(err: &Error) {
599 /// if let Some(inner_err) = err.get_ref() {
600 /// println!("Inner error: {}", inner_err);
602 /// println!("No inner error");
607 /// // Will print "No inner error".
608 /// print_error(&change_error(Error::last_os_error()));
609 /// // Will print "Inner error: ...".
610 /// print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
613 #[stable(feature = "io_error_inner", since = "1.3.0")]
615 pub fn get_mut(&mut self) -> Option
<&mut (dyn error
::Error
+ Send
+ Sync
+ '
static)> {
617 Repr
::Os(..) => None
,
618 Repr
::Simple(..) => None
,
619 Repr
::SimpleMessage(..) => None
,
620 Repr
::Custom(ref mut c
) => Some(&mut *c
.error
),
624 /// Consumes the `Error`, returning its inner error (if any).
626 /// If this [`Error`] was constructed via [`new`] then this function will
627 /// return [`Some`], otherwise it will return [`None`].
629 /// [`new`]: Error::new
634 /// use std::io::{Error, ErrorKind};
636 /// fn print_error(err: Error) {
637 /// if let Some(inner_err) = err.into_inner() {
638 /// println!("Inner error: {}", inner_err);
640 /// println!("No inner error");
645 /// // Will print "No inner error".
646 /// print_error(Error::last_os_error());
647 /// // Will print "Inner error: ...".
648 /// print_error(Error::new(ErrorKind::Other, "oh no!"));
651 #[stable(feature = "io_error_inner", since = "1.3.0")]
653 pub fn into_inner(self) -> Option
<Box
<dyn error
::Error
+ Send
+ Sync
>> {
655 Repr
::Os(..) => None
,
656 Repr
::Simple(..) => None
,
657 Repr
::SimpleMessage(..) => None
,
658 Repr
::Custom(c
) => Some(c
.error
),
662 /// Returns the corresponding [`ErrorKind`] for this error.
667 /// use std::io::{Error, ErrorKind};
669 /// fn print_error(err: Error) {
670 /// println!("{:?}", err.kind());
674 /// // Will print "Uncategorized".
675 /// print_error(Error::last_os_error());
676 /// // Will print "AddrInUse".
677 /// print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
680 #[stable(feature = "rust1", since = "1.0.0")]
682 pub fn kind(&self) -> ErrorKind
{
684 Repr
::Os(code
) => sys
::decode_error_kind(code
),
685 Repr
::Custom(ref c
) => c
.kind
,
686 Repr
::Simple(kind
) => kind
,
687 Repr
::SimpleMessage(kind
, _
) => kind
,
692 impl fmt
::Debug
for Repr
{
693 fn fmt(&self, fmt
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
695 Repr
::Os(code
) => fmt
697 .field("code", &code
)
698 .field("kind", &sys
::decode_error_kind(code
))
699 .field("message", &sys
::os
::error_string(code
))
701 Repr
::Custom(ref c
) => fmt
::Debug
::fmt(&c
, fmt
),
702 Repr
::Simple(kind
) => fmt
.debug_tuple("Kind").field(&kind
).finish(),
703 Repr
::SimpleMessage(kind
, &message
) => {
704 fmt
.debug_struct("Error").field("kind", &kind
).field("message", &message
).finish()
710 #[stable(feature = "rust1", since = "1.0.0")]
711 impl fmt
::Display
for Error
{
712 fn fmt(&self, fmt
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
715 let detail
= sys
::os
::error_string(code
);
716 write
!(fmt
, "{} (os error {})", detail
, code
)
718 Repr
::Custom(ref c
) => c
.error
.fmt(fmt
),
719 Repr
::Simple(kind
) => write
!(fmt
, "{}", kind
.as_str()),
720 Repr
::SimpleMessage(_
, &msg
) => msg
.fmt(fmt
),
725 #[stable(feature = "rust1", since = "1.0.0")]
726 impl error
::Error
for Error
{
727 #[allow(deprecated, deprecated_in_future)]
728 fn description(&self) -> &str {
730 Repr
::Os(..) | Repr
::Simple(..) => self.kind().as_str(),
731 Repr
::SimpleMessage(_
, &msg
) => msg
,
732 Repr
::Custom(ref c
) => c
.error
.description(),
737 fn cause(&self) -> Option
<&dyn error
::Error
> {
739 Repr
::Os(..) => None
,
740 Repr
::Simple(..) => None
,
741 Repr
::SimpleMessage(..) => None
,
742 Repr
::Custom(ref c
) => c
.error
.cause(),
746 fn source(&self) -> Option
<&(dyn error
::Error
+ '
static)> {
748 Repr
::Os(..) => None
,
749 Repr
::Simple(..) => None
,
750 Repr
::SimpleMessage(..) => None
,
751 Repr
::Custom(ref c
) => c
.error
.source(),
756 fn _assert_error_is_sync_send() {
757 fn _is_sync_send
<T
: Sync
+ Send
>() {}
758 _is_sync_send
::<Error
>();