# conn.rs - source
Source of the Rust file `crates/tlsn/src/prover/conn.rs`.

Skip to main content

#
tlsn/prover/
conn.rs

    1use futures::{AsyncRead, AsyncWrite};
    2use futures_plex::DuplexStream;
    3use std::{
    4    pin::Pin,
    5    task::{Context, Poll},
    6};
    7
    8/// A TLS connection to a server.
    9///
    10/// This type implements [`AsyncRead`] and [`AsyncWrite`] and can be used to
    11/// communicate with a server using TLS.
    12///
    13/// # Note
    14///
    15/// This connection is closed on a best-effort basis if this is dropped. To
    16/// ensure a clean close, you should call
    17/// [`AsyncWriteExt::close`](futures::io::AsyncWriteExt::close) to close the
    18/// connection.
    19pub struct TlsConnection {
    20    duplex: DuplexStream,
    21}
    22
    23impl TlsConnection {
    24    pub(crate) fn new(duplex: DuplexStream) -> Self {
    25        Self { duplex }
    26    }
    27}
    28
    29impl AsyncRead for TlsConnection {
    30    fn poll_read(
    31        mut self: Pin<&mut Self>,
    32        cx: &mut Context<'_>,
    33        buf: &mut [u8],
    34    ) -> Poll<std::io::Result<usize>> {
    35        let duplex = Pin::new(&mut self.duplex);
    36        duplex.poll_read(cx, buf)
    37    }
    38}
    39
    40impl AsyncWrite for TlsConnection {
    41    fn poll_write(
    42        mut self: Pin<&mut Self>,
    43        cx: &mut Context<'_>,
    44        buf: &[u8],
    45    ) -> Poll<std::io::Result<usize>> {
    46        Pin::new(&mut self.duplex).poll_write(cx, buf)
    47    }
    48
    49    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
    50        Poll::Ready(Ok(()))
    51    }
    52
    53    fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
    54        Pin::new(&mut self.duplex).poll_close(cx)
    55    }
    56}