4 #[cfg(target_pointer_width = "64")]
6 #[cfg(target_pointer_width = "64")]
7 use repr_bitpacked
::Repr
;
9 #[cfg(not(target_pointer_width = "64"))]
11 #[cfg(not(target_pointer_width = "64"))]
12 use repr_unpacked
::Repr
;
19 /// A specialized [`Result`] type for I/O operations.
21 /// This type is broadly used across [`std::io`] for any operation which may
24 /// This typedef is generally used to avoid writing out [`io::Error`] directly and
25 /// is otherwise a direct mapping to [`Result`].
27 /// While usual Rust style is to import types directly, aliases of [`Result`]
28 /// often are not, to make it easier to distinguish between them. [`Result`] is
29 /// generally assumed to be [`std::result::Result`][`Result`], and so users of this alias
30 /// will generally use `io::Result` instead of shadowing the [prelude]'s import
31 /// of [`std::result::Result`][`Result`].
33 /// [`std::io`]: crate::io
34 /// [`io::Error`]: Error
35 /// [`Result`]: crate::result::Result
36 /// [prelude]: crate::prelude
40 /// A convenience function that bubbles an `io::Result` to its caller:
45 /// fn get_string() -> io::Result<String> {
46 /// let mut buffer = String::new();
48 /// io::stdin().read_line(&mut buffer)?;
53 #[stable(feature = "rust1", since = "1.0.0")]
54 pub type Result
<T
> = result
::Result
<T
, Error
>;
56 /// The error type for I/O operations of the [`Read`], [`Write`], [`Seek`], and
57 /// associated traits.
59 /// Errors mostly originate from the underlying OS, but custom instances of
60 /// `Error` can be created with crafted error messages and a particular value of
63 /// [`Read`]: crate::io::Read
64 /// [`Write`]: crate::io::Write
65 /// [`Seek`]: crate::io::Seek
66 #[stable(feature = "rust1", since = "1.0.0")]
71 #[stable(feature = "rust1", since = "1.0.0")]
72 impl fmt
::Debug
for Error
{
73 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
74 fmt
::Debug
::fmt(&self.repr
, f
)
78 #[stable(feature = "rust1", since = "1.0.0")]
79 impl From
<alloc
::ffi
::NulError
> for Error
{
80 /// Converts a [`alloc::ffi::NulError`] into a [`Error`].
81 fn from(_
: alloc
::ffi
::NulError
) -> Error
{
82 const_io_error
!(ErrorKind
::InvalidInput
, "data provided contains a nul byte")
86 // Only derive debug in tests, to make sure it
87 // doesn't accidentally get printed.
88 #[cfg_attr(test, derive(Debug))]
92 SimpleMessage(&'
static SimpleMessage
),
96 /// The type of raw OS error codes returned by [`Error::raw_os_error`].
98 /// This is an [`i32`] on all currently supported platforms, but platforms
99 /// added in the future (such as UEFI) may use a different primitive type like
100 /// [`usize`]. Use `as`or [`into`] conversions where applicable to ensure maximum
103 /// [`into`]: Into::into
104 #[unstable(feature = "raw_os_error_ty", issue = "107792")]
105 pub type RawOsError
= i32;
107 // `#[repr(align(4))]` is probably redundant, it should have that value or
108 // higher already. We include it just because repr_bitpacked.rs's encoding
109 // requires an alignment >= 4 (note that `#[repr(align)]` will not reduce the
110 // alignment required by the struct, only increase it).
112 // If we add more variants to ErrorData, this can be increased to 8, but it
113 // should probably be behind `#[cfg_attr(target_pointer_width = "64", ...)]` or
114 // whatever cfg we're using to enable the `repr_bitpacked` code, since only the
115 // that version needs the alignment, and 8 is higher than the alignment we'll
116 // have on 32 bit platforms.
118 // (For the sake of being explicit: the alignment requirement here only matters
119 // if `error/repr_bitpacked.rs` is in use — for the unpacked repr it doesn't
123 pub(crate) struct SimpleMessage
{
125 message
: &'
static str,
129 pub(crate) const fn new(kind
: ErrorKind
, message
: &'
static str) -> Self {
130 Self { kind, message }
134 /// Create and return an `io::Error` for a given `ErrorKind` and constant
135 /// message. This doesn't allocate.
136 pub(crate) macro const_io_error($kind
:expr
, $message
:expr $
(,)?
) {
137 $
crate::io
::error
::Error
::from_static_message({
138 const MESSAGE_DATA
: $
crate::io
::error
::SimpleMessage
=
139 $
crate::io
::error
::SimpleMessage
::new($kind
, $message
);
144 // As with `SimpleMessage`: `#[repr(align(4))]` here is just because
145 // repr_bitpacked's encoding requires it. In practice it almost certainly be
146 // already be this high or higher.
151 error
: Box
<dyn error
::Error
+ Send
+ Sync
>,
154 /// A list specifying general categories of I/O error.
156 /// This list is intended to grow over time and it is not recommended to
157 /// exhaustively match against it.
159 /// It is used with the [`io::Error`] type.
161 /// [`io::Error`]: Error
163 /// # Handling errors and matching on `ErrorKind`
165 /// In application code, use `match` for the `ErrorKind` values you are
166 /// expecting; use `_` to match "all other errors".
168 /// In comprehensive and thorough tests that want to verify that a test doesn't
169 /// return any known incorrect error kind, you may want to cut-and-paste the
170 /// current full list of errors from here into your test code, and then match
171 /// `_` as the correct case. This seems counterintuitive, but it will make your
172 /// tests more robust. In particular, if you want to verify that your code does
173 /// produce an unrecognized error kind, the robust solution is to check for all
174 /// the recognized error kinds and fail in those cases.
175 #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
176 #[stable(feature = "rust1", since = "1.0.0")]
180 /// An entity was not found, often a file.
181 #[stable(feature = "rust1", since = "1.0.0")]
183 /// The operation lacked the necessary privileges to complete.
184 #[stable(feature = "rust1", since = "1.0.0")]
186 /// The connection was refused by the remote server.
187 #[stable(feature = "rust1", since = "1.0.0")]
189 /// The connection was reset by the remote server.
190 #[stable(feature = "rust1", since = "1.0.0")]
192 /// The remote host is not reachable.
193 #[unstable(feature = "io_error_more", issue = "86442")]
195 /// The network containing the remote host is not reachable.
196 #[unstable(feature = "io_error_more", issue = "86442")]
198 /// The connection was aborted (terminated) by the remote server.
199 #[stable(feature = "rust1", since = "1.0.0")]
201 /// The network operation failed because it was not connected yet.
202 #[stable(feature = "rust1", since = "1.0.0")]
204 /// A socket address could not be bound because the address is already in
206 #[stable(feature = "rust1", since = "1.0.0")]
208 /// A nonexistent interface was requested or the requested address was not
210 #[stable(feature = "rust1", since = "1.0.0")]
212 /// The system's networking is down.
213 #[unstable(feature = "io_error_more", issue = "86442")]
215 /// The operation failed because a pipe was closed.
216 #[stable(feature = "rust1", since = "1.0.0")]
218 /// An entity already exists, often a file.
219 #[stable(feature = "rust1", since = "1.0.0")]
221 /// The operation needs to block to complete, but the blocking operation was
222 /// requested to not occur.
223 #[stable(feature = "rust1", since = "1.0.0")]
225 /// A filesystem object is, unexpectedly, not a directory.
227 /// For example, a filesystem path was specified where one of the intermediate directory
228 /// components was, in fact, a plain file.
229 #[unstable(feature = "io_error_more", issue = "86442")]
231 /// The filesystem object is, unexpectedly, a directory.
233 /// A directory was specified when a non-directory was expected.
234 #[unstable(feature = "io_error_more", issue = "86442")]
236 /// A non-empty directory was specified where an empty directory was expected.
237 #[unstable(feature = "io_error_more", issue = "86442")]
239 /// The filesystem or storage medium is read-only, but a write operation was attempted.
240 #[unstable(feature = "io_error_more", issue = "86442")]
242 /// Loop in the filesystem or IO subsystem; often, too many levels of symbolic links.
244 /// There was a loop (or excessively long chain) resolving a filesystem object
245 /// or file IO object.
247 /// On Unix this is usually the result of a symbolic link loop; or, of exceeding the
248 /// system-specific limit on the depth of symlink traversal.
249 #[unstable(feature = "io_error_more", issue = "86442")]
251 /// Stale network file handle.
253 /// With some network filesystems, notably NFS, an open file (or directory) can be invalidated
254 /// by problems with the network or server.
255 #[unstable(feature = "io_error_more", issue = "86442")]
256 StaleNetworkFileHandle
,
257 /// A parameter was incorrect.
258 #[stable(feature = "rust1", since = "1.0.0")]
260 /// Data not valid for the operation were encountered.
262 /// Unlike [`InvalidInput`], this typically means that the operation
263 /// parameters were valid, however the error was caused by malformed
266 /// For example, a function that reads a file into a string will error with
267 /// `InvalidData` if the file's contents are not valid UTF-8.
269 /// [`InvalidInput`]: ErrorKind::InvalidInput
270 #[stable(feature = "io_invalid_data", since = "1.2.0")]
272 /// The I/O operation's timeout expired, causing it to be canceled.
273 #[stable(feature = "rust1", since = "1.0.0")]
275 /// An error returned when an operation could not be completed because a
276 /// call to [`write`] returned [`Ok(0)`].
278 /// This typically means that an operation could only succeed if it wrote a
279 /// particular number of bytes but only a smaller number of bytes could be
282 /// [`write`]: crate::io::Write::write
284 #[stable(feature = "rust1", since = "1.0.0")]
286 /// The underlying storage (typically, a filesystem) is full.
288 /// This does not include out of quota errors.
289 #[unstable(feature = "io_error_more", issue = "86442")]
291 /// Seek on unseekable file.
293 /// Seeking was attempted on an open file handle which is not suitable for seeking - for
294 /// example, on Unix, a named pipe opened with `File::open`.
295 #[unstable(feature = "io_error_more", issue = "86442")]
297 /// Filesystem quota was exceeded.
298 #[unstable(feature = "io_error_more", issue = "86442")]
299 FilesystemQuotaExceeded
,
300 /// File larger than allowed or supported.
302 /// This might arise from a hard limit of the underlying filesystem or file access API, or from
303 /// an administratively imposed resource limitation. Simple disk full, and out of quota, have
304 /// their own errors.
305 #[unstable(feature = "io_error_more", issue = "86442")]
307 /// Resource is busy.
308 #[unstable(feature = "io_error_more", issue = "86442")]
310 /// Executable file is busy.
312 /// An attempt was made to write to a file which is also in use as a running program. (Not all
313 /// operating systems detect this situation.)
314 #[unstable(feature = "io_error_more", issue = "86442")]
316 /// Deadlock (avoided).
318 /// A file locking operation would result in deadlock. This situation is typically detected, if
319 /// at all, on a best-effort basis.
320 #[unstable(feature = "io_error_more", issue = "86442")]
322 /// Cross-device or cross-filesystem (hard) link or rename.
323 #[unstable(feature = "io_error_more", issue = "86442")]
325 /// Too many (hard) links to the same filesystem object.
327 /// The filesystem does not support making so many hardlinks to the same file.
328 #[unstable(feature = "io_error_more", issue = "86442")]
330 /// A filename was invalid.
332 /// This error can also cause if it exceeded the filename length limit.
333 #[unstable(feature = "io_error_more", issue = "86442")]
335 /// Program argument list too long.
337 /// When trying to run an external program, a system or process limit on the size of the
338 /// arguments would have been exceeded.
339 #[unstable(feature = "io_error_more", issue = "86442")]
341 /// This operation was interrupted.
343 /// Interrupted operations can typically be retried.
344 #[stable(feature = "rust1", since = "1.0.0")]
347 /// This operation is unsupported on this platform.
349 /// This means that the operation can never succeed.
350 #[stable(feature = "unsupported_error", since = "1.53.0")]
353 // ErrorKinds which are primarily categorisations for OS error
354 // codes should be added above.
356 /// An error returned when an operation could not be completed because an
357 /// "end of file" was reached prematurely.
359 /// This typically means that an operation could only succeed if it read a
360 /// particular number of bytes but only a smaller number of bytes could be
362 #[stable(feature = "read_exact", since = "1.6.0")]
365 /// An operation could not be completed, because it failed
366 /// to allocate enough memory.
367 #[stable(feature = "out_of_memory_error", since = "1.54.0")]
370 // "Unusual" error kinds which do not correspond simply to (sets
371 // of) OS error codes, should be added just above this comment.
372 // `Other` and `Uncategorized` should remain at the end:
374 /// A custom error that does not fall under any other I/O error kind.
376 /// This can be used to construct your own [`Error`]s that do not match any
379 /// This [`ErrorKind`] is not used by the standard library.
381 /// Errors from the standard library that do not fall under any of the I/O
382 /// error kinds cannot be `match`ed on, and will only match a wildcard (`_`) pattern.
383 /// New [`ErrorKind`]s might be added in the future for some of those.
384 #[stable(feature = "rust1", since = "1.0.0")]
387 /// Any I/O error from the standard library that's not part of this list.
389 /// Errors that are `Uncategorized` now may move to a different or a new
390 /// [`ErrorKind`] variant in the future. It is not recommended to match
391 /// an error against `Uncategorized`; use a wildcard match (`_`) instead.
392 #[unstable(feature = "io_error_uncategorized", issue = "none")]
398 pub(crate) fn as_str(&self) -> &'
static str {
400 // tidy-alphabetical-start
402 AddrInUse
=> "address in use",
403 AddrNotAvailable
=> "address not available",
404 AlreadyExists
=> "entity already exists",
405 ArgumentListTooLong
=> "argument list too long",
406 BrokenPipe
=> "broken pipe",
407 ConnectionAborted
=> "connection aborted",
408 ConnectionRefused
=> "connection refused",
409 ConnectionReset
=> "connection reset",
410 CrossesDevices
=> "cross-device link or rename",
411 Deadlock
=> "deadlock",
412 DirectoryNotEmpty
=> "directory not empty",
413 ExecutableFileBusy
=> "executable file busy",
414 FileTooLarge
=> "file too large",
415 FilesystemLoop
=> "filesystem loop or indirection limit (e.g. symlink loop)",
416 FilesystemQuotaExceeded
=> "filesystem quota exceeded",
417 HostUnreachable
=> "host unreachable",
418 Interrupted
=> "operation interrupted",
419 InvalidData
=> "invalid data",
420 InvalidFilename
=> "invalid filename",
421 InvalidInput
=> "invalid input parameter",
422 IsADirectory
=> "is a directory",
423 NetworkDown
=> "network down",
424 NetworkUnreachable
=> "network unreachable",
425 NotADirectory
=> "not a directory",
426 NotConnected
=> "not connected",
427 NotFound
=> "entity not found",
428 NotSeekable
=> "seek on unseekable file",
429 Other
=> "other error",
430 OutOfMemory
=> "out of memory",
431 PermissionDenied
=> "permission denied",
432 ReadOnlyFilesystem
=> "read-only filesystem or storage medium",
433 ResourceBusy
=> "resource busy",
434 StaleNetworkFileHandle
=> "stale network file handle",
435 StorageFull
=> "no storage space",
436 TimedOut
=> "timed out",
437 TooManyLinks
=> "too many links",
438 Uncategorized
=> "uncategorized error",
439 UnexpectedEof
=> "unexpected end of file",
440 Unsupported
=> "unsupported",
441 WouldBlock
=> "operation would block",
442 WriteZero
=> "write zero",
444 // tidy-alphabetical-end
448 #[stable(feature = "io_errorkind_display", since = "1.60.0")]
449 impl fmt
::Display
for ErrorKind
{
450 /// Shows a human-readable description of the `ErrorKind`.
452 /// This is similar to `impl Display for Error`, but doesn't require first converting to Error.
456 /// use std::io::ErrorKind;
457 /// assert_eq!("entity not found", ErrorKind::NotFound.to_string());
459 fn fmt(&self, fmt
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
460 fmt
.write_str(self.as_str())
464 /// Intended for use for errors not exposed to the user, where allocating onto
465 /// the heap (for normal construction via Error::new) is too costly.
466 #[stable(feature = "io_error_from_errorkind", since = "1.14.0")]
467 impl From
<ErrorKind
> for Error
{
468 /// Converts an [`ErrorKind`] into an [`Error`].
470 /// This conversion creates a new error with a simple representation of error kind.
475 /// use std::io::{Error, ErrorKind};
477 /// let not_found = ErrorKind::NotFound;
478 /// let error = Error::from(not_found);
479 /// assert_eq!("entity not found", format!("{error}"));
482 fn from(kind
: ErrorKind
) -> Error
{
483 Error { repr: Repr::new_simple(kind) }
488 /// Creates a new I/O error from a known kind of error as well as an
489 /// arbitrary error payload.
491 /// This function is used to generically create I/O errors which do not
492 /// originate from the OS itself. The `error` argument is an arbitrary
493 /// payload which will be contained in this [`Error`].
495 /// Note that this function allocates memory on the heap.
496 /// If no extra payload is required, use the `From` conversion from
502 /// use std::io::{Error, ErrorKind};
504 /// // errors can be created from strings
505 /// let custom_error = Error::new(ErrorKind::Other, "oh no!");
507 /// // errors can also be created from other errors
508 /// let custom_error2 = Error::new(ErrorKind::Interrupted, custom_error);
510 /// // creating an error without payload (and without memory allocation)
511 /// let eof_error = Error::from(ErrorKind::UnexpectedEof);
513 #[stable(feature = "rust1", since = "1.0.0")]
514 pub fn new
<E
>(kind
: ErrorKind
, error
: E
) -> Error
516 E
: Into
<Box
<dyn error
::Error
+ Send
+ Sync
>>,
518 Self::_new(kind
, error
.into())
521 /// Creates a new I/O error from an arbitrary error payload.
523 /// This function is used to generically create I/O errors which do not
524 /// originate from the OS itself. It is a shortcut for [`Error::new`]
525 /// with [`ErrorKind::Other`].
530 /// #![feature(io_error_other)]
532 /// use std::io::Error;
534 /// // errors can be created from strings
535 /// let custom_error = Error::other("oh no!");
537 /// // errors can also be created from other errors
538 /// let custom_error2 = Error::other(custom_error);
540 #[unstable(feature = "io_error_other", issue = "91946")]
541 pub fn other
<E
>(error
: E
) -> Error
543 E
: Into
<Box
<dyn error
::Error
+ Send
+ Sync
>>,
545 Self::_new(ErrorKind
::Other
, error
.into())
548 fn _new(kind
: ErrorKind
, error
: Box
<dyn error
::Error
+ Send
+ Sync
>) -> Error
{
549 Error { repr: Repr::new_custom(Box::new(Custom { kind, error }
)) }
552 /// Creates a new I/O error from a known kind of error as well as a constant
555 /// This function does not allocate.
557 /// You should not use this directly, and instead use the `const_io_error!`
558 /// macro: `io::const_io_error!(ErrorKind::Something, "some_message")`.
560 /// This function should maybe change to `from_static_message<const MSG: &'static
561 /// str>(kind: ErrorKind)` in the future, when const generics allow that.
563 pub(crate) const fn from_static_message(msg
: &'
static SimpleMessage
) -> Error
{
564 Self { repr: Repr::new_simple_message(msg) }
567 /// Returns an error representing the last OS error which occurred.
569 /// This function reads the value of `errno` for the target platform (e.g.
570 /// `GetLastError` on Windows) and will return a corresponding instance of
571 /// [`Error`] for the error code.
573 /// This should be called immediately after a call to a platform function,
574 /// otherwise the state of the error value is indeterminate. In particular,
575 /// other standard library functions may call platform functions that may
576 /// (or may not) reset the error value even if they succeed.
581 /// use std::io::Error;
583 /// let os_error = Error::last_os_error();
584 /// println!("last OS error: {os_error:?}");
586 #[stable(feature = "rust1", since = "1.0.0")]
587 #[doc(alias = "GetLastError")]
588 #[doc(alias = "errno")]
591 pub fn last_os_error() -> Error
{
592 Error
::from_raw_os_error(sys
::os
::errno())
595 /// Creates a new instance of an [`Error`] from a particular OS error code.
602 /// # if cfg!(target_os = "linux") {
605 /// let error = io::Error::from_raw_os_error(22);
606 /// assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
613 /// # if cfg!(windows) {
616 /// let error = io::Error::from_raw_os_error(10022);
617 /// assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
620 #[stable(feature = "rust1", since = "1.0.0")]
623 pub fn from_raw_os_error(code
: RawOsError
) -> Error
{
624 Error { repr: Repr::new_os(code) }
627 /// Returns the OS error that this error represents (if any).
629 /// If this [`Error`] was constructed via [`last_os_error`] or
630 /// [`from_raw_os_error`], then this function will return [`Some`], otherwise
631 /// it will return [`None`].
633 /// [`last_os_error`]: Error::last_os_error
634 /// [`from_raw_os_error`]: Error::from_raw_os_error
639 /// use std::io::{Error, ErrorKind};
641 /// fn print_os_error(err: &Error) {
642 /// if let Some(raw_os_err) = err.raw_os_error() {
643 /// println!("raw OS error: {raw_os_err:?}");
645 /// println!("Not an OS error");
650 /// // Will print "raw OS error: ...".
651 /// print_os_error(&Error::last_os_error());
652 /// // Will print "Not an OS error".
653 /// print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
656 #[stable(feature = "rust1", since = "1.0.0")]
659 pub fn raw_os_error(&self) -> Option
<RawOsError
> {
660 match self.repr
.data() {
661 ErrorData
::Os(i
) => Some(i
),
662 ErrorData
::Custom(..) => None
,
663 ErrorData
::Simple(..) => None
,
664 ErrorData
::SimpleMessage(..) => None
,
668 /// Returns a reference to the inner error wrapped by this error (if any).
670 /// If this [`Error`] was constructed via [`new`] then this function will
671 /// return [`Some`], otherwise it will return [`None`].
673 /// [`new`]: Error::new
678 /// use std::io::{Error, ErrorKind};
680 /// fn print_error(err: &Error) {
681 /// if let Some(inner_err) = err.get_ref() {
682 /// println!("Inner error: {inner_err:?}");
684 /// println!("No inner error");
689 /// // Will print "No inner error".
690 /// print_error(&Error::last_os_error());
691 /// // Will print "Inner error: ...".
692 /// print_error(&Error::new(ErrorKind::Other, "oh no!"));
695 #[stable(feature = "io_error_inner", since = "1.3.0")]
698 pub fn get_ref(&self) -> Option
<&(dyn error
::Error
+ Send
+ Sync
+ '
static)> {
699 match self.repr
.data() {
700 ErrorData
::Os(..) => None
,
701 ErrorData
::Simple(..) => None
,
702 ErrorData
::SimpleMessage(..) => None
,
703 ErrorData
::Custom(c
) => Some(&*c
.error
),
707 /// Returns a mutable reference to the inner error wrapped by this error
710 /// If this [`Error`] was constructed via [`new`] then this function will
711 /// return [`Some`], otherwise it will return [`None`].
713 /// [`new`]: Error::new
718 /// use std::io::{Error, ErrorKind};
719 /// use std::{error, fmt};
720 /// use std::fmt::Display;
728 /// fn new() -> MyError {
730 /// v: "oh no!".to_string()
734 /// fn change_message(&mut self, new_message: &str) {
735 /// self.v = new_message.to_string();
739 /// impl error::Error for MyError {}
741 /// impl Display for MyError {
742 /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
743 /// write!(f, "MyError: {}", &self.v)
747 /// fn change_error(mut err: Error) -> Error {
748 /// if let Some(inner_err) = err.get_mut() {
749 /// inner_err.downcast_mut::<MyError>().unwrap().change_message("I've been changed!");
754 /// fn print_error(err: &Error) {
755 /// if let Some(inner_err) = err.get_ref() {
756 /// println!("Inner error: {inner_err}");
758 /// println!("No inner error");
763 /// // Will print "No inner error".
764 /// print_error(&change_error(Error::last_os_error()));
765 /// // Will print "Inner error: ...".
766 /// print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
769 #[stable(feature = "io_error_inner", since = "1.3.0")]
772 pub fn get_mut(&mut self) -> Option
<&mut (dyn error
::Error
+ Send
+ Sync
+ '
static)> {
773 match self.repr
.data_mut() {
774 ErrorData
::Os(..) => None
,
775 ErrorData
::Simple(..) => None
,
776 ErrorData
::SimpleMessage(..) => None
,
777 ErrorData
::Custom(c
) => Some(&mut *c
.error
),
781 /// Consumes the `Error`, returning its inner error (if any).
783 /// If this [`Error`] was constructed via [`new`] then this function will
784 /// return [`Some`], otherwise it will return [`None`].
786 /// [`new`]: Error::new
791 /// use std::io::{Error, ErrorKind};
793 /// fn print_error(err: Error) {
794 /// if let Some(inner_err) = err.into_inner() {
795 /// println!("Inner error: {inner_err}");
797 /// println!("No inner error");
802 /// // Will print "No inner error".
803 /// print_error(Error::last_os_error());
804 /// // Will print "Inner error: ...".
805 /// print_error(Error::new(ErrorKind::Other, "oh no!"));
808 #[stable(feature = "io_error_inner", since = "1.3.0")]
809 #[must_use = "`self` will be dropped if the result is not used"]
811 pub fn into_inner(self) -> Option
<Box
<dyn error
::Error
+ Send
+ Sync
>> {
812 match self.repr
.into_data() {
813 ErrorData
::Os(..) => None
,
814 ErrorData
::Simple(..) => None
,
815 ErrorData
::SimpleMessage(..) => None
,
816 ErrorData
::Custom(c
) => Some(c
.error
),
820 /// Attempt to downgrade the inner error to `E` if any.
822 /// If this [`Error`] was constructed via [`new`] then this function will
823 /// attempt to perform downgrade on it, otherwise it will return [`Err`].
825 /// If downgrade succeeds, it will return [`Ok`], otherwise it will also
828 /// [`new`]: Error::new
833 /// #![feature(io_error_downcast)]
837 /// use std::error::Error;
842 /// SomeOtherVariant,
845 /// impl fmt::Display for E {
847 /// # fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
851 /// impl Error for E {}
853 /// impl From<io::Error> for E {
854 /// fn from(err: io::Error) -> E {
855 /// err.downcast::<E>()
857 /// .unwrap_or_else(E::Io)
861 #[unstable(feature = "io_error_downcast", issue = "99262")]
862 pub fn downcast
<E
>(self) -> result
::Result
<Box
<E
>, Self>
864 E
: error
::Error
+ Send
+ Sync
+ '
static,
866 match self.repr
.into_data() {
867 ErrorData
::Custom(b
) if b
.error
.is
::<E
>() => {
868 let res
= (*b
).error
.downcast
::<E
>();
870 // downcast is a really trivial and is marked as inline, so
871 // it's likely be inlined here.
873 // And the compiler should be able to eliminate the branch
874 // that produces `Err` here since b.error.is::<E>()
878 repr_data
=> Err(Self { repr: Repr::new(repr_data) }
),
882 /// Returns the corresponding [`ErrorKind`] for this error.
884 /// This may be a value set by Rust code constructing custom `io::Error`s,
885 /// or if this `io::Error` was sourced from the operating system,
886 /// it will be a value inferred from the system's error encoding.
887 /// See [`last_os_error`] for more details.
889 /// [`last_os_error`]: Error::last_os_error
894 /// use std::io::{Error, ErrorKind};
896 /// fn print_error(err: Error) {
897 /// println!("{:?}", err.kind());
901 /// // As no error has (visibly) occurred, this may print anything!
902 /// // It likely prints a placeholder for unidentified (non-)errors.
903 /// print_error(Error::last_os_error());
904 /// // Will print "AddrInUse".
905 /// print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
908 #[stable(feature = "rust1", since = "1.0.0")]
911 pub fn kind(&self) -> ErrorKind
{
912 match self.repr
.data() {
913 ErrorData
::Os(code
) => sys
::decode_error_kind(code
),
914 ErrorData
::Custom(c
) => c
.kind
,
915 ErrorData
::Simple(kind
) => kind
,
916 ErrorData
::SimpleMessage(m
) => m
.kind
,
921 impl fmt
::Debug
for Repr
{
922 fn fmt(&self, fmt
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
924 ErrorData
::Os(code
) => fmt
926 .field("code", &code
)
927 .field("kind", &sys
::decode_error_kind(code
))
928 .field("message", &sys
::os
::error_string(code
))
930 ErrorData
::Custom(c
) => fmt
::Debug
::fmt(&c
, fmt
),
931 ErrorData
::Simple(kind
) => fmt
.debug_tuple("Kind").field(&kind
).finish(),
932 ErrorData
::SimpleMessage(msg
) => fmt
933 .debug_struct("Error")
934 .field("kind", &msg
.kind
)
935 .field("message", &msg
.message
)
941 #[stable(feature = "rust1", since = "1.0.0")]
942 impl fmt
::Display
for Error
{
943 fn fmt(&self, fmt
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
944 match self.repr
.data() {
945 ErrorData
::Os(code
) => {
946 let detail
= sys
::os
::error_string(code
);
947 write
!(fmt
, "{detail} (os error {code})")
949 ErrorData
::Custom(ref c
) => c
.error
.fmt(fmt
),
950 ErrorData
::Simple(kind
) => write
!(fmt
, "{}", kind
.as_str()),
951 ErrorData
::SimpleMessage(msg
) => msg
.message
.fmt(fmt
),
956 #[stable(feature = "rust1", since = "1.0.0")]
957 impl error
::Error
for Error
{
958 #[allow(deprecated, deprecated_in_future)]
959 fn description(&self) -> &str {
960 match self.repr
.data() {
961 ErrorData
::Os(..) | ErrorData
::Simple(..) => self.kind().as_str(),
962 ErrorData
::SimpleMessage(msg
) => msg
.message
,
963 ErrorData
::Custom(c
) => c
.error
.description(),
968 fn cause(&self) -> Option
<&dyn error
::Error
> {
969 match self.repr
.data() {
970 ErrorData
::Os(..) => None
,
971 ErrorData
::Simple(..) => None
,
972 ErrorData
::SimpleMessage(..) => None
,
973 ErrorData
::Custom(c
) => c
.error
.cause(),
977 fn source(&self) -> Option
<&(dyn error
::Error
+ '
static)> {
978 match self.repr
.data() {
979 ErrorData
::Os(..) => None
,
980 ErrorData
::Simple(..) => None
,
981 ErrorData
::SimpleMessage(..) => None
,
982 ErrorData
::Custom(c
) => c
.error
.source(),
987 fn _assert_error_is_sync_send() {
988 fn _is_sync_send
<T
: Sync
+ Send
>() {}
989 _is_sync_send
::<Error
>();