Skip to main content

redis_tower_core/
stream.rs

1use std::pin::Pin;
2use std::task::{Context, Poll};
3
4use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
5use tokio::net::TcpStream;
6
7#[cfg(unix)]
8use tokio::net::UnixStream;
9
10/// Transport abstraction over TCP, Unix, and TLS connections.
11///
12/// Implements [`AsyncRead`] + [`AsyncWrite`] so it can be used with
13/// `tokio_util::codec::Framed`. The active variant is determined at
14/// connection time based on the URL scheme or connect method used.
15pub enum RedisStream {
16    /// Plain TCP connection.
17    Tcp(TcpStream),
18
19    /// Unix domain socket connection.
20    #[cfg(unix)]
21    Unix(UnixStream),
22
23    /// TLS connection via native-tls.
24    #[cfg(feature = "tls-native-tls")]
25    NativeTls(Box<tokio_native_tls::TlsStream<TcpStream>>),
26
27    /// TLS connection via rustls.
28    #[cfg(feature = "tls-rustls")]
29    Rustls(Box<tokio_rustls::client::TlsStream<TcpStream>>),
30}
31
32impl AsyncRead for RedisStream {
33    fn poll_read(
34        self: Pin<&mut Self>,
35        cx: &mut Context<'_>,
36        buf: &mut ReadBuf<'_>,
37    ) -> Poll<std::io::Result<()>> {
38        match self.get_mut() {
39            RedisStream::Tcp(s) => Pin::new(s).poll_read(cx, buf),
40            #[cfg(unix)]
41            RedisStream::Unix(s) => Pin::new(s).poll_read(cx, buf),
42            #[cfg(feature = "tls-native-tls")]
43            RedisStream::NativeTls(s) => Pin::new(s).poll_read(cx, buf),
44            #[cfg(feature = "tls-rustls")]
45            RedisStream::Rustls(s) => Pin::new(s).poll_read(cx, buf),
46        }
47    }
48}
49
50impl AsyncWrite for RedisStream {
51    fn poll_write(
52        self: Pin<&mut Self>,
53        cx: &mut Context<'_>,
54        buf: &[u8],
55    ) -> Poll<std::io::Result<usize>> {
56        match self.get_mut() {
57            RedisStream::Tcp(s) => Pin::new(s).poll_write(cx, buf),
58            #[cfg(unix)]
59            RedisStream::Unix(s) => Pin::new(s).poll_write(cx, buf),
60            #[cfg(feature = "tls-native-tls")]
61            RedisStream::NativeTls(s) => Pin::new(s).poll_write(cx, buf),
62            #[cfg(feature = "tls-rustls")]
63            RedisStream::Rustls(s) => Pin::new(s).poll_write(cx, buf),
64        }
65    }
66
67    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
68        match self.get_mut() {
69            RedisStream::Tcp(s) => Pin::new(s).poll_flush(cx),
70            #[cfg(unix)]
71            RedisStream::Unix(s) => Pin::new(s).poll_flush(cx),
72            #[cfg(feature = "tls-native-tls")]
73            RedisStream::NativeTls(s) => Pin::new(s).poll_flush(cx),
74            #[cfg(feature = "tls-rustls")]
75            RedisStream::Rustls(s) => Pin::new(s).poll_flush(cx),
76        }
77    }
78
79    fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
80        match self.get_mut() {
81            RedisStream::Tcp(s) => Pin::new(s).poll_shutdown(cx),
82            #[cfg(unix)]
83            RedisStream::Unix(s) => Pin::new(s).poll_shutdown(cx),
84            #[cfg(feature = "tls-native-tls")]
85            RedisStream::NativeTls(s) => Pin::new(s).poll_shutdown(cx),
86            #[cfg(feature = "tls-rustls")]
87            RedisStream::Rustls(s) => Pin::new(s).poll_shutdown(cx),
88        }
89    }
90}