std:🥅:ToSocketAddrs - Rust (original) (raw)
Trait std::net::ToSocketAddrs1.0.0 [−] [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 moreSocketAddr values.
This trait is used for generic address resolution when constructing network objects. By default it is implemented for the following types:
- SocketAddr: to_socket_addrs is the identity function.
- SocketAddrV4, SocketAddrV6,
(
IpAddr,
u16)
,(
Ipv4Addr,
u16)
,(
Ipv6Addr,
u16)
:to_socket_addrs constructs a SocketAddr trivially. (
&str,
u16)
: the string should be either a string representation of an IpAddr address as expected by FromStr implementation or a host name.- &str: the string should be either a string representation of aSocketAddr as expected by its FromStr implementation or a string like
<host_name>:<port>
pair where<port>
is a u16 value.
This trait allows constructing network objects like TcpStream orUdpSocket 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"
is much nicer than manual construction of the corresponding SocketAddr, but sometimesSocketAddr 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 toSocketAddr in constructor methods is pointless.
Addresses returned by the operating system that are not IP addresses are silently ignored.
Creating a SocketAddr iterator that yields one item:
use std:🥅:{ToSocketAddrs, SocketAddr};
let addr = SocketAddr::from(([127, 0, 0, 1], 443)); let mut addrs_iter = addr.to_socket_addrs().unwrap();
assert_eq!(Some(addr), addrs_iter.next()); assert!(addrs_iter.next().is_none());Run
Creating a SocketAddr iterator from a hostname:
use std:🥅:{SocketAddr, ToSocketAddrs};
let mut addrs_iter = "localhost:443".to_socket_addrs().unwrap(); assert_eq!(addrs_iter.next(), Some(SocketAddr::from(([127, 0, 0, 1], 443)))); assert!(addrs_iter.next().is_none());
assert!("foo:443".to_socket_addrs().is_err());Run
Creating a SocketAddr iterator that yields multiple items:
use std:🥅:{SocketAddr, ToSocketAddrs};
let addr1 = SocketAddr::from(([0, 0, 0, 0], 80)); let addr2 = SocketAddr::from(([127, 0, 0, 1], 443)); let addrs = vec![addr1, addr2];
let mut addrs_iter = (&addrs[..]).to_socket_addrs().unwrap();
assert_eq!(Some(addr1), addrs_iter.next()); assert_eq!(Some(addr2), addrs_iter.next()); assert!(addrs_iter.next().is_none());Run
Attempting to create a SocketAddr iterator from an improperly formatted socket address &str
(missing the port):
use std::io; use std:🥅:ToSocketAddrs;
let err = "127.0.0.1".to_socket_addrs().unwrap_err(); assert_eq!(err.kind(), io::ErrorKind::InvalidInput);Run
TcpStream::connect is an example of an function that utilizesToSocketAddrs
as a trait bound on its parameter in order to accept different types:
use std:🥅:{TcpStream, Ipv4Addr};
let stream = TcpStream::connect(("127.0.0.1", 443));
let stream = TcpStream::connect("127.0.0.1:443");
let stream = TcpStream::connect((Ipv4Addr::new(127, 0, 0, 1), 443));Run
type [Iter](#associatedtype.Iter): [Iterator](../../std/iter/trait.Iterator.html "trait std::iter::Iterator")<Item = [SocketAddr](../../std/net/enum.SocketAddr.html "enum std:🥅:SocketAddr")>
Returned iterator over socket addresses which this type may correspond to.
fn [to_socket_addrs](#tymethod.to%5Fsocket%5Faddrs)(&self) -> [Result](../../std/io/type.Result.html "type std::io::Result")<Self::[Iter](../../std/net/trait.ToSocketAddrs.html#associatedtype.Iter "type std:🥅:ToSocketAddrs::Iter")>
Converts this object to an iterator of resolved 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.
impl ToSocketAddrs for [SocketAddr](../../std/net/enum.SocketAddr.html "enum std:🥅:SocketAddr") type [Iter](#associatedtype.Iter) = [IntoIter](../../std/option/struct.IntoIter.html "struct std::option::IntoIter")<[SocketAddr](../../std/net/enum.SocketAddr.html "enum std:🥅:SocketAddr")>;
impl ToSocketAddrs for [SocketAddrV4](../../std/net/struct.SocketAddrV4.html "struct std:🥅:SocketAddrV4") type [Iter](#associatedtype.Iter) = [IntoIter](../../std/option/struct.IntoIter.html "struct std::option::IntoIter")<[SocketAddr](../../std/net/enum.SocketAddr.html "enum std:🥅:SocketAddr")>;
impl ToSocketAddrs for [SocketAddrV6](../../std/net/struct.SocketAddrV6.html "struct std:🥅:SocketAddrV6") type [Iter](#associatedtype.Iter) = [IntoIter](../../std/option/struct.IntoIter.html "struct std::option::IntoIter")<[SocketAddr](../../std/net/enum.SocketAddr.html "enum std:🥅:SocketAddr")>;
impl ToSocketAddrs for [(](../primitive.tuple.html)[IpAddr](../../std/net/enum.IpAddr.html "enum std:🥅:IpAddr"), [u16](../primitive.u16.html)[)](../primitive.tuple.html) type [Iter](#associatedtype.Iter) = [IntoIter](../../std/option/struct.IntoIter.html "struct std::option::IntoIter")<[SocketAddr](../../std/net/enum.SocketAddr.html "enum std:🥅:SocketAddr")>;
impl ToSocketAddrs for [(](../primitive.tuple.html)[Ipv4Addr](../../std/net/struct.Ipv4Addr.html "struct std:🥅:Ipv4Addr"), [u16](../primitive.u16.html)[)](../primitive.tuple.html) type [Iter](#associatedtype.Iter) = [IntoIter](../../std/option/struct.IntoIter.html "struct std::option::IntoIter")<[SocketAddr](../../std/net/enum.SocketAddr.html "enum std:🥅:SocketAddr")>;
impl ToSocketAddrs for [(](../primitive.tuple.html)[Ipv6Addr](../../std/net/struct.Ipv6Addr.html "struct std:🥅:Ipv6Addr"), [u16](../primitive.u16.html)[)](../primitive.tuple.html) type [Iter](#associatedtype.Iter) = [IntoIter](../../std/option/struct.IntoIter.html "struct std::option::IntoIter")<[SocketAddr](../../std/net/enum.SocketAddr.html "enum std:🥅:SocketAddr")>;
impl<'a> ToSocketAddrs for [(](../primitive.tuple.html)&'a [str](../primitive.str.html), [u16](../primitive.u16.html)[)](../primitive.tuple.html) type [Iter](#associatedtype.Iter) = [IntoIter](../../std/vec/struct.IntoIter.html "struct std::vec::IntoIter")<[SocketAddr](../../std/net/enum.SocketAddr.html "enum std:🥅:SocketAddr")>;
impl ToSocketAddrs for [str](../primitive.str.html) type [Iter](#associatedtype.Iter) = [IntoIter](../../std/vec/struct.IntoIter.html "struct std::vec::IntoIter")<[SocketAddr](../../std/net/enum.SocketAddr.html "enum std:🥅:SocketAddr")>;
impl<'a> ToSocketAddrs for [&'a [](../primitive.slice.html)[SocketAddr](../../std/net/enum.SocketAddr.html "enum std:🥅:SocketAddr")[]](../primitive.slice.html) type [Iter](#associatedtype.Iter) = [Cloned](../../std/iter/struct.Cloned.html "struct std::iter::Cloned")<[Iter](../../std/slice/struct.Iter.html "struct std::slice::Iter")<'a, [SocketAddr](../../std/net/enum.SocketAddr.html "enum std:🥅:SocketAddr")>>;
impl<'a, T: [ToSocketAddrs](../../std/net/trait.ToSocketAddrs.html "trait std:🥅:ToSocketAddrs") + ?[Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized")> ToSocketAddrs for [&'a ](../primitive.reference.html)T type [Iter](#associatedtype.Iter) = T::[Iter](../../std/net/trait.ToSocketAddrs.html#associatedtype.Iter "type std:🥅:ToSocketAddrs::Iter");
impl ToSocketAddrs for [String](../../std/string/struct.String.html "struct std:🧵:String") type [Iter](#associatedtype.Iter) = [IntoIter](../../std/vec/struct.IntoIter.html "struct std::vec::IntoIter")<[SocketAddr](../../std/net/enum.SocketAddr.html "enum std:🥅:SocketAddr")>;