2015-02-06 00:50:11 +00:00
|
|
|
//! A private parser implementation of IPv4, IPv6, and socket addresses.
|
|
|
|
//!
|
|
|
|
//! This module is "publicly exported" through the `FromStr` implementations
|
|
|
|
//! below.
|
|
|
|
|
2020-08-27 13:45:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2020-10-04 12:39:39 +00:00
|
|
|
use crate::convert::TryInto as _;
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::error::Error;
|
|
|
|
use crate::fmt;
|
|
|
|
use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
|
|
|
|
use crate::str::FromStr;
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2020-10-04 12:39:39 +00:00
|
|
|
trait ReadNumberHelper: crate::marker::Sized {
|
|
|
|
const ZERO: Self;
|
|
|
|
fn checked_mul(&self, other: u32) -> Option<Self>;
|
|
|
|
fn checked_add(&self, other: u32) -> Option<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_helper {
|
|
|
|
($($t:ty)*) => ($(impl ReadNumberHelper for $t {
|
|
|
|
const ZERO: Self = 0;
|
|
|
|
#[inline]
|
|
|
|
fn checked_mul(&self, other: u32) -> Option<Self> {
|
|
|
|
Self::checked_mul(*self, other.try_into().ok()?)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn checked_add(&self, other: u32) -> Option<Self> {
|
|
|
|
Self::checked_add(*self, other.try_into().ok()?)
|
|
|
|
}
|
|
|
|
})*)
|
|
|
|
}
|
|
|
|
|
2020-10-04 21:56:48 +00:00
|
|
|
impl_helper! { u8 u16 u32 }
|
2020-10-04 12:39:39 +00:00
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
struct Parser<'a> {
|
|
|
|
// parsing as ASCII, so can use byte array
|
2020-05-20 03:26:49 +00:00
|
|
|
state: &'a [u8],
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Parser<'a> {
|
2020-05-20 03:26:49 +00:00
|
|
|
fn new(input: &'a str) -> Parser<'a> {
|
|
|
|
Parser { state: input.as_bytes() }
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
/// Run a parser, and restore the pre-parse state if it fails
|
|
|
|
fn read_atomically<T, F>(&mut self, inner: F) -> Option<T>
|
2019-11-27 18:29:00 +00:00
|
|
|
where
|
2019-03-01 08:34:11 +00:00
|
|
|
F: FnOnce(&mut Parser<'_>) -> Option<T>,
|
2015-02-06 00:50:11 +00:00
|
|
|
{
|
2020-05-20 03:26:49 +00:00
|
|
|
let state = self.state;
|
|
|
|
let result = inner(self);
|
|
|
|
if result.is_none() {
|
|
|
|
self.state = state;
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
2020-05-20 03:26:49 +00:00
|
|
|
result
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
/// Run a parser, but fail if the entire input wasn't consumed.
|
|
|
|
/// Doesn't run atomically.
|
|
|
|
fn parse_with<T, F>(&mut self, inner: F) -> Result<T, AddrParseError>
|
2019-11-27 18:29:00 +00:00
|
|
|
where
|
2020-05-20 03:26:49 +00:00
|
|
|
F: FnOnce(&mut Parser<'_>) -> Option<T>,
|
2015-02-06 00:50:11 +00:00
|
|
|
{
|
2020-10-04 17:07:30 +00:00
|
|
|
let result = inner(self);
|
|
|
|
if self.state.is_empty() { result } else { None }.ok_or(AddrParseError(()))
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
/// Read the next character from the input
|
2015-02-06 00:50:11 +00:00
|
|
|
fn read_char(&mut self) -> Option<char> {
|
2020-05-20 03:26:49 +00:00
|
|
|
self.state.split_first().map(|(&b, tail)| {
|
|
|
|
self.state = tail;
|
2020-10-04 12:39:39 +00:00
|
|
|
char::from(b)
|
2015-02-06 00:50:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-04 21:46:49 +00:00
|
|
|
#[must_use]
|
|
|
|
/// Read the next character from the input if it matches the target.
|
|
|
|
fn read_given_char(&mut self, target: char) -> Option<()> {
|
|
|
|
self.read_atomically(|p| {
|
|
|
|
p.read_char().and_then(|c| if c == target { Some(()) } else { None })
|
|
|
|
})
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
/// Helper for reading separators in an indexed loop. Reads the separator
|
|
|
|
/// character iff index > 0, then runs the parser. When used in a loop,
|
|
|
|
/// the separator character will only be read on index > 0 (see
|
|
|
|
/// read_ipv4_addr for an example)
|
|
|
|
fn read_separator<T, F>(&mut self, sep: char, index: usize, inner: F) -> Option<T>
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Parser<'_>) -> Option<T>,
|
|
|
|
{
|
|
|
|
self.read_atomically(move |p| {
|
|
|
|
if index > 0 {
|
2020-10-04 21:46:49 +00:00
|
|
|
p.read_given_char(sep)?;
|
2020-05-20 03:26:49 +00:00
|
|
|
}
|
|
|
|
inner(p)
|
|
|
|
})
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
// Read a number off the front of the input in the given radix, stopping
|
|
|
|
// at the first non-digit character or eof. Fails if the number has more
|
2020-10-04 12:39:39 +00:00
|
|
|
// digits than max_digits or if there is no number.
|
|
|
|
fn read_number<T: ReadNumberHelper>(
|
|
|
|
&mut self,
|
|
|
|
radix: u32,
|
|
|
|
max_digits: Option<usize>,
|
|
|
|
) -> Option<T> {
|
2020-05-20 03:26:49 +00:00
|
|
|
self.read_atomically(move |p| {
|
2020-10-04 12:39:39 +00:00
|
|
|
let mut result = T::ZERO;
|
2020-05-20 03:26:49 +00:00
|
|
|
let mut digit_count = 0;
|
|
|
|
|
2020-10-04 12:39:39 +00:00
|
|
|
while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) {
|
|
|
|
result = result.checked_mul(radix)?;
|
|
|
|
result = result.checked_add(digit)?;
|
2020-05-20 03:26:49 +00:00
|
|
|
digit_count += 1;
|
2020-10-04 12:39:39 +00:00
|
|
|
if let Some(max_digits) = max_digits {
|
|
|
|
if digit_count > max_digits {
|
|
|
|
return None;
|
|
|
|
}
|
2020-05-20 03:26:49 +00:00
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
if digit_count == 0 { None } else { Some(result) }
|
|
|
|
})
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
/// Read an IPv4 address
|
2015-02-06 00:50:11 +00:00
|
|
|
fn read_ipv4_addr(&mut self) -> Option<Ipv4Addr> {
|
2020-05-20 03:26:49 +00:00
|
|
|
self.read_atomically(|p| {
|
|
|
|
let mut groups = [0; 4];
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
for (i, slot) in groups.iter_mut().enumerate() {
|
2020-10-04 12:39:39 +00:00
|
|
|
*slot = p.read_separator('.', i, |p| p.read_number(10, None))?;
|
2020-05-20 03:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(groups.into())
|
|
|
|
})
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
/// Read an IPV6 Address
|
|
|
|
fn read_ipv6_addr(&mut self) -> Option<Ipv6Addr> {
|
|
|
|
/// Read a chunk of an ipv6 address into `groups`. Returns the number
|
|
|
|
/// of groups read, along with a bool indicating if an embedded
|
|
|
|
/// trailing ipv4 address was read. Specifically, read a series of
|
|
|
|
/// colon-separated ipv6 groups (0x0000 - 0xFFFF), with an optional
|
|
|
|
/// trailing embedded ipv4 address.
|
|
|
|
fn read_groups(p: &mut Parser<'_>, groups: &mut [u16]) -> (usize, bool) {
|
|
|
|
let limit = groups.len();
|
|
|
|
|
|
|
|
for (i, slot) in groups.iter_mut().enumerate() {
|
|
|
|
// Try to read a trailing embedded ipv4 address. There must be
|
|
|
|
// at least two groups left.
|
2015-02-06 00:50:11 +00:00
|
|
|
if i < limit - 1 {
|
2020-05-20 03:26:49 +00:00
|
|
|
let ipv4 = p.read_separator(':', i, |p| p.read_ipv4_addr());
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
if let Some(v4_addr) = ipv4 {
|
2020-10-04 12:39:39 +00:00
|
|
|
let [one, two, three, four] = v4_addr.octets();
|
|
|
|
groups[i + 0] = u16::from_be_bytes([one, two]);
|
|
|
|
groups[i + 1] = u16::from_be_bytes([three, four]);
|
2015-02-06 00:50:11 +00:00
|
|
|
return (i + 2, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-04 12:39:39 +00:00
|
|
|
let group = p.read_separator(':', i, |p| p.read_number(16, Some(4)));
|
2020-05-20 03:26:49 +00:00
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
match group {
|
2020-10-04 12:39:39 +00:00
|
|
|
Some(g) => *slot = g,
|
2019-11-27 18:29:00 +00:00
|
|
|
None => return (i, false),
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-20 03:26:49 +00:00
|
|
|
(groups.len(), false)
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
self.read_atomically(|p| {
|
|
|
|
// Read the front part of the address; either the whole thing, or up
|
|
|
|
// to the first ::
|
|
|
|
let mut head = [0; 8];
|
|
|
|
let (head_size, head_ipv4) = read_groups(p, &mut head);
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
if head_size == 8 {
|
|
|
|
return Some(head.into());
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
// IPv4 part is not allowed before `::`
|
|
|
|
if head_ipv4 {
|
|
|
|
return None;
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
// read `::` if previous code parsed less than 8 groups
|
|
|
|
// `::` indicates one or more groups of 16 bits of zeros
|
2020-10-04 21:46:49 +00:00
|
|
|
p.read_given_char(':')?;
|
|
|
|
p.read_given_char(':')?;
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
// Read the back part of the address. The :: must contain at least one
|
|
|
|
// set of zeroes, so our max length is 7.
|
|
|
|
let mut tail = [0; 7];
|
|
|
|
let limit = 8 - (head_size + 1);
|
|
|
|
let (tail_size, _) = read_groups(p, &mut tail[..limit]);
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
// Concat the head and tail of the IP address
|
|
|
|
head[(8 - tail_size)..8].copy_from_slice(&tail[..tail_size]);
|
|
|
|
|
|
|
|
Some(head.into())
|
|
|
|
})
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
/// Read an IP Address, either IPV4 or IPV6.
|
2015-02-06 00:50:11 +00:00
|
|
|
fn read_ip_addr(&mut self) -> Option<IpAddr> {
|
2020-05-20 03:26:49 +00:00
|
|
|
self.read_ipv4_addr().map(IpAddr::V4).or_else(move || self.read_ipv6_addr().map(IpAddr::V6))
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 12:39:39 +00:00
|
|
|
/// Read a : followed by a port in base 10.
|
2020-05-20 03:26:49 +00:00
|
|
|
fn read_port(&mut self) -> Option<u16> {
|
|
|
|
self.read_atomically(|p| {
|
2020-10-04 21:46:49 +00:00
|
|
|
p.read_given_char(':')?;
|
2020-10-04 12:39:39 +00:00
|
|
|
p.read_number(10, None)
|
2020-05-20 03:26:49 +00:00
|
|
|
})
|
|
|
|
}
|
2015-10-20 18:35:05 +00:00
|
|
|
|
2020-10-04 21:56:48 +00:00
|
|
|
/// Read a % followed by a scope id in base 10.
|
|
|
|
fn read_scope_id(&mut self) -> Option<u32> {
|
|
|
|
self.read_atomically(|p| {
|
|
|
|
p.read_given_char('%')?;
|
|
|
|
p.read_number(10, None)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
/// Read an IPV4 address with a port
|
|
|
|
fn read_socket_addr_v4(&mut self) -> Option<SocketAddrV4> {
|
|
|
|
self.read_atomically(|p| {
|
|
|
|
let ip = p.read_ipv4_addr()?;
|
|
|
|
let port = p.read_port()?;
|
|
|
|
Some(SocketAddrV4::new(ip, port))
|
2015-10-20 18:35:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
/// Read an IPV6 address with a port
|
2015-10-20 18:35:05 +00:00
|
|
|
fn read_socket_addr_v6(&mut self) -> Option<SocketAddrV6> {
|
2020-05-20 03:26:49 +00:00
|
|
|
self.read_atomically(|p| {
|
2020-10-04 21:46:49 +00:00
|
|
|
p.read_given_char('[')?;
|
2020-05-20 03:26:49 +00:00
|
|
|
let ip = p.read_ipv6_addr()?;
|
2020-10-04 21:56:48 +00:00
|
|
|
let scope_id = p.read_scope_id().unwrap_or(0);
|
2020-10-04 21:46:49 +00:00
|
|
|
p.read_given_char(']')?;
|
2020-05-20 03:26:49 +00:00
|
|
|
|
|
|
|
let port = p.read_port()?;
|
2020-10-04 21:56:48 +00:00
|
|
|
Some(SocketAddrV6::new(ip, port, 0, scope_id))
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
})
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
2015-10-20 18:35:05 +00:00
|
|
|
|
2020-05-20 03:26:49 +00:00
|
|
|
/// Read an IP address with a port
|
2015-10-20 18:35:05 +00:00
|
|
|
fn read_socket_addr(&mut self) -> Option<SocketAddr> {
|
2019-12-22 22:42:04 +00:00
|
|
|
self.read_socket_addr_v4()
|
|
|
|
.map(SocketAddr::V4)
|
2019-12-06 12:27:39 +00:00
|
|
|
.or_else(|| self.read_socket_addr_v6().map(SocketAddr::V6))
|
2015-10-20 18:35:05 +00:00
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-28 10:28:42 +00:00
|
|
|
#[stable(feature = "ip_addr", since = "1.7.0")]
|
2015-03-26 20:31:37 +00:00
|
|
|
impl FromStr for IpAddr {
|
|
|
|
type Err = AddrParseError;
|
|
|
|
fn from_str(s: &str) -> Result<IpAddr, AddrParseError> {
|
2020-05-20 03:26:49 +00:00
|
|
|
Parser::new(s).parse_with(|p| p.read_ip_addr())
|
2015-03-26 20:31:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 03:08:15 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-06 00:50:11 +00:00
|
|
|
impl FromStr for Ipv4Addr {
|
2015-03-19 03:08:15 +00:00
|
|
|
type Err = AddrParseError;
|
|
|
|
fn from_str(s: &str) -> Result<Ipv4Addr, AddrParseError> {
|
2020-05-20 03:26:49 +00:00
|
|
|
Parser::new(s).parse_with(|p| p.read_ipv4_addr())
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 03:08:15 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-06 00:50:11 +00:00
|
|
|
impl FromStr for Ipv6Addr {
|
2015-03-19 03:08:15 +00:00
|
|
|
type Err = AddrParseError;
|
|
|
|
fn from_str(s: &str) -> Result<Ipv6Addr, AddrParseError> {
|
2020-05-20 03:26:49 +00:00
|
|
|
Parser::new(s).parse_with(|p| p.read_ipv6_addr())
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:35:05 +00:00
|
|
|
#[stable(feature = "socket_addr_from_str", since = "1.5.0")]
|
|
|
|
impl FromStr for SocketAddrV4 {
|
|
|
|
type Err = AddrParseError;
|
|
|
|
fn from_str(s: &str) -> Result<SocketAddrV4, AddrParseError> {
|
2020-05-20 03:26:49 +00:00
|
|
|
Parser::new(s).parse_with(|p| p.read_socket_addr_v4())
|
2015-10-20 18:35:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "socket_addr_from_str", since = "1.5.0")]
|
|
|
|
impl FromStr for SocketAddrV6 {
|
|
|
|
type Err = AddrParseError;
|
|
|
|
fn from_str(s: &str) -> Result<SocketAddrV6, AddrParseError> {
|
2020-05-20 03:26:49 +00:00
|
|
|
Parser::new(s).parse_with(|p| p.read_socket_addr_v6())
|
2015-10-20 18:35:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 03:08:15 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-06 00:50:11 +00:00
|
|
|
impl FromStr for SocketAddr {
|
2015-03-19 03:08:15 +00:00
|
|
|
type Err = AddrParseError;
|
|
|
|
fn from_str(s: &str) -> Result<SocketAddr, AddrParseError> {
|
2020-05-20 03:26:49 +00:00
|
|
|
Parser::new(s).parse_with(|p| p.read_socket_addr())
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 18:23:13 +00:00
|
|
|
/// An error which can be returned when parsing an IP address or a socket address.
|
|
|
|
///
|
|
|
|
/// This error is used as the error type for the [`FromStr`] implementation for
|
|
|
|
/// [`IpAddr`], [`Ipv4Addr`], [`Ipv6Addr`], [`SocketAddr`], [`SocketAddrV4`], and
|
|
|
|
/// [`SocketAddrV6`].
|
|
|
|
///
|
2018-03-08 22:18:45 +00:00
|
|
|
/// # Potential causes
|
|
|
|
///
|
|
|
|
/// `AddrParseError` may be thrown because the provided string does not parse as the given type,
|
|
|
|
/// often because it includes information only handled by a different address type.
|
|
|
|
///
|
|
|
|
/// ```should_panic
|
|
|
|
/// use std::net::IpAddr;
|
|
|
|
/// let _foo: IpAddr = "127.0.0.1:8080".parse().expect("Cannot handle the socket port");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// [`IpAddr`] doesn't handle the port. Use [`SocketAddr`] instead.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::net::SocketAddr;
|
|
|
|
///
|
|
|
|
/// // No problem, the `panic!` message has disappeared.
|
|
|
|
/// let _foo: SocketAddr = "127.0.0.1:8080".parse().expect("unreachable panic");
|
|
|
|
/// ```
|
2015-03-19 03:08:15 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-09-12 19:37:41 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2015-03-19 03:08:15 +00:00
|
|
|
pub struct AddrParseError(());
|
2015-08-24 06:00:18 +00:00
|
|
|
|
2015-08-24 15:59:45 +00:00
|
|
|
#[stable(feature = "addr_parse_error_error", since = "1.4.0")]
|
2015-08-24 06:00:18 +00:00
|
|
|
impl fmt::Display for AddrParseError {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated, deprecated_in_future)]
|
2019-03-01 08:34:11 +00:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-08-24 06:00:18 +00:00
|
|
|
fmt.write_str(self.description())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 15:59:45 +00:00
|
|
|
#[stable(feature = "addr_parse_error_error", since = "1.4.0")]
|
2015-08-24 06:00:18 +00:00
|
|
|
impl Error for AddrParseError {
|
2019-12-01 04:01:48 +00:00
|
|
|
#[allow(deprecated)]
|
2015-08-24 06:00:18 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
"invalid IP address syntax"
|
|
|
|
}
|
|
|
|
}
|