Skip to content

Commit d7a656f

Browse files
Rollup merge of rust-lang#88339 - piegamesde:master, r=joshtriplett
Add TcpListener::into_incoming and IntoIncoming The `incoming` method is really useful, however for some use cases the borrow this introduces is needlessly restricting. Thus, an owned variant is added. r? `@joshtriplett`
2 parents dc7a66c + ced597e commit d7a656f

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

library/std/src/net/tcp.rs

+51
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ pub struct Incoming<'a> {
9696
listener: &'a TcpListener,
9797
}
9898

99+
/// An iterator that infinitely [`accept`]s connections on a [`TcpListener`].
100+
///
101+
/// This `struct` is created by the [`TcpListener::into_incoming`] method.
102+
/// See its documentation for more.
103+
///
104+
/// [`accept`]: TcpListener::accept
105+
#[derive(Debug)]
106+
#[unstable(feature = "tcplistener_into_incoming", issue = "88339")]
107+
pub struct IntoIncoming {
108+
listener: TcpListener,
109+
}
110+
99111
impl TcpStream {
100112
/// Opens a TCP connection to a remote host.
101113
///
@@ -845,6 +857,37 @@ impl TcpListener {
845857
Incoming { listener: self }
846858
}
847859

860+
/// Turn this into an iterator over the connections being received on this
861+
/// listener.
862+
///
863+
/// The returned iterator will never return [`None`] and will also not yield
864+
/// the peer's [`SocketAddr`] structure. Iterating over it is equivalent to
865+
/// calling [`TcpListener::accept`] in a loop.
866+
///
867+
/// # Examples
868+
///
869+
/// ```no_run
870+
/// #![feature(tcplistener_into_incoming)]
871+
/// use std::net::{TcpListener, TcpStream};
872+
///
873+
/// fn listen_on(port: u16) -> impl Iterator<Item = TcpStream> {
874+
/// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
875+
/// listener.into_incoming()
876+
/// .filter_map(Result::ok) /* Ignore failed connections */
877+
/// }
878+
///
879+
/// fn main() -> std::io::Result<()> {
880+
/// for stream in listen_on(80) {
881+
/// /* handle the connection here */
882+
/// }
883+
/// Ok(())
884+
/// }
885+
/// ```
886+
#[unstable(feature = "tcplistener_into_incoming", issue = "88339")]
887+
pub fn into_incoming(self) -> IntoIncoming {
888+
IntoIncoming { listener: self }
889+
}
890+
848891
/// Sets the value for the `IP_TTL` option on this socket.
849892
///
850893
/// This value sets the time-to-live field that is used in every packet sent
@@ -982,6 +1025,14 @@ impl<'a> Iterator for Incoming<'a> {
9821025
}
9831026
}
9841027

1028+
#[unstable(feature = "tcplistener_into_incoming", issue = "88339")]
1029+
impl Iterator for IntoIncoming {
1030+
type Item = io::Result<TcpStream>;
1031+
fn next(&mut self) -> Option<io::Result<TcpStream>> {
1032+
Some(self.listener.accept().map(|p| p.0))
1033+
}
1034+
}
1035+
9851036
impl AsInner<net_imp::TcpListener> for TcpListener {
9861037
fn as_inner(&self) -> &net_imp::TcpListener {
9871038
&self.0

0 commit comments

Comments
 (0)