no_std_net

Trait ToSocketAddrs

Source
pub trait ToSocketAddrs {
    type Iter: Iterator<Item = SocketAddr>;

    // Required method
    fn to_socket_addrs(&self) -> Result<Self::Iter, ToSocketAddrError>;
}
Expand description

A trait for objects which can be converted or resolved to one or more SocketAddr values.

This trait is used for generic address resolution when constructing network objects. By default it is implemented for the following types:

This trait allows constructing network objects like TcpStream or UdpSocket 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 sometimes SocketAddr 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 in constructor methods is pointless.

Addresses returned by the operating system that are not IP addresses are silently ignored.

§Examples

Creating a SocketAddr iterator that yields one item:

use no_std_net::{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());

Creating a SocketAddr iterator that yields multiple items:

use no_std_net::{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());

Required Associated Types§

Source

type Iter: Iterator<Item = SocketAddr>

Returned iterator over socket addresses which this type may correspond to.

Required Methods§

Source

fn to_socket_addrs(&self) -> Result<Self::Iter, ToSocketAddrError>

Converts this object to an iterator of resolved SocketAddrs.

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.

Implementations on Foreign Types§

Source§

impl ToSocketAddrs for (IpAddr, u16)

Source§

impl ToSocketAddrs for (Ipv4Addr, u16)

Source§

impl ToSocketAddrs for (Ipv6Addr, u16)

Source§

impl<'a> ToSocketAddrs for &'a [SocketAddr]

Source§

type Iter = Cloned<Iter<'a, SocketAddr>>

Source§

fn to_socket_addrs(&self) -> Result<Self::Iter, ToSocketAddrError>

Source§

impl<'a, T: ToSocketAddrs + ?Sized> ToSocketAddrs for &'a T

Source§

type Iter = <T as ToSocketAddrs>::Iter

Source§

fn to_socket_addrs(&self) -> Result<T::Iter, ToSocketAddrError>

Implementors§