[stdlib feature request] `TcpListener::incoming` add owning variant

There is TcpListener::incoming that turns a TcpListener into an Iterator of incoming TcpStreams, but it has a lifetime. I cannot simply pass the iterator around and manipulate it, because it has 'a lifetime bound to the TcpListener. An alternative that takes over ownership would be appreciated.

1 Like
struct OwnedIncoming(TcpListener);

impl Iterator for OwnedIncoming {
    type Item = io::Result<TcpStream>;
    fn next(&mut self) -> Option<io::Result<TcpStream>> {
        Some(self.listener.accept().map(|p| p.0))

How exactly are you "manipulating" the iterator?

Probably something like map, chain, filter, for_each

Yes. This is exactly what I'd like to have in stdlib. Can I simply PR it or what is the common process for such a minor change?

You can open a PR: Introduction - Guide to Rustc Development

I opened Add TcpListener::into_incoming and IntoIncoming by piegamesde · Pull Request #88339 · rust-lang/rust · GitHub</, although it complains about missing feature gate and stuff.

Sorry, I linked the wrong section of the book:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.