jsonrpsee_client_transport/ws/
stream.rs1use std::io::Error as IoError;
30use std::pin::Pin;
31use std::task::Context;
32use std::task::Poll;
33
34use pin_project::pin_project;
35use tokio::io::{AsyncRead, AsyncWrite};
36use tokio::net::TcpStream;
37
38#[pin_project(project = EitherStreamProj)]
40#[derive(Debug)]
41#[allow(clippy::large_enum_variant)]
42pub enum EitherStream {
43 Plain(#[pin] TcpStream),
45 #[cfg(feature = "tls")]
47 Tls(#[pin] tokio_rustls::client::TlsStream<TcpStream>),
48}
49
50impl AsyncRead for EitherStream {
51 fn poll_read(
52 self: Pin<&mut Self>,
53 cx: &mut Context,
54 buf: &mut tokio::io::ReadBuf<'_>,
55 ) -> Poll<Result<(), IoError>> {
56 match self.project() {
57 EitherStreamProj::Plain(stream) => AsyncRead::poll_read(stream, cx, buf),
58 #[cfg(feature = "tls")]
59 EitherStreamProj::Tls(stream) => AsyncRead::poll_read(stream, cx, buf),
60 }
61 }
62}
63
64impl AsyncWrite for EitherStream {
65 fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<Result<usize, IoError>> {
66 match self.project() {
67 EitherStreamProj::Plain(stream) => AsyncWrite::poll_write(stream, cx, buf),
68 #[cfg(feature = "tls")]
69 EitherStreamProj::Tls(stream) => AsyncWrite::poll_write(stream, cx, buf),
70 }
71 }
72
73 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), IoError>> {
74 match self.project() {
75 EitherStreamProj::Plain(stream) => AsyncWrite::poll_flush(stream, cx),
76 #[cfg(feature = "tls")]
77 EitherStreamProj::Tls(stream) => AsyncWrite::poll_flush(stream, cx),
78 }
79 }
80
81 fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), IoError>> {
82 match self.project() {
83 EitherStreamProj::Plain(stream) => AsyncWrite::poll_shutdown(stream, cx),
84 #[cfg(feature = "tls")]
85 EitherStreamProj::Tls(stream) => AsyncWrite::poll_shutdown(stream, cx),
86 }
87 }
88}