1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
/************************************************************************************************** * * * This Source Code Form is subject to the terms of the Mozilla Public * * License, v. 2.0. If a copy of the MPL was not distributed with this * * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * * **************************************************************************************************/ // ======================================== Documentation ======================================= \\ //! This crate provides [`TcpListener`] and [`TcpStream`], two traits that can be implemented by //! any async versions of [`std::net::TcpListener`] and [`std::net::TcpStream`]. // =========================================== Imports ========================================== \\ #[cfg(feature = "async-io")] mod async_io; #[cfg(feature = "async-net")] mod async_net; use async_peek::AsyncPeek; use core::future::Future; use core::pin::Pin; use core::task::{Context, Poll}; use futures_lite::{ready, AsyncRead, AsyncWrite, Stream}; use std::io::Result; use std::net::{Shutdown, SocketAddr}; // ========================================= Interfaces ========================================= \\ /// A TCP socket server, listening for connections. /// /// After creating a `TcpListener` by [`bind`ing] it to a socket address, it listens for incoming /// TCP connections. Thse can be accepted by calling [`accept()`] or by awaiting items from the /// stream of [`incoming`] connections. /// /// The socket will be closed when all handles to it are dropped. /// /// The Transmission Control Protocol is specified in [IETF RFC 793]. /// /// [`bind`ing]: TcpListener::bind() /// [`accept()`]: TcpListener::accept() /// [`incoming`]: TcpListener::incoming() /// [IETF RFC 793]: https://tools.ietf.org/html/rfc793 pub trait TcpListener: Sized { /// The type of a TCP connection created by [`accept`ing] an incoming connection. /// /// [`accept`ing]: TcpListener::accept() type Stream: TcpStream; /// Creates a new `TcpListener` bound to the given address. /// /// Binding with a port number of `0` will request that the operating system assigns an /// available port to this listener. The assigned port can be queried via [`local_addr()`]. /// /// ## Examples /// /// ```rust /// # use async_net_ as async_net; /// use async_tcp::TcpListener; /// use std::net::SocketAddr; /// # use std::io::Result; /// use std::str::FromStr; /// /// # async fn tcp_bind() -> Result<impl TcpListener> { /// # /// let addr = SocketAddr::from_str("127.0.0.1:0").unwrap(); /// let listener = async_net::TcpListener::bind(addr).await?; /// # /// # Ok(listener) } /// # /// # smol::block_on(tcp_bind()).unwrap(); /// ``` /// /// [`local_addr()`]: TcpListener::local_addr() fn bind(addr: SocketAddr) -> Bind<Self>; /// Returns the local socket address of this listener. /// /// ## Examples /// /// ```rust /// # smol::block_on(async { /// # /// # use async_net_ as async_net; /// use async_tcp::TcpListener; /// use std::net::SocketAddr; /// # use std::io::Result; /// use std::str::FromStr; /// /// let addr = SocketAddr::from_str("127.0.0.1:1105").unwrap(); /// let listener = async_net::TcpListener::bind(addr).await?; /// /// # fn tcp_local_addr<Listener: TcpListener>(addr: SocketAddr, listener: Listener) -> Result<()> { /// # /// assert_eq!(addr, listener.local_addr()?); /// # /// # Ok(()) } /// # /// # tcp_local_addr(addr, listener)?; /// # /// # Result::<()>::Ok(()) }).unwrap(); /// ``` fn local_addr(&self) -> Result<SocketAddr>; /// Accepts a new incoming connection. /// /// Returns a [`TcpStream`] and the address it is connected to. /// /// ## Examples /// /// ```rust /// # smol::block_on(async { /// # /// # use async_net_ as async_net; /// use async_tcp::{TcpListener, TcpStream}; /// use std::net::SocketAddr; /// # use std::io::Result; /// use std::str::FromStr; /// /// let addr = SocketAddr::from_str("127.0.0.1:0").unwrap(); /// let listener = async_net::TcpListener::bind(addr).await?; /// /// # let addr = listener.local_addr()?; /// # let conn = smol::spawn(async_net::TcpStream::connect(addr)); /// # /// # async fn tcp_accept<Listener: TcpListener>( /// # listener: &Listener, /// # ) -> Result<impl TcpStream> { /// # /// let (stream, addr) = listener.accept().await?; /// assert_eq!(stream.peer_addr()?, addr); /// # /// # Ok(stream) } /// # /// # let stream = tcp_accept(&listener).await?; /// # let conn = conn.await?; /// # assert_eq!(conn.local_addr()?, stream.peer_addr()?); /// # assert_eq!(conn.peer_addr()?, stream.local_addr()?); /// # /// # Result::<()>::Ok(()) }).unwrap(); /// ``` fn accept(&self) -> Accept<Self> { Accept { listener: self } } /// Attempts to accept a new incoming connection from this listener. /// /// On success, returns a [`TcpStream`] and the address it is connected to. /// /// If no new incoming connection is ready to be accepted, the current task is registered to be /// notified when one becomes ready to be accepted or the socket closed, and `Poll::Pending` is /// returned. /// /// This method exists to be used by [`accept()`] and [`incoming()`], we recommend you use of /// these methods instead. /// /// [`accept()`]: TcpListener::accept() /// [`incoming()`]: TcpListener::incoming() fn poll_accept(&self, ctx: &mut Context) -> Poll<Result<(Self::Stream, SocketAddr)>>; /// Returns a stream of incoming connections. /// /// Iterating over this stream is equivalent to calling [`accept()`] in a loop. The stream of /// connections is infinite, ie. it will never return `None`. /// /// ## Examples /// /// ```rust /// # smol::block_on(async { /// # /// # use async_net_ as async_net; /// use async_tcp::{TcpListener, TcpStream}; /// use futures_lite::StreamExt; /// use std::net::SocketAddr; /// # use std::io::Result; /// use std::str::FromStr; /// /// let addr = SocketAddr::from_str("127.0.0.1:0").unwrap(); /// let listener = async_net::TcpListener::bind(addr).await?; /// # /// # let addr = listener.local_addr()?; /// # let conn = smol::spawn(async_net::TcpStream::connect(addr)); /// # /// # async fn tcp_incoming<Listener: TcpListener>( /// # listener: &Listener, /// # ) -> Result<impl TcpStream> { /// # /// let mut incoming = listener.incoming(); /// /// while let Some(stream) = incoming.next().await { /// let mut stream = stream?; /// assert_eq!(stream.local_addr()?, listener.local_addr()?); /// # return Ok(stream); /// } /// # /// # unreachable!(); } /// # /// # let stream = tcp_incoming(&listener).await?; /// # let conn = conn.await?; /// # assert_eq!(conn.local_addr()?, stream.peer_addr()?); /// # assert_eq!(conn.peer_addr()?, stream.local_addr()?); /// # /// # Result::<()>::Ok(()) }).unwrap(); /// ``` /// /// [`accept()`]: TcpListener::accept() fn incoming(&self) -> Incoming<Self> { Incoming { listener: self } } /// Gets the value of the `IP_TTL` option on this socket. /// /// For more information about this option, see [`set_ttl()`]. /// /// ## Examples /// /// ```rust /// # smol::block_on(async { /// # /// # use async_net_ as async_net; /// use async_tcp::TcpListener; /// use std::net::SocketAddr; /// # use std::io::Result; /// use std::str::FromStr; /// /// let addr = SocketAddr::from_str("127.0.0.1:0").unwrap(); /// let listener = async_net::TcpListener::bind(addr).await.unwrap(); /// /// # fn tcp_ttl<Listener: TcpListener>(listener: Listener) -> Result<()> { /// # /// listener.set_ttl(100)?; /// assert_eq!(listener.ttl()?, 100); /// # /// # Ok(()) } /// # /// # tcp_ttl(listener)?; /// # /// # Result::<()>::Ok(()) }).unwrap(); /// ``` /// /// [`set_ttl()`]: TcpListener::set_ttl() fn ttl(&self) -> Result<u32>; /// Sets the value for the `IP_TTL` option on this socket. /// /// This value sets the time-to-live field that is used in every packet sent from this socket. /// /// ## Example /// /// ```rust /// # smol::block_on(async { /// # /// # use async_net_ as async_net; /// use async_tcp::TcpListener; /// use std::net::SocketAddr; /// # use std::io::Result; /// use std::str::FromStr; /// /// let addr = SocketAddr::from_str("127.0.0.1:0").unwrap(); /// let listener = async_net::TcpListener::bind(addr).await.unwrap(); /// /// # fn tcp_set_ttl<Listener: TcpListener>(listener: Listener) -> Result<()> { /// # /// listener.set_ttl(100)?; /// assert_eq!(listener.ttl()?, 100); /// # /// # Ok(()) } /// # /// # tcp_set_ttl(listener)?; /// # /// # Result::<()>::Ok(()) }).unwrap(); /// ``` fn set_ttl(&self, ttl: u32) -> Result<()>; } /// A TCP stream between a local and a remote socket. /// /// A `TcpStream` can be created by either [`connect`ing] to an endpoint or [`accept`ing] a /// connection on a [`TcpListener`]. /// /// [`TcpStream`] is a bidirectional stream that implements [`AsyncPeek`], [`AsyncRead`] and /// [`AsyncWrite`]. /// /// The socket will be closed when all handles to it are dropped. The reading and writing portions /// of the connection can also be shut down individually with the [`shutdown()`] method. /// /// [`connect`ing]: TcpStream::connect() /// [`accept`ing]: TcpListener::accept() /// [`shutdown()`]: TcpStream::shutdown() pub trait TcpStream: AsyncPeek + AsyncRead + AsyncWrite + Sized { /// Opens a TCP connection to the specified address. /// /// ## Examples /// /// ```rust /// # use async_net_ as async_net; /// # use async_tcp::TcpListener; /// use async_tcp::TcpStream; /// use std::net::SocketAddr; /// # use std::io::Result; /// use std::str::FromStr; /// /// # async fn tcp_connect() -> Result<impl TcpStream> { /// # /// # let addr = SocketAddr::from_str("127.0.0.1:0").unwrap(); /// # let listener = async_net::TcpListener::bind(addr).await?; /// # let addr = listener.local_addr()?; /// // let addr = ...; /// # /// # let connect = smol::spawn(async move { /// let stream = async_net::TcpStream::connect(addr).await?; /// # Result::Ok(stream) }); /// # /// # let (conn, peer_addr) = listener.accept().await?; /// # let stream = connect.await?; /// # assert_eq!(peer_addr, conn.peer_addr()?); /// # assert_eq!(peer_addr, stream.local_addr()?); /// assert_eq!(addr, stream.peer_addr()?); /// # /// # Ok(stream) } /// # /// # smol::block_on(tcp_connect()).unwrap(); /// ``` fn connect(addr: SocketAddr) -> Connect<Self>; /// Returns the socket address of the local half of this TCP connection. /// /// ## Examples /// /// ```rust /// # smol::block_on(async { /// # /// # use async_net_ as async_net; /// # use async_tcp::TcpListener; /// use async_tcp::TcpStream; /// use std::net::{IpAddr, SocketAddr}; /// # use std::io::Result; /// use std::str::FromStr; /// /// # let addr = SocketAddr::from_str("127.0.0.1:0").unwrap(); /// # let listener = async_net::TcpListener::bind(addr).await?; /// # let addr = listener.local_addr()?; /// // let addr = ...; /// # /// # let connect = smol::spawn(async move { /// let stream = async_net::TcpStream::connect(addr).await?; /// # Result::Ok(stream) }); /// # /// # let (conn, peer_addr) = listener.accept().await?; /// # let stream = connect.await?; /// /// # fn tcp_local_addr<Stream: TcpStream>(stream: Stream) -> Result<()> { /// # /// assert_eq!(stream.local_addr()?.ip(), IpAddr::from_str("127.0.0.1").unwrap()); /// # /// # Ok(()) } /// # /// # tcp_local_addr(stream)?; /// # /// # Result::<()>::Ok(()) }).unwrap(); /// ``` fn local_addr(&self) -> Result<SocketAddr>; /// Returns the socket address of the remote peer of this TCP connection. /// /// ## Examples /// /// ```rust /// # smol::block_on(async { /// # /// # use async_net_ as async_net; /// # use async_tcp::TcpListener; /// use async_tcp::TcpStream; /// use std::net::SocketAddr; /// # use std::io::Result; /// use std::str::FromStr; /// /// # let addr = SocketAddr::from_str("127.0.0.1:0").unwrap(); /// # let listener = async_net::TcpListener::bind(addr).await?; /// # let addr = listener.local_addr()?; /// // let addr = ...; /// # /// # let connect = smol::spawn(async move { /// let stream = async_net::TcpStream::connect(addr).await?; /// # Result::Ok(stream) }); /// # /// # let (conn, peer_addr) = listener.accept().await?; /// # let stream = connect.await?; /// /// # fn tcp_peer_addr<Stream: TcpStream>(addr: SocketAddr, stream: Stream) -> Result<()> { /// # /// assert_eq!(addr, stream.peer_addr()?); /// # /// # Ok(()) } /// # /// # tcp_peer_addr(addr, stream)?; /// # /// # Result::<()>::Ok(()) }).unwrap(); /// ``` fn peer_addr(&self) -> Result<SocketAddr>; /// Shuts down the read half, write half, or both halves of this connection. /// /// This method will cause all pending and future I/O in the given directions to return /// immediately with an appropriate value (see the documentation of [`Shutdown`]). /// /// ## Examples /// /// ```rust /// # smol::block_on(async { /// # /// # use async_net_ as async_net; /// # use async_tcp::TcpListener; /// use async_tcp::TcpStream; /// use std::net::{Shutdown, SocketAddr}; /// # use std::io::Result; /// use std::str::FromStr; /// /// # let addr = SocketAddr::from_str("127.0.0.1:0").unwrap(); /// # let listener = async_net::TcpListener::bind(addr).await?; /// # let addr = listener.local_addr()?; /// // let addr = ...; /// # /// # let connect = smol::spawn(async move { /// let stream = async_net::TcpStream::connect(addr).await?; /// # Result::Ok(stream) }); /// # /// # let (conn, peer_addr) = listener.accept().await?; /// # let stream = connect.await?; /// /// # fn tcp_shutdown<Stream: TcpStream>(stream: Stream) -> Result<()> { /// # /// stream.shutdown(Shutdown::Both)?; /// # /// # Ok(()) } /// # /// # tcp_shutdown(stream)?; /// # /// # Result::<()>::Ok(()) }).unwrap(); /// ``` fn shutdown(&self, how: Shutdown) -> Result<()>; /// Gets the value of the `TCP_NODELAY` option on this socket. /// /// For more information about this option, see [`set_nodelay()`]. /// /// ## Examples /// /// ```rust /// # smol::block_on(async { /// # /// # use async_net_ as async_net; /// # use async_tcp::TcpListener; /// use async_tcp::TcpStream; /// use std::net::SocketAddr; /// # use std::io::Result; /// use std::str::FromStr; /// /// # let addr = SocketAddr::from_str("127.0.0.1:0").unwrap(); /// # let listener = async_net::TcpListener::bind(addr).await?; /// # let addr = listener.local_addr()?; /// // let addr = ...; /// # /// # let connect = smol::spawn(async move { /// let stream = async_net::TcpStream::connect(addr).await?; /// # Result::Ok(stream) }); /// # /// # let (conn, peer_addr) = listener.accept().await?; /// # let stream = connect.await?; /// /// # fn tcp_nodelay<Stream: TcpStream>(stream: Stream) -> Result<()> { /// # /// stream.set_nodelay(true)?; /// assert_eq!(stream.nodelay()?, true); /// # /// # Ok(()) } /// # /// # tcp_nodelay(stream)?; /// # /// # Result::<()>::Ok(()) }).unwrap(); /// ``` /// /// [`set_nodelay()`]: TcpStream::set_nodelay() fn nodelay(&self) -> Result<bool>; /// Sets the value of `TCP_NODELAY` option on this socket. /// /// If set, this option disables the Nagle algorithm. This means that segments are always sent /// as soon as possible, even if there is only a small amount of data. When not set, data is /// buffered until there is a sufficient amount to send out, thereby avoiding the frequent /// reading of small packets. /// /// ## Examples /// /// ```rust /// # smol::block_on(async { /// # /// # use async_net_ as async_net; /// # use async_tcp::TcpListener; /// use async_tcp::TcpStream; /// use std::net::SocketAddr; /// # use std::io::Result; /// use std::str::FromStr; /// /// # let addr = SocketAddr::from_str("127.0.0.1:0").unwrap(); /// # let listener = async_net::TcpListener::bind(addr).await?; /// # let addr = listener.local_addr()?; /// // let addr = ...; /// # /// # let connect = smol::spawn(async move { /// let stream = async_net::TcpStream::connect(addr).await?; /// # Result::Ok(stream) }); /// # /// # let (conn, peer_addr) = listener.accept().await?; /// # let stream = connect.await?; /// /// # fn tcp_set_nodelay<Stream: TcpStream>(stream: Stream) -> Result<()> { /// # /// stream.set_nodelay(true)?; /// assert_eq!(stream.nodelay()?, true); /// # /// # Ok(()) } /// # /// # tcp_set_nodelay(stream)?; /// # /// # Result::<()>::Ok(()) }).unwrap(); /// ``` fn set_nodelay(&self, nodelay: bool) -> Result<()>; /// Gets the value of the `IP_TTL` option on this socket. /// /// For more information about this option, see [`set_ttl()`]. /// /// ## Examples /// /// ```rust /// # smol::block_on(async { /// # /// # use async_net_ as async_net; /// # use async_tcp::TcpListener; /// use async_tcp::TcpStream; /// use std::net::SocketAddr; /// # use std::io::Result; /// use std::str::FromStr; /// /// # let addr = SocketAddr::from_str("127.0.0.1:0").unwrap(); /// # let listener = async_net::TcpListener::bind(addr).await?; /// # let addr = listener.local_addr()?; /// // let addr = ...; /// # /// # let connect = smol::spawn(async move { /// let stream = async_net::TcpStream::connect(addr).await?; /// # Result::Ok(stream) }); /// # /// # let (conn, peer_addr) = listener.accept().await?; /// # let stream = connect.await?; /// /// # fn tcp_ttl<Stream: TcpStream>(stream: Stream) -> Result<()> { /// # /// stream.set_ttl(100)?; /// assert_eq!(stream.ttl()?, 100); /// # /// # Ok(()) } /// # /// # tcp_ttl(stream)?; /// # /// # Result::<()>::Ok(()) }).unwrap(); /// ``` /// /// [`set_ttl()`]: TcpStream::set_ttl() fn ttl(&self) -> Result<u32>; /// Sets the value for the `IP_TTL` option on this socket. /// /// This value sets the time-to-live field that is used in every packet sent from this socket. /// /// ## Examples /// /// ```rust /// # smol::block_on(async { /// # /// # use async_net_ as async_net; /// # use async_tcp::TcpListener; /// use async_tcp::TcpStream; /// use std::net::SocketAddr; /// # use std::io::Result; /// use std::str::FromStr; /// /// # let addr = SocketAddr::from_str("127.0.0.1:0").unwrap(); /// # let listener = async_net::TcpListener::bind(addr).await?; /// # let addr = listener.local_addr()?; /// // let addr = ...; /// # /// # let connect = smol::spawn(async move { /// let stream = async_net::TcpStream::connect(addr).await?; /// # Result::Ok(stream) }); /// # /// # let (conn, peer_addr) = listener.accept().await?; /// # let stream = connect.await?; /// /// # fn tcp_set_ttl<Stream: TcpStream>(stream: Stream) -> Result<()> { /// # /// stream.set_ttl(100)?; /// assert_eq!(stream.ttl()?, 100); /// # /// # Ok(()) } /// # /// # tcp_set_ttl(stream)?; /// # /// # Result::<()>::Ok(()) }).unwrap(); /// ``` fn set_ttl(&self, ttl: u32) -> Result<()>; } // ============================================ Types =========================================== \\ /// Future returned by the [`TcpListener::bind()`] method. /// /// If you are implementing [`TcpListener`] manually, you can construct a new instance of `Bind` /// using its implementation of `From<Pin<Box<dyn Future<Output = Result<Listener>> + Send>>>`. pub struct Bind<Listener> { fut: Pin<Box<dyn Future<Output = Result<Listener>> + Send>>, } /// Future returned by the [`TcpListener::accept()`] method. pub struct Accept<'listener, Listener> { listener: &'listener Listener, } /// Stream returned by the [`TcpListener::incoming()`] method. pub struct Incoming<'listener, Listener> { listener: &'listener Listener, } /// Future returned by the [`TcpStream::connect()`] method. /// /// If you are implementing [`TcpStream`] manually, you can construct a new instance of `Connect` /// using its implementation of `From<Pin<Box<dyn Future<Output = Result<Stream>> + Send>>>`. pub struct Connect<Stream> { fut: Pin<Box<dyn Future<Output = Result<Stream>> + Send>> } // ========================================== impl From ========================================= \\ impl<Listener> From<Pin<Box<dyn Future<Output = Result<Listener>> + Send>>> for Bind<Listener> where Listener: TcpListener, { fn from(fut: Pin<Box<dyn Future<Output = Result<Listener>> + Send>>) -> Self { Bind { fut } } } impl<Stream> From<Pin<Box<dyn Future<Output = Result<Stream>> + Send>>> for Connect<Stream> where Stream: TcpStream, { fn from(fut: Pin<Box<dyn Future<Output = Result<Stream>> + Send>>) -> Self { Connect { fut } } } // ========================================= impl Future ======================================== \\ impl<Listener: TcpListener> Future for Bind<Listener> { type Output = Result<Listener>; fn poll(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> { self.fut.as_mut().poll(ctx) } } impl<Listener: TcpListener> Future for Accept<'_, Listener> { type Output = Result<(Listener::Stream, SocketAddr)>; fn poll(self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> { self.listener.poll_accept(ctx) } } impl<Stream: TcpStream> Future for Connect<Stream> { type Output = Result<Stream>; fn poll(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> { self.fut.as_mut().poll(ctx) } } // ========================================= impl Stream ======================================== \\ impl<Listener: TcpListener> Stream for Incoming<'_, Listener> { type Item = Result<Listener::Stream>; fn poll_next(self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Option<Self::Item>> { let (stream, _) = ready!(self.listener.poll_accept(ctx))?; Poll::Ready(Some(Ok(stream))) } }