core/io/error.rs
1#![unstable(feature = "core_io", issue = "154046")]
2
3// On 64-bit platforms, `io::Error` may use a bit-packed representation to
4// reduce size. However, this representation assumes that error codes are
5// always 32-bit wide.
6//
7// This assumption is invalid on 64-bit UEFI, where error codes are 64-bit.
8// Therefore, the packed representation is explicitly disabled for UEFI
9// targets, and the unpacked representation must be used instead.
10#[cfg_attr(
11 all(target_pointer_width = "64", not(target_os = "uefi")),
12 path = "error/repr_bitpacked.rs"
13)]
14#[cfg_attr(
15 not(all(target_pointer_width = "64", not(target_os = "uefi"))),
16 path = "error/repr_unpacked.rs"
17)]
18mod repr;
19
20#[cfg_attr(
21 all(target_has_atomic_load_store = "ptr", not(no_io_statics)),
22 path = "error/os_functions_atomic.rs"
23)]
24#[cfg_attr(
25 not(all(target_has_atomic_load_store = "ptr", not(no_io_statics))),
26 path = "error/os_functions.rs"
27)]
28mod os_functions;
29
30use self::os_functions::{decode_error_kind, format_os_error, is_interrupted, set_functions};
31use self::repr::Repr;
32use crate::{error, fmt, result};
33
34/// A specialized [`Result`] type for I/O operations.
35///
36/// This type is broadly used across [`std::io`] for any operation which may
37/// produce an error.
38///
39/// This type alias is generally used to avoid writing out [`io::Error`] directly and
40/// is otherwise a direct mapping to [`Result`].
41///
42/// While usual Rust style is to import types directly, aliases of [`Result`]
43/// often are not, to make it easier to distinguish between them. [`Result`] is
44/// generally assumed to be [`core::result::Result`][`Result`], and so users of this alias
45/// will generally use `io::Result` instead of shadowing the [prelude]'s import
46/// of [`core::result::Result`][`Result`].
47///
48// FIXME(#74481): Hard-links required to link from `core` to `std`
49/// [`std::io`]: ../../std/io/index.html
50/// [`io::Error`]: Error
51/// [`Result`]: crate::result::Result
52/// [prelude]: crate::prelude
53///
54/// # Examples
55///
56/// A convenience function that bubbles an `io::Result` to its caller:
57///
58/// ```
59/// use std::io;
60///
61/// fn get_string() -> io::Result<String> {
62/// let mut buffer = String::new();
63///
64/// io::stdin().read_line(&mut buffer)?;
65///
66/// Ok(buffer)
67/// }
68/// ```
69#[stable(feature = "rust1", since = "1.0.0")]
70#[doc(search_unbox)]
71pub type Result<T> = result::Result<T, Error>;
72
73/// The error type for I/O operations of the [`Read`][Read], [`Write`][Write], [`Seek`][Seek], and
74/// associated traits.
75///
76/// Errors mostly originate from the underlying OS, but custom instances of
77/// `Error` can be created with crafted error messages and a particular value of
78/// [`ErrorKind`].
79///
80// FIXME(#74481): Hard-links required to link from `core` to `std`
81/// [Read]: ../../std/io/trait.Read.html
82/// [Write]: ../../std/io/trait.Write.html
83/// [Seek]: ../../std/io/trait.Seek.html
84#[stable(feature = "rust1", since = "1.0.0")]
85#[rustc_has_incoherent_inherent_impls]
86pub struct Error {
87 repr: Repr,
88}
89
90#[stable(feature = "rust1", since = "1.0.0")]
91impl fmt::Debug for Error {
92 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93 fmt::Debug::fmt(&self.repr, f)
94 }
95}
96
97/// Common errors constants for use in std
98#[doc(hidden)]
99impl Error {
100 #[doc(hidden)]
101 #[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
102 pub const INVALID_UTF8: Self =
103 const_error!(ErrorKind::InvalidData, "stream did not contain valid UTF-8");
104
105 #[doc(hidden)]
106 #[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
107 pub const READ_EXACT_EOF: Self =
108 const_error!(ErrorKind::UnexpectedEof, "failed to fill whole buffer");
109
110 #[doc(hidden)]
111 #[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
112 pub const UNKNOWN_THREAD_COUNT: Self = const_error!(
113 ErrorKind::NotFound,
114 "the number of hardware threads is not known for the target platform",
115 );
116
117 #[doc(hidden)]
118 #[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
119 pub const UNSUPPORTED_PLATFORM: Self =
120 const_error!(ErrorKind::Unsupported, "operation not supported on this platform");
121
122 #[doc(hidden)]
123 #[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
124 pub const WRITE_ALL_EOF: Self =
125 const_error!(ErrorKind::WriteZero, "failed to write whole buffer");
126
127 #[doc(hidden)]
128 #[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
129 pub const ZERO_TIMEOUT: Self =
130 const_error!(ErrorKind::InvalidInput, "cannot set a 0 duration timeout");
131
132 #[doc(hidden)]
133 #[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
134 pub const NO_ADDRESSES: Self =
135 const_error!(ErrorKind::InvalidInput, "could not resolve to any addresses");
136}
137
138// Only derive debug in tests, to make sure it
139// doesn't accidentally get printed.
140#[cfg_attr(test, derive(Debug))]
141enum ErrorData<C> {
142 Os(RawOsError),
143 Simple(ErrorKind),
144 SimpleMessage(&'static SimpleMessage),
145 Custom(C),
146}
147
148// `#[repr(align(4))]` is probably redundant, it should have that value or
149// higher already. We include it just because repr_bitpacked.rs's encoding
150// requires an alignment >= 4 (note that `#[repr(align)]` will not reduce the
151// alignment required by the struct, only increase it).
152//
153// If we add more variants to ErrorData, this can be increased to 8, but it
154// should probably be behind `#[cfg_attr(target_pointer_width = "64", ...)]` or
155// whatever cfg we're using to enable the `repr_bitpacked` code, since only the
156// that version needs the alignment, and 8 is higher than the alignment we'll
157// have on 32 bit platforms.
158//
159// (For the sake of being explicit: the alignment requirement here only matters
160// if `error/repr_bitpacked.rs` is in use — for the unpacked repr it doesn't
161// matter at all)
162#[doc(hidden)]
163#[unstable(feature = "io_const_error_internals", issue = "none")]
164#[repr(align(4))]
165#[derive(Debug)]
166pub struct SimpleMessage {
167 pub kind: ErrorKind,
168 pub message: &'static str,
169}
170
171/// Creates a new I/O error from a known kind of error and a string literal.
172///
173/// Contrary to [`Error::new`][new], this macro does not allocate and can be used in
174/// `const` contexts.
175///
176// FIXME(#74481): Hard-links required to link from `core` to `alloc` for incoherent method
177/// [new]: ../../alloc/io/struct.Error.html#method.new
178///
179/// # Example
180/// ```
181/// #![feature(io_const_error)]
182/// use std::io::{const_error, Error, ErrorKind};
183///
184/// const FAIL: Error = const_error!(ErrorKind::Unsupported, "tried something that never works");
185///
186/// fn not_here() -> Result<(), Error> {
187/// Err(FAIL)
188/// }
189/// ```
190#[rustc_macro_transparency = "semiopaque"]
191#[unstable(feature = "io_const_error", issue = "133448")]
192#[allow_internal_unstable(core_io, hint_must_use, io_const_error_internals)]
193pub macro const_error($kind:expr, $message:expr $(,)?) {
194 $crate::hint::must_use($crate::io::Error::from_static_message(
195 const { &$crate::io::SimpleMessage { kind: $kind, message: $message } },
196 ))
197}
198
199/// Intended for use for errors not exposed to the user, where allocating onto
200/// the heap (for normal construction via Error::new) is too costly.
201#[stable(feature = "io_error_from_errorkind", since = "1.14.0")]
202impl From<ErrorKind> for Error {
203 /// Converts an [`ErrorKind`] into an [`Error`].
204 ///
205 /// This conversion creates a new error with a simple representation of error kind.
206 ///
207 /// # Examples
208 ///
209 /// ```
210 /// use std::io::{Error, ErrorKind};
211 ///
212 /// let not_found = ErrorKind::NotFound;
213 /// let error = Error::from(not_found);
214 /// assert_eq!("entity not found", format!("{error}"));
215 /// ```
216 #[inline]
217 fn from(kind: ErrorKind) -> Error {
218 Error { repr: Repr::new_simple(kind) }
219 }
220}
221
222impl Error {
223 /// # Safety
224 ///
225 /// The provided `CustomOwner` must have been constructed from a `Box` from the `alloc` crate.
226 #[doc(hidden)]
227 #[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
228 #[must_use]
229 #[inline]
230 pub unsafe fn from_custom_owner(custom: CustomOwner) -> Error {
231 Error { repr: Repr::new_custom(custom) }
232 }
233
234 #[doc(hidden)]
235 #[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
236 #[must_use]
237 #[inline]
238 pub fn into_custom_owner(self) -> result::Result<CustomOwner, Self> {
239 if matches!(self.repr.data(), ErrorData::Custom(..)) {
240 let ErrorData::Custom(c) = self.repr.into_data() else {
241 // SAFETY: Checked above using `matches!`.
242 unsafe { crate::hint::unreachable_unchecked() }
243 };
244 Ok(c)
245 } else {
246 Err(self)
247 }
248 }
249
250 /// Creates a new I/O error from a known kind of error as well as a constant
251 /// message.
252 ///
253 /// This function does not allocate.
254 ///
255 /// You should not use this directly, and instead use the `const_error!`
256 /// macro: `io::const_error!(ErrorKind::Something, "some_message")`.
257 ///
258 /// This function should maybe change to `from_static_message<const MSG: &'static
259 /// str>(kind: ErrorKind)` in the future, when const generics allow that.
260 #[inline]
261 #[doc(hidden)]
262 #[unstable(feature = "io_const_error_internals", issue = "none")]
263 pub const fn from_static_message(msg: &'static SimpleMessage) -> Error {
264 Self { repr: Repr::new_simple_message(msg) }
265 }
266
267 /// # Safety
268 ///
269 /// `functions` must point to data that is entirely constant; it must
270 /// not be created during runtime.
271 #[doc(hidden)]
272 #[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
273 #[must_use]
274 #[inline]
275 pub unsafe fn from_raw_os_error_with_functions(
276 code: RawOsError,
277 functions: &'static OsFunctions,
278 ) -> Error {
279 // SAFETY: Caller ensures `functions` is a constant not created at runtime.
280 unsafe {
281 set_functions(functions);
282 }
283 Error { repr: Repr::new_os(code) }
284 }
285
286 /// Returns the OS error that this error represents (if any).
287 ///
288 /// If this [`Error`] was constructed via [`last_os_error`][last_os_error] or
289 /// [`from_raw_os_error`][from_raw_os_error], then this function will return [`Some`], otherwise
290 /// it will return [`None`].
291 ///
292 // FIXME(#74481): Hard-links required to link from `core` to `std` for incoherent method
293 /// [last_os_error]: ../../std/io/struct.Error.html#method.last_os_error
294 /// [from_raw_os_error]: ../../std/io/struct.Error.html#method.from_raw_os_error
295 ///
296 /// # Examples
297 ///
298 /// ```
299 /// use std::io::{Error, ErrorKind};
300 ///
301 /// fn print_os_error(err: &Error) {
302 /// if let Some(raw_os_err) = err.raw_os_error() {
303 /// println!("raw OS error: {raw_os_err:?}");
304 /// } else {
305 /// println!("Not an OS error");
306 /// }
307 /// }
308 ///
309 /// fn main() {
310 /// // Will print "raw OS error: ...".
311 /// print_os_error(&Error::last_os_error());
312 /// // Will print "Not an OS error".
313 /// print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
314 /// }
315 /// ```
316 #[stable(feature = "rust1", since = "1.0.0")]
317 #[must_use]
318 #[inline]
319 pub fn raw_os_error(&self) -> Option<RawOsError> {
320 match self.repr.data() {
321 ErrorData::Os(i) => Some(i),
322 ErrorData::Custom(..) => None,
323 ErrorData::Simple(..) => None,
324 ErrorData::SimpleMessage(..) => None,
325 }
326 }
327
328 /// Returns a reference to the inner error wrapped by this error (if any).
329 ///
330 /// If this [`Error`] was constructed via [`new`][new] then this function will
331 /// return [`Some`], otherwise it will return [`None`].
332 ///
333 /// [new]: ../../alloc/io/struct.Error.html#method.new
334 ///
335 /// # Examples
336 ///
337 /// ```
338 /// use std::io::{Error, ErrorKind};
339 ///
340 /// fn print_error(err: &Error) {
341 /// if let Some(inner_err) = err.get_ref() {
342 /// println!("Inner error: {inner_err:?}");
343 /// } else {
344 /// println!("No inner error");
345 /// }
346 /// }
347 ///
348 /// fn main() {
349 /// // Will print "No inner error".
350 /// print_error(&Error::last_os_error());
351 /// // Will print "Inner error: ...".
352 /// print_error(&Error::new(ErrorKind::Other, "oh no!"));
353 /// }
354 /// ```
355 #[stable(feature = "io_error_inner", since = "1.3.0")]
356 #[must_use]
357 #[inline]
358 pub fn get_ref(&self) -> Option<&(dyn error::Error + Send + Sync + 'static)> {
359 match self.repr.data() {
360 ErrorData::Os(..) => None,
361 ErrorData::Simple(..) => None,
362 ErrorData::SimpleMessage(..) => None,
363 ErrorData::Custom(c) => Some(c.error_ref()),
364 }
365 }
366
367 /// Returns a mutable reference to the inner error wrapped by this error
368 /// (if any).
369 ///
370 /// If this [`Error`] was constructed via [`new`][new] then this function will
371 /// return [`Some`], otherwise it will return [`None`].
372 ///
373 // FIXME(#74481): Hard-links required to link from `core` to `std`
374 /// [new]: ../../alloc/io/struct.Error.html#method.new
375 ///
376 /// # Examples
377 ///
378 /// ```
379 /// use std::io::{Error, ErrorKind};
380 /// use std::{error, fmt};
381 /// use std::fmt::Display;
382 ///
383 /// #[derive(Debug)]
384 /// struct MyError {
385 /// v: String,
386 /// }
387 ///
388 /// impl MyError {
389 /// fn new() -> MyError {
390 /// MyError {
391 /// v: "oh no!".to_string()
392 /// }
393 /// }
394 ///
395 /// fn change_message(&mut self, new_message: &str) {
396 /// self.v = new_message.to_string();
397 /// }
398 /// }
399 ///
400 /// impl error::Error for MyError {}
401 ///
402 /// impl Display for MyError {
403 /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
404 /// write!(f, "MyError: {}", self.v)
405 /// }
406 /// }
407 ///
408 /// fn change_error(mut err: Error) -> Error {
409 /// if let Some(inner_err) = err.get_mut() {
410 /// inner_err.downcast_mut::<MyError>().unwrap().change_message("I've been changed!");
411 /// }
412 /// err
413 /// }
414 ///
415 /// fn print_error(err: &Error) {
416 /// if let Some(inner_err) = err.get_ref() {
417 /// println!("Inner error: {inner_err}");
418 /// } else {
419 /// println!("No inner error");
420 /// }
421 /// }
422 ///
423 /// fn main() {
424 /// // Will print "No inner error".
425 /// print_error(&change_error(Error::last_os_error()));
426 /// // Will print "Inner error: ...".
427 /// print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
428 /// }
429 /// ```
430 #[stable(feature = "io_error_inner", since = "1.3.0")]
431 #[must_use]
432 #[inline]
433 pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Send + Sync + 'static)> {
434 match self.repr.data_mut() {
435 ErrorData::Os(..) => None,
436 ErrorData::Simple(..) => None,
437 ErrorData::SimpleMessage(..) => None,
438 ErrorData::Custom(c) => Some(c.error_mut()),
439 }
440 }
441
442 /// Returns the corresponding [`ErrorKind`] for this error.
443 ///
444 /// This may be a value set by Rust code constructing custom `io::Error`s,
445 /// or if this `io::Error` was sourced from the operating system,
446 /// it will be a value inferred from the system's error encoding.
447 /// See [`last_os_error`][last_os_error] for more details.
448 ///
449 // FIXME(#74481): Hard-links required to link from `core` to `std`
450 /// [last_os_error]: ../../std/io/struct.Error.html#method.last_os_error
451 ///
452 /// # Examples
453 ///
454 /// ```
455 /// use std::io::{Error, ErrorKind};
456 ///
457 /// fn print_error(err: Error) {
458 /// println!("{:?}", err.kind());
459 /// }
460 ///
461 /// fn main() {
462 /// // As no error has (visibly) occurred, this may print anything!
463 /// // It likely prints a placeholder for unidentified (non-)errors.
464 /// print_error(Error::last_os_error());
465 /// // Will print "AddrInUse".
466 /// print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
467 /// }
468 /// ```
469 #[stable(feature = "rust1", since = "1.0.0")]
470 #[must_use]
471 #[inline]
472 pub fn kind(&self) -> ErrorKind {
473 match self.repr.data() {
474 ErrorData::Os(code) => decode_error_kind(code),
475 ErrorData::Custom(c) => c.kind,
476 ErrorData::Simple(kind) => kind,
477 ErrorData::SimpleMessage(m) => m.kind,
478 }
479 }
480
481 #[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
482 #[doc(hidden)]
483 #[inline]
484 pub fn is_interrupted(&self) -> bool {
485 match self.repr.data() {
486 ErrorData::Os(code) => is_interrupted(code),
487 ErrorData::Custom(c) => c.kind == ErrorKind::Interrupted,
488 ErrorData::Simple(kind) => kind == ErrorKind::Interrupted,
489 ErrorData::SimpleMessage(m) => m.kind == ErrorKind::Interrupted,
490 }
491 }
492}
493
494impl fmt::Debug for Repr {
495 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
496 match self.data() {
497 ErrorData::Os(code) => fmt
498 .debug_struct("Os")
499 .field("code", &code)
500 .field("kind", &decode_error_kind(code))
501 .field(
502 "message",
503 &fmt::from_fn(|fmt| {
504 write!(fmt, "\"{}\"", fmt::from_fn(|fmt| format_os_error(code, fmt)))
505 }),
506 )
507 .finish(),
508 ErrorData::Custom(c) => fmt::Debug::fmt(&c, fmt),
509 ErrorData::Simple(kind) => fmt.debug_tuple("Kind").field(&kind).finish(),
510 ErrorData::SimpleMessage(msg) => fmt
511 .debug_struct("Error")
512 .field("kind", &msg.kind)
513 .field("message", &msg.message)
514 .finish(),
515 }
516 }
517}
518
519#[stable(feature = "rust1", since = "1.0.0")]
520impl fmt::Display for Error {
521 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
522 match self.repr.data() {
523 ErrorData::Os(code) => {
524 let detail = fmt::from_fn(|fmt| format_os_error(code, fmt));
525 write!(fmt, "{detail} (os error {code})")
526 }
527 ErrorData::Custom(c) => fmt::Display::fmt(c.error_ref(), fmt),
528 ErrorData::Simple(kind) => kind.fmt(fmt),
529 ErrorData::SimpleMessage(msg) => msg.message.fmt(fmt),
530 }
531 }
532}
533
534#[stable(feature = "rust1", since = "1.0.0")]
535impl error::Error for Error {
536 #[allow(deprecated)]
537 fn cause(&self) -> Option<&dyn error::Error> {
538 match self.repr.data() {
539 ErrorData::Os(..) => None,
540 ErrorData::Simple(..) => None,
541 ErrorData::SimpleMessage(..) => None,
542 ErrorData::Custom(c) => c.error_ref().cause(),
543 }
544 }
545
546 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
547 match self.repr.data() {
548 ErrorData::Os(..) => None,
549 ErrorData::Simple(..) => None,
550 ErrorData::SimpleMessage(..) => None,
551 ErrorData::Custom(c) => c.error_ref().source(),
552 }
553 }
554}
555
556fn _assert_error_is_sync_send() {
557 fn _is_sync_send<T: Sync + Send>() {}
558 _is_sync_send::<Error>();
559}
560
561#[doc(hidden)]
562#[derive(Debug)]
563#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
564pub struct OsFunctions {
565 pub format_os_error: fn(_: RawOsError, _: &mut fmt::Formatter<'_>) -> fmt::Result,
566 pub decode_error_kind: fn(_: RawOsError) -> ErrorKind,
567 pub is_interrupted: fn(_: RawOsError) -> bool,
568}
569
570impl OsFunctions {
571 const DEFAULT: &'static OsFunctions = &OsFunctions {
572 format_os_error: |_, _| Ok(()),
573 decode_error_kind: |_| ErrorKind::Uncategorized,
574 is_interrupted: |_| false,
575 };
576}
577
578// As with `SimpleMessage`: `#[repr(align(4))]` here is just because
579// repr_bitpacked's encoding requires it. In practice it almost certainly be
580// already be this high or higher.
581#[doc(hidden)]
582#[repr(align(4))]
583#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
584pub struct Custom {
585 kind: ErrorKind,
586 error: crate::ptr::NonNull<dyn error::Error + Send + Sync>,
587 error_drop: unsafe fn(*mut (dyn error::Error + Send + Sync)),
588 outer_drop: unsafe fn(*mut Self),
589}
590
591// SAFETY: All members of `Custom` are `Send`
592#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
593unsafe impl Send for Custom {}
594
595// SAFETY: All members of `Custom` are `Sync`
596#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
597unsafe impl Sync for Custom {}
598
599#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
600impl fmt::Debug for Custom {
601 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
602 f.debug_struct("Custom").field("kind", &self.kind).field("error", self.error_ref()).finish()
603 }
604}
605
606#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
607impl Drop for Custom {
608 fn drop(&mut self) {
609 // SAFETY: `Custom::from_raw` ensures this call is safe.
610 unsafe {
611 (self.error_drop)(self.error.as_ptr());
612 }
613 }
614}
615
616impl Custom {
617 /// # Safety
618 ///
619 /// * `error` must be valid for up to a static lifetime, and own its pointee.
620 /// * `error_drop` must be safe to call for the pointer `error` exactly once.
621 /// * `outer_drop` must be safe to call on a pointer to this instance of `Custom`
622 /// if it were stored within a [`CustomOwner`].
623 #[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
624 pub unsafe fn from_raw(
625 kind: ErrorKind,
626 error: crate::ptr::NonNull<dyn error::Error + Send + Sync>,
627 error_drop: unsafe fn(*mut (dyn error::Error + Send + Sync)),
628 outer_drop: unsafe fn(*mut Self),
629 ) -> Custom {
630 Custom { kind, error, error_drop, outer_drop }
631 }
632
633 #[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
634 pub fn into_raw(self) -> crate::ptr::NonNull<dyn error::Error + Send + Sync> {
635 let ptr = self.error;
636 core::mem::forget(self);
637 ptr
638 }
639
640 fn error_ref(&self) -> &(dyn error::Error + Send + Sync + 'static) {
641 // SAFETY:
642 // `from_raw` ensures `error` is a valid pointer up to a static lifetime
643 // and is owned by `self`
644 unsafe { self.error.as_ref() }
645 }
646
647 fn error_mut(&mut self) -> &mut (dyn error::Error + Send + Sync + 'static) {
648 // SAFETY:
649 // `from_raw` ensures `error` is a valid pointer up to a static lifetime
650 // and is owned by `self`
651 unsafe { self.error.as_mut() }
652 }
653}
654
655#[derive(Debug)]
656#[repr(transparent)]
657#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
658#[doc(hidden)]
659pub struct CustomOwner(crate::ptr::NonNull<Custom>);
660
661// SAFETY: Custom is `Send`
662#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
663unsafe impl Send for CustomOwner {}
664
665// SAFETY: Custom is `Sync`
666#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
667unsafe impl Sync for CustomOwner {}
668
669#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
670impl Drop for CustomOwner {
671 fn drop(&mut self) {
672 // SAFETY: `CustomOwner::from_raw` ensures this call is safe.
673 unsafe {
674 (self.0.as_ref().outer_drop)(self.0.as_ptr());
675 }
676 }
677}
678
679impl CustomOwner {
680 /// # Safety
681 ///
682 /// * The `outer_drop` of the provided `custom` must be safe to call exactly once.
683 #[doc(hidden)]
684 #[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
685 pub unsafe fn from_raw(custom: crate::ptr::NonNull<Custom>) -> CustomOwner {
686 CustomOwner(custom)
687 }
688
689 #[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
690 pub fn into_raw(self) -> crate::ptr::NonNull<Custom> {
691 let ptr = self.0;
692 core::mem::forget(self);
693 ptr
694 }
695
696 #[allow(dead_code, reason = "only used for unpacked representation")]
697 fn custom_ref(&self) -> &Custom {
698 // SAFETY:
699 // `from_raw` ensures `0` is a valid pointer up to a static lifetime
700 // and is owned by `self`
701 unsafe { self.0.as_ref() }
702 }
703
704 #[allow(dead_code, reason = "only used for unpacked representation")]
705 fn custom_mut(&mut self) -> &mut Custom {
706 // SAFETY:
707 // `from_raw` ensures `0` is a valid pointer up to a static lifetime
708 // and is owned by `self`
709 unsafe { self.0.as_mut() }
710 }
711}
712
713/// The type of raw OS error codes.
714///
715/// This is an [`i32`] on all currently supported platforms, but platforms
716/// added in the future (such as UEFI) may use a different primitive type like
717/// [`usize`]. Use `as` or [`into`] conversions where applicable to ensure maximum
718/// portability.
719///
720/// [`into`]: Into::into
721#[unstable(feature = "raw_os_error_ty", issue = "107792")]
722pub type RawOsError = cfg_select! {
723 target_os = "uefi" => usize,
724 _ => i32,
725};
726
727/// A list specifying general categories of I/O error.
728///
729/// This list is intended to grow over time and it is not recommended to
730/// exhaustively match against it.
731///
732/// It is used with the [`io::Error`][error] type.
733///
734/// [error]: Error
735///
736/// # Handling errors and matching on `ErrorKind`
737///
738/// In application code, use `match` for the `ErrorKind` values you are
739/// expecting; use `_` to match "all other errors".
740///
741/// In comprehensive and thorough tests that want to verify that a test doesn't
742/// return any known incorrect error kind, you may want to cut-and-paste the
743/// current full list of errors from here into your test code, and then match
744/// `_` as the correct case. This seems counterintuitive, but it will make your
745/// tests more robust. In particular, if you want to verify that your code does
746/// produce an unrecognized error kind, the robust solution is to check for all
747/// the recognized error kinds and fail in those cases.
748#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
749#[stable(feature = "rust1", since = "1.0.0")]
750#[cfg_attr(not(test), rustc_diagnostic_item = "io_errorkind")]
751#[allow(deprecated)]
752#[non_exhaustive]
753pub enum ErrorKind {
754 /// An entity was not found, often a file.
755 #[stable(feature = "rust1", since = "1.0.0")]
756 NotFound,
757 /// The operation lacked the necessary privileges to complete.
758 #[stable(feature = "rust1", since = "1.0.0")]
759 PermissionDenied,
760 /// The connection was refused by the remote server.
761 #[stable(feature = "rust1", since = "1.0.0")]
762 ConnectionRefused,
763 /// The connection was reset by the remote server.
764 #[stable(feature = "rust1", since = "1.0.0")]
765 ConnectionReset,
766 /// The remote host is not reachable.
767 #[stable(feature = "io_error_a_bit_more", since = "1.83.0")]
768 HostUnreachable,
769 /// The network containing the remote host is not reachable.
770 #[stable(feature = "io_error_a_bit_more", since = "1.83.0")]
771 NetworkUnreachable,
772 /// The connection was aborted (terminated) by the remote server.
773 #[stable(feature = "rust1", since = "1.0.0")]
774 ConnectionAborted,
775 /// The network operation failed because it was not connected yet.
776 #[stable(feature = "rust1", since = "1.0.0")]
777 NotConnected,
778 /// A socket address could not be bound because the address is already in
779 /// use elsewhere.
780 #[stable(feature = "rust1", since = "1.0.0")]
781 AddrInUse,
782 /// A nonexistent interface was requested or the requested address was not
783 /// local.
784 #[stable(feature = "rust1", since = "1.0.0")]
785 AddrNotAvailable,
786 /// The system's networking is down.
787 #[stable(feature = "io_error_a_bit_more", since = "1.83.0")]
788 NetworkDown,
789 /// The operation failed because a pipe was closed.
790 #[stable(feature = "rust1", since = "1.0.0")]
791 BrokenPipe,
792 /// An entity already exists, often a file.
793 #[stable(feature = "rust1", since = "1.0.0")]
794 AlreadyExists,
795 /// The operation needs to block to complete, but the blocking operation was
796 /// requested to not occur.
797 #[stable(feature = "rust1", since = "1.0.0")]
798 WouldBlock,
799 /// A filesystem object is, unexpectedly, not a directory.
800 ///
801 /// For example, a filesystem path was specified where one of the intermediate directory
802 /// components was, in fact, a plain file.
803 #[stable(feature = "io_error_a_bit_more", since = "1.83.0")]
804 NotADirectory,
805 /// The filesystem object is, unexpectedly, a directory.
806 ///
807 /// A directory was specified when a non-directory was expected.
808 #[stable(feature = "io_error_a_bit_more", since = "1.83.0")]
809 IsADirectory,
810 /// A non-empty directory was specified where an empty directory was expected.
811 #[stable(feature = "io_error_a_bit_more", since = "1.83.0")]
812 DirectoryNotEmpty,
813 /// The filesystem or storage medium is read-only, but a write operation was attempted.
814 #[stable(feature = "io_error_a_bit_more", since = "1.83.0")]
815 ReadOnlyFilesystem,
816 /// Loop in the filesystem or IO subsystem; often, too many levels of symbolic links.
817 ///
818 /// There was a loop (or excessively long chain) resolving a filesystem object
819 /// or file IO object.
820 ///
821 /// On Unix this is usually the result of a symbolic link loop; or, of exceeding the
822 /// system-specific limit on the depth of symlink traversal.
823 #[unstable(feature = "io_error_more", issue = "86442")]
824 FilesystemLoop,
825 /// Stale network file handle.
826 ///
827 /// With some network filesystems, notably NFS, an open file (or directory) can be invalidated
828 /// by problems with the network or server.
829 #[stable(feature = "io_error_a_bit_more", since = "1.83.0")]
830 StaleNetworkFileHandle,
831 /// A parameter was incorrect.
832 #[stable(feature = "rust1", since = "1.0.0")]
833 InvalidInput,
834 /// Data not valid for the operation were encountered.
835 ///
836 /// Unlike [`InvalidInput`], this typically means that the operation
837 /// parameters were valid, however the error was caused by malformed
838 /// input data.
839 ///
840 /// For example, a function that reads a file into a string will error with
841 /// `InvalidData` if the file's contents are not valid UTF-8.
842 ///
843 /// [`InvalidInput`]: ErrorKind::InvalidInput
844 #[stable(feature = "io_invalid_data", since = "1.2.0")]
845 InvalidData,
846 /// The I/O operation's timeout expired, causing it to be canceled.
847 #[stable(feature = "rust1", since = "1.0.0")]
848 TimedOut,
849 /// An error returned when an operation could not be completed because a
850 /// call to [`write`][write] returned [`Ok(0)`].
851 ///
852 /// This typically means that an operation could only succeed if it wrote a
853 /// particular number of bytes but only a smaller number of bytes could be
854 /// written.
855 ///
856 /// [write]: ../../std/io/trait.Write.html#tymethod.write
857 /// [`Ok(0)`]: Ok
858 #[stable(feature = "rust1", since = "1.0.0")]
859 WriteZero,
860 /// The underlying storage (typically, a filesystem) is full.
861 ///
862 /// This does not include out of quota errors.
863 #[stable(feature = "io_error_a_bit_more", since = "1.83.0")]
864 StorageFull,
865 /// Seek on unseekable file.
866 ///
867 /// Seeking was attempted on an open file handle which is not suitable for seeking - for
868 /// example, on Unix, a named pipe opened with `File::open`.
869 #[stable(feature = "io_error_a_bit_more", since = "1.83.0")]
870 NotSeekable,
871 /// Filesystem quota or some other kind of quota was exceeded.
872 #[stable(feature = "io_error_quota_exceeded", since = "1.85.0")]
873 QuotaExceeded,
874 /// File larger than allowed or supported.
875 ///
876 /// This might arise from a hard limit of the underlying filesystem or file access API, or from
877 /// an administratively imposed resource limitation. Simple disk full, and out of quota, have
878 /// their own errors.
879 #[stable(feature = "io_error_a_bit_more", since = "1.83.0")]
880 FileTooLarge,
881 /// Resource is busy.
882 #[stable(feature = "io_error_a_bit_more", since = "1.83.0")]
883 ResourceBusy,
884 /// Executable file is busy.
885 ///
886 /// An attempt was made to write to a file which is also in use as a running program. (Not all
887 /// operating systems detect this situation.)
888 #[stable(feature = "io_error_a_bit_more", since = "1.83.0")]
889 ExecutableFileBusy,
890 /// Deadlock (avoided).
891 ///
892 /// A file locking operation would result in deadlock. This situation is typically detected, if
893 /// at all, on a best-effort basis.
894 #[stable(feature = "io_error_a_bit_more", since = "1.83.0")]
895 Deadlock,
896 /// Cross-device or cross-filesystem (hard) link or rename.
897 #[stable(feature = "io_error_crosses_devices", since = "1.85.0")]
898 CrossesDevices,
899 /// Too many (hard) links to the same filesystem object.
900 ///
901 /// The filesystem does not support making so many hardlinks to the same file.
902 #[stable(feature = "io_error_a_bit_more", since = "1.83.0")]
903 TooManyLinks,
904 /// A filename was invalid.
905 ///
906 /// This error can also occur if a length limit for a name was exceeded.
907 #[stable(feature = "io_error_invalid_filename", since = "1.87.0")]
908 InvalidFilename,
909 /// Program argument list too long.
910 ///
911 /// When trying to run an external program, a system or process limit on the size of the
912 /// arguments would have been exceeded.
913 #[stable(feature = "io_error_a_bit_more", since = "1.83.0")]
914 ArgumentListTooLong,
915 /// This operation was interrupted.
916 ///
917 /// Interrupted operations can typically be retried.
918 #[stable(feature = "rust1", since = "1.0.0")]
919 Interrupted,
920
921 /// This operation is unsupported on this platform.
922 ///
923 /// This means that the operation can never succeed.
924 #[stable(feature = "unsupported_error", since = "1.53.0")]
925 Unsupported,
926
927 // ErrorKinds which are primarily categorisations for OS error
928 // codes should be added above.
929 //
930 /// An error returned when an operation could not be completed because an
931 /// "end of file" was reached prematurely.
932 ///
933 /// This typically means that an operation could only succeed if it read a
934 /// particular number of bytes but only a smaller number of bytes could be
935 /// read.
936 #[stable(feature = "read_exact", since = "1.6.0")]
937 UnexpectedEof,
938
939 /// An operation could not be completed, because it failed
940 /// to allocate enough memory.
941 #[stable(feature = "out_of_memory_error", since = "1.54.0")]
942 OutOfMemory,
943
944 /// The operation was partially successful and needs to be checked
945 /// later on due to not blocking.
946 #[unstable(feature = "io_error_inprogress", issue = "130840")]
947 InProgress,
948
949 /// The process or the whole system has reached its limit on the number of
950 /// open files or sockets.
951 #[unstable(feature = "io_error_too_many_open_files", issue = "158319")]
952 TooManyOpenFiles,
953
954 // "Unusual" error kinds which do not correspond simply to (sets
955 // of) OS error codes, should be added just above this comment.
956 // `Other` and `Uncategorized` should remain at the end:
957 //
958 /// A custom error that does not fall under any other I/O error kind.
959 ///
960 /// This can be used to construct your own [`Error`][error]s that do not match any
961 /// [`ErrorKind`].
962 ///
963 /// This [`ErrorKind`] is not used by the standard library.
964 ///
965 /// Errors from the standard library that do not fall under any of the I/O
966 /// error kinds cannot be `match`ed on, and will only match a wildcard (`_`) pattern.
967 /// New [`ErrorKind`]s might be added in the future for some of those.
968 ///
969 /// [error]: Error
970 #[stable(feature = "rust1", since = "1.0.0")]
971 Other,
972
973 /// Any I/O error from the standard library that's not part of this list.
974 ///
975 /// Errors that are `Uncategorized` now may move to a different or a new
976 /// [`ErrorKind`] variant in the future. It is not recommended to match
977 /// an error against `Uncategorized`; use a wildcard match (`_`) instead.
978 #[unstable(feature = "io_error_uncategorized", issue = "none")]
979 #[doc(hidden)]
980 Uncategorized,
981}
982
983impl ErrorKind {
984 const fn as_str(&self) -> &'static str {
985 use ErrorKind::*;
986 match *self {
987 // tidy-alphabetical-start
988 AddrInUse => "address in use",
989 AddrNotAvailable => "address not available",
990 AlreadyExists => "entity already exists",
991 ArgumentListTooLong => "argument list too long",
992 BrokenPipe => "broken pipe",
993 ConnectionAborted => "connection aborted",
994 ConnectionRefused => "connection refused",
995 ConnectionReset => "connection reset",
996 CrossesDevices => "cross-device link or rename",
997 Deadlock => "deadlock",
998 DirectoryNotEmpty => "directory not empty",
999 ExecutableFileBusy => "executable file busy",
1000 FileTooLarge => "file too large",
1001 FilesystemLoop => "filesystem loop or indirection limit (e.g. symlink loop)",
1002 HostUnreachable => "host unreachable",
1003 InProgress => "in progress",
1004 Interrupted => "operation interrupted",
1005 InvalidData => "invalid data",
1006 InvalidFilename => "invalid filename",
1007 InvalidInput => "invalid input parameter",
1008 IsADirectory => "is a directory",
1009 NetworkDown => "network down",
1010 NetworkUnreachable => "network unreachable",
1011 NotADirectory => "not a directory",
1012 NotConnected => "not connected",
1013 NotFound => "entity not found",
1014 NotSeekable => "seek on unseekable file",
1015 Other => "other error",
1016 OutOfMemory => "out of memory",
1017 PermissionDenied => "permission denied",
1018 QuotaExceeded => "quota exceeded",
1019 ReadOnlyFilesystem => "read-only filesystem or storage medium",
1020 ResourceBusy => "resource busy",
1021 StaleNetworkFileHandle => "stale network file handle",
1022 StorageFull => "no storage space",
1023 TimedOut => "timed out",
1024 TooManyLinks => "too many links",
1025 TooManyOpenFiles => "too many open files",
1026 Uncategorized => "uncategorized error",
1027 UnexpectedEof => "unexpected end of file",
1028 Unsupported => "unsupported",
1029 WouldBlock => "operation would block",
1030 WriteZero => "write zero",
1031 // tidy-alphabetical-end
1032 }
1033 }
1034
1035 // This compiles to the same code as the check+transmute, but doesn't require
1036 // unsafe, or to hard-code max ErrorKind or its size in a way the compiler
1037 // couldn't verify.
1038 #[inline]
1039 #[allow(dead_code, reason = "only used for packed representation")]
1040 const fn from_prim(ek: u32) -> Option<Self> {
1041 macro_rules! from_prim {
1042 ($prim:expr => $Enum:ident { $($Variant:ident),* $(,)? }) => {{
1043 // Force a compile error if the list gets out of date.
1044 const _: fn(e: $Enum) = |e: $Enum| match e {
1045 $($Enum::$Variant => (),)*
1046 };
1047 match $prim {
1048 $(v if v == ($Enum::$Variant as _) => Some($Enum::$Variant),)*
1049 _ => None,
1050 }
1051 }}
1052 }
1053 from_prim!(ek => ErrorKind {
1054 NotFound,
1055 PermissionDenied,
1056 ConnectionRefused,
1057 ConnectionReset,
1058 HostUnreachable,
1059 NetworkUnreachable,
1060 ConnectionAborted,
1061 NotConnected,
1062 AddrInUse,
1063 AddrNotAvailable,
1064 NetworkDown,
1065 BrokenPipe,
1066 AlreadyExists,
1067 WouldBlock,
1068 NotADirectory,
1069 IsADirectory,
1070 DirectoryNotEmpty,
1071 ReadOnlyFilesystem,
1072 FilesystemLoop,
1073 StaleNetworkFileHandle,
1074 InvalidInput,
1075 InvalidData,
1076 TimedOut,
1077 WriteZero,
1078 StorageFull,
1079 NotSeekable,
1080 QuotaExceeded,
1081 FileTooLarge,
1082 ResourceBusy,
1083 ExecutableFileBusy,
1084 Deadlock,
1085 CrossesDevices,
1086 TooManyLinks,
1087 InvalidFilename,
1088 ArgumentListTooLong,
1089 Interrupted,
1090 Other,
1091 UnexpectedEof,
1092 Unsupported,
1093 OutOfMemory,
1094 InProgress,
1095 TooManyOpenFiles,
1096 Uncategorized,
1097 })
1098 }
1099}
1100
1101#[stable(feature = "io_errorkind_display", since = "1.60.0")]
1102impl fmt::Display for ErrorKind {
1103 /// Shows a human-readable description of the [`ErrorKind`].
1104 ///
1105 /// This is similar to `impl Display for Error`, but doesn't require first converting to Error.
1106 ///
1107 /// # Examples
1108 /// ```
1109 /// use core::io::ErrorKind;
1110 /// assert_eq!("entity not found", ErrorKind::NotFound.to_string());
1111 /// ```
1112 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1113 fmt.write_str(self.as_str())
1114 }
1115}