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
use crate::aio::{ActualConnection, Connect};
use crate::types::RedisResult;
use async_std::net::TcpStream;
#[cfg(unix)]
use async_std::os::unix::net::UnixStream;
use async_trait::async_trait;
use std::net::SocketAddr;
#[cfg(unix)]
use std::path::Path;
use std::pin::Pin;
use tokio::io::{AsyncRead, AsyncWrite};

/// Wraps the async_std TcpStream in order to implemented the required Traits for it
pub struct TcpStreamAsyncStdWrapped(TcpStream);
#[cfg(unix)]
/// Wraps the async_std UnixStream in order to implemented the required Traits for it
pub struct UnixStreamAsyncStdWrapped(UnixStream);

impl AsyncWrite for TcpStreamAsyncStdWrapped {
    fn poll_write(
        mut self: Pin<&mut Self>,
        cx: &mut core::task::Context,
        buf: &[u8],
    ) -> std::task::Poll<Result<usize, tokio::io::Error>> {
        async_std::io::Write::poll_write(Pin::new(&mut self.0), cx, buf)
    }

    fn poll_flush(
        mut self: Pin<&mut Self>,
        cx: &mut core::task::Context,
    ) -> std::task::Poll<Result<(), tokio::io::Error>> {
        async_std::io::Write::poll_flush(Pin::new(&mut self.0), cx)
    }

    fn poll_shutdown(
        mut self: Pin<&mut Self>,
        cx: &mut core::task::Context,
    ) -> std::task::Poll<Result<(), tokio::io::Error>> {
        async_std::io::Write::poll_close(Pin::new(&mut self.0), cx)
    }
}

impl AsyncRead for TcpStreamAsyncStdWrapped {
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut core::task::Context,
        buf: &mut [u8],
    ) -> std::task::Poll<Result<usize, tokio::io::Error>> {
        async_std::io::Read::poll_read(Pin::new(&mut self.0), cx, buf)
    }
}

#[cfg(unix)]
impl AsyncWrite for UnixStreamAsyncStdWrapped {
    fn poll_write(
        mut self: Pin<&mut Self>,
        cx: &mut core::task::Context,
        buf: &[u8],
    ) -> std::task::Poll<Result<usize, tokio::io::Error>> {
        async_std::io::Write::poll_write(Pin::new(&mut self.0), cx, buf)
    }

    fn poll_flush(
        mut self: Pin<&mut Self>,
        cx: &mut core::task::Context,
    ) -> std::task::Poll<Result<(), tokio::io::Error>> {
        async_std::io::Write::poll_flush(Pin::new(&mut self.0), cx)
    }

    fn poll_shutdown(
        mut self: Pin<&mut Self>,
        cx: &mut core::task::Context,
    ) -> std::task::Poll<Result<(), tokio::io::Error>> {
        async_std::io::Write::poll_close(Pin::new(&mut self.0), cx)
    }
}

#[cfg(unix)]
impl AsyncRead for UnixStreamAsyncStdWrapped {
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut core::task::Context,
        buf: &mut [u8],
    ) -> std::task::Poll<Result<usize, tokio::io::Error>> {
        async_std::io::Read::poll_read(Pin::new(&mut self.0), cx, buf)
    }
}

/// Represents an AsyncStd connectable
pub struct AsyncStd;

#[async_trait]
impl Connect for AsyncStd {
    async fn connect_tcp(socket_addr: SocketAddr) -> RedisResult<ActualConnection> {
        Ok(TcpStream::connect(&socket_addr)
            .await
            .map(|con| ActualConnection::TcpAsyncStd(TcpStreamAsyncStdWrapped(con)))?)
    }

    #[cfg(unix)]
    async fn connect_unix(path: &Path) -> RedisResult<ActualConnection> {
        Ok(UnixStream::connect(path)
            .await
            .map(|con| ActualConnection::UnixAsyncStd(UnixStreamAsyncStdWrapped(con)))?)
    }
}