Trait std::net::ToSocketAddrs
[−]
[src]
pub trait ToSocketAddrs { type Iter: Iterator<Item=SocketAddr>; fn to_socket_addrs(&self) -> Result<Self::Iter>; }
A trait for objects which can be converted or resolved to one or more
SocketAddr
`SocketAddr` values.
This trait is used for generic address resolution when constructing network objects. By default it is implemented for the following types:
SocketAddr
`SocketAddr,
`,SocketAddrV4
`SocketAddrV4,
`,SocketAddrV6
`SocketAddrV6-
` -to_socket_addrs
`to_socket_addrs` is identity function.(IpvNAddr, u16)
`(IpvNAddr, u16)-
` -to_socket_addrs
`to_socket_addrsconstructs
` constructsSocketAddr
`SocketAddr` trivially.(&str, u16)
`(&str, u16)- the string should be either a string representation of an IP address expected by
` - the string should be either a string representation of an IP address expected byFromStr
`FromStrimplementation for
` implementation forIpvNAddr
`IpvNAddr` or a host name.&str
`&str- the string should be either a string representation of a
` - the string should be either a string representation of aSocketAddr
`SocketAddras expected by its
` as expected by itsFromStr
`FromStrimplementation or a string like
` implementation or a string like<host_name>:<port>
`: pair where
` pair where<port>
`is a
` is au16
`u16` value.
This trait allows constructing network objects like TcpStream
`TcpStreamor
` or
UdpSocket
`UdpSocketeasily with values of various types for the bind/connection address. It is needed because sometimes one type is more appropriate than the other: for simple uses a string like
` easily with values of various types for the bind/connection
address. It is needed because sometimes one type is more appropriate than
the other: for simple uses a string like "localhost:12345"
`"localhost:12345"is much nicer than manual construction of the corresponding
` is much nicer
than manual construction of the corresponding SocketAddr
`SocketAddr, but sometimes
`, but sometimes
SocketAddr
`SocketAddrvalue is *the* main source of the address, and converting it to some other type (e.g. a string) just for it to be converted back to
` value is the main source of the address, and converting it to
some other type (e.g. a string) just for it to be converted back to
SocketAddr
`SocketAddr` in constructor methods is pointless.
Some examples:
use std::net::{SocketAddrV4, TcpStream, UdpSocket, TcpListener, Ipv4Addr}; fn main() { let ip = Ipv4Addr::new(127, 0, 0, 1); let port = 12345; // The following lines are equivalent modulo possible "localhost" name // resolution differences let tcp_s = TcpStream::connect(SocketAddrV4::new(ip, port)); let tcp_s = TcpStream::connect((ip, port)); let tcp_s = TcpStream::connect(("127.0.0.1", port)); let tcp_s = TcpStream::connect(("localhost", port)); let tcp_s = TcpStream::connect("127.0.0.1:12345"); let tcp_s = TcpStream::connect("localhost:12345"); // TcpListener::bind(), UdpSocket::bind() and UdpSocket::send_to() // behave similarly let tcp_l = TcpListener::bind("localhost:12345"); let mut udp_s = UdpSocket::bind(("127.0.0.1", port)).unwrap(); udp_s.send_to(&[7], (ip, 23451)).unwrap(); }use std::net::{SocketAddrV4, TcpStream, UdpSocket, TcpListener, Ipv4Addr}; fn main() { let ip = Ipv4Addr::new(127, 0, 0, 1); let port = 12345; // The following lines are equivalent modulo possible "localhost" name // resolution differences let tcp_s = TcpStream::connect(SocketAddrV4::new(ip, port)); let tcp_s = TcpStream::connect((ip, port)); let tcp_s = TcpStream::connect(("127.0.0.1", port)); let tcp_s = TcpStream::connect(("localhost", port)); let tcp_s = TcpStream::connect("127.0.0.1:12345"); let tcp_s = TcpStream::connect("localhost:12345"); // TcpListener::bind(), UdpSocket::bind() and UdpSocket::send_to() // behave similarly let tcp_l = TcpListener::bind("localhost:12345"); let mut udp_s = UdpSocket::bind(("127.0.0.1", port)).unwrap(); udp_s.send_to(&[7], (ip, 23451)).unwrap(); }
Associated Types
type Iter: Iterator<Item=SocketAddr>
Returned iterator over socket addresses which this type may correspond to.
Required Methods
fn to_socket_addrs(&self) -> Result<Self::Iter>
Converts this object to an iterator of resolved SocketAddr
`SocketAddr`s.
The returned iterator may not actually yield any values depending on the outcome of any resolution performed.
Note that this function may block the current thread while resolution is performed.
Errors
Any errors encountered during resolution will be returned as an Err
`Err`.
Implementors
impl ToSocketAddrs for SocketAddr
impl ToSocketAddrs for SocketAddrV4
impl ToSocketAddrs for SocketAddrV6
impl ToSocketAddrs for (IpAddr, u16)
impl ToSocketAddrs for (Ipv4Addr, u16)
impl ToSocketAddrs for (Ipv6Addr, u16)
impl<'a> ToSocketAddrs for (&'a str, u16)
impl ToSocketAddrs for str
impl<'a, T: ToSocketAddrs + ?Sized> ToSocketAddrs for &'a T