Skip to main content

base_scheduler/
address_resolve_error.rs

1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4use crate::AddressResolveErrorKind;
5use std::{fmt, io};
6
7/// Resolver-specific details stored inside `OperationError::Io`. Recover this value with `io_error.get_ref().and_then(|error| error.downcast_ref::<AddressResolveError>())`.
8#[derive(Debug)]
9pub struct AddressResolveError {
10    /// The portable failure category, which selects the [`io::ErrorKind`] when converted into an [`io::Error`].
11    pub kind: AddressResolveErrorKind,
12    /// A backend-specific resolver code (`EAI_*` or Winsock). Simulators may omit it. Do not interpret it as errno or carry it between platforms.
13    pub code: Option<i32>,
14    /// A human-readable description, used as this error's `Display` output.
15    pub message: String,
16    /// An underlying system failure, such as errno captured immediately after `EAI_SYSTEM`.
17    pub system_error: Option<io::Error>,
18}
19
20impl fmt::Display for AddressResolveError {
21    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
22        self.message.fmt(formatter)
23    }
24}
25
26impl std::error::Error for AddressResolveError {
27    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
28        self.system_error.as_ref().map(|error| error as _)
29    }
30}
31
32impl From<AddressResolveError> for io::Error {
33    fn from(error: AddressResolveError) -> Self {
34        use AddressResolveErrorKind::*;
35        let kind = match error.kind {
36            TemporaryFailure => io::ErrorKind::WouldBlock,
37            NameNotFound | ServiceUnavailable => io::ErrorKind::NotFound,
38            InvalidHints => io::ErrorKind::InvalidInput,
39            OutOfMemory => io::ErrorKind::OutOfMemory,
40            System => error
41                .system_error
42                .as_ref()
43                .map_or(io::ErrorKind::Other, io::Error::kind),
44            PermanentFailure | Other => io::ErrorKind::Other,
45        };
46        Self::new(kind, error)
47    }
48}