Is it possible to use `Iterator::map` in a ToSocketAddrs impl? (associated types involving closures)

It seems like it ought to be possible to use Iterator::map (and other iterator combinators) to avoid a lot of extra typing when defining a ToSocketAddrs impl, something like this:

use std::io;
use std::iter;
use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
use std::range::{Range, RangeIter};

pub struct PortRange {
    addr: IpAddr,
    range: Range<u16>,
}
impl PortRange {
    pub fn new(addr: IpAddr, range: Range<u16>) -> PortRange {
        PortRange { addr, range }
    }
}
impl ToSocketAddrs for PortRange {
    type Iter = iter::Map<RangeIter<u16>, _>; // <-- problem here
    fn to_socket_addrs(&self) -> io::Result<Self::Iter> {
        Ok(self
            .range
            .iter()
            .map(|port| SocketAddr::new(self.addr, port)))
    }
}

(Playground)

The problem is that the second type argument to iter::Map needs to be the type of the closure, or something close to it, and I haven't been able to find a way to name that type. If you compile the above as is you get

error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated types

I tried changing the placeholder to FnMut(u16)->SocketAddr and impl FnMut(u16)->SocketAddr but those just gave me different errors.

You can do it on unstable with ITIAT (or just TAIT[1]), though I'd probably just use impl Iterator<Item = u16> for the entire associated type.

With stable I believe you need to use Box<dyn FnMut(u16) -> SocketAddr>, or fn(u16) -> SocketAddr if possible (you're capturing state so it is not possible), or go through the rigamarole of having your own Iterator type that manually does the mapping in the implementation instead of using a closure. (I.e. on stable AFAIK there is no way to name a specific closure.)


  1. type = impl Trait ↩︎

For anyone who finds this post before (I)TAIT is stabilized, the "rigmarole of having your own Iterator type" turns out not to be that bad in this case:

use std::io;
use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
use std::range::{Range, RangeIter};

struct PortRangeIter {
    addr: IpAddr,
    ports: RangeIter<u16>,
}
impl Iterator for PortRangeIter {
    type Item = SocketAddr;
    fn next(&mut self) -> Option<Self::Item> {
        self.ports.next().map(|p| SocketAddr::new(self.addr, p))
    }
}

struct PortRange {
    addr: IpAddr,
    range: Range<u16>,
}
impl ToSocketAddrs for PortRange {
    type Iter = PortRangeIter;
    fn to_socket_addrs(&self) -> io::Result<Self::Iter> {
        Ok(PortRangeIter {
            addr: self.addr,
            ports: self.range.iter(),
        })
    }
}

I initially tried to make the PortRangeIter wrap a reference to the PortRange, but that turns out to be pointless extra complexity.

It's not (soundly) compatible with the ToSocketAddrs trait anyway (type Iter would need a lifetime parameter).

... so that's why I got a screenful of lifetime-related error messages that I decided not to try to decode :slight_smile: