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    pub kind: AddressResolveErrorKind,
11    /// A backend-specific resolver code (`EAI_*` or Winsock). Simulators may omit it. Do not interpret it as errno or carry it between platforms.
12    pub code: Option<i32>,
13    pub message: String,
14    /// An underlying system failure, such as errno captured immediately after `EAI_SYSTEM`.
15    pub system_error: Option<io::Error>,
16}
17
18impl fmt::Display for AddressResolveError {
19    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
20        self.message.fmt(formatter)
21    }
22}
23
24impl std::error::Error for AddressResolveError {
25    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
26        self.system_error.as_ref().map(|error| error as _)
27    }
28}
29
30impl From<AddressResolveError> for io::Error {
31    fn from(error: AddressResolveError) -> Self {
32        use AddressResolveErrorKind::*;
33        let kind = match error.kind {
34            TemporaryFailure => io::ErrorKind::WouldBlock,
35            NameNotFound | ServiceUnavailable => io::ErrorKind::NotFound,
36            InvalidHints => io::ErrorKind::InvalidInput,
37            OutOfMemory => io::ErrorKind::OutOfMemory,
38            System => error
39                .system_error
40                .as_ref()
41                .map_or(io::ErrorKind::Other, io::Error::kind),
42            PermanentFailure | Other => io::ErrorKind::Other,
43        };
44        Self::new(kind, error)
45    }
46}