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
use crate::runtime::AsyncRead;
use crate::runtime::AsyncWrite;
use std::fmt;

/// Type alias for necessary socket async traits.
///
/// Type alias exists to avoid repetition of traits in function signatures.
///
/// This type cannot be implemented directly, and there's no need to.
pub trait AsyncSocket: AsyncRead + AsyncWrite + fmt::Debug + Unpin + Send + 'static {}

/// Auto-implement for all socket types.
impl<A: AsyncRead + AsyncWrite + fmt::Debug + Unpin + Send + 'static> AsyncSocket for A {}

/// Delegate [`AsyncSocket`] implementation to the underlying socket.
///
/// This is meant to be used only by API implementations.
///
/// # See also
/// * [PR in tokio](https://github.com/tokio-rs/tokio/pull/3540)
/// * [PR in futures](https://github.com/rust-lang/futures-rs/pull/2352)
#[cfg(feature = "runtime-tokio")]
#[macro_export]
macro_rules! spi_async_socket_impl_delegate {
    ( "AsyncRead" ) => {
        fn poll_read(
            self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
            buf: &mut $crate::runtime::ReadBuf,
        ) -> std::task::Poll<std::io::Result<()>> {
            self.deref_pin_mut_for_impl_socket().poll_read(cx, buf)
        }
    };
    ( "AsyncWrite" ) => {
        fn poll_write(
            self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
            buf: &[u8],
        ) -> std::task::Poll<std::io::Result<usize>> {
            self.deref_pin_mut_for_impl_socket().poll_write(cx, buf)
        }

        fn poll_write_vectored(
            self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
            bufs: &[std::io::IoSlice<'_>],
        ) -> std::task::Poll<std::result::Result<usize, std::io::Error>> {
            self.deref_pin_mut_for_impl_socket()
                .poll_write_vectored(cx, bufs)
        }

        fn is_write_vectored(&self) -> bool {
            self.deref_for_impl_socket().is_write_vectored()
        }

        fn poll_flush(
            self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<std::io::Result<()>> {
            self.deref_pin_mut_for_impl_socket().poll_flush(cx)
        }

        fn poll_shutdown(
            self: std::pin::Pin<&mut Self>,
            ctx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<std::io::Result<()>> {
            self.deref_pin_mut_for_impl_socket().poll_shutdown(ctx)
        }
    };
    ( $t:ident <S> ) => {
        impl<S: $crate::AsyncSocket> $crate::runtime::AsyncRead for $t<S> {
            spi_async_socket_impl_delegate!("AsyncRead");
        }

        impl<S: $crate::AsyncSocket> $crate::runtime::AsyncWrite for $t<S> {
            spi_async_socket_impl_delegate!("AsyncWrite");
        }
    };
    ( $t:ty ) => {
        impl $crate::runtime::AsyncRead for $t {
            spi_async_socket_impl_delegate!("AsyncRead");
        }

        impl $crate::runtime::AsyncWrite for $t {
            spi_async_socket_impl_delegate!("AsyncWrite");
        }
    };
}

/// Delegate [`AsyncSocket`] implementation to the underlying socket.
///
/// This is meant to be used only by API implementations.
///
/// # See also
/// * [PR in tokio](https://github.com/tokio-rs/tokio/pull/3540)
/// * [PR in futures](https://github.com/rust-lang/futures-rs/pull/2352)
#[cfg(feature = "runtime-async-std")]
#[macro_export]
macro_rules! spi_async_socket_impl_delegate {
    ( "AsyncRead" ) => {
        fn poll_read(
            self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
            buf: &mut [u8],
        ) -> std::task::Poll<std::io::Result<usize>> {
            self.deref_pin_mut_for_impl_socket().poll_read(cx, buf)
        }
    };
    ( "AsyncWrite" ) => {
        fn poll_write(
            self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
            buf: &[u8],
        ) -> std::task::Poll<std::io::Result<usize>> {
            self.deref_pin_mut_for_impl_socket().poll_write(cx, buf)
        }

        fn poll_flush(
            self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<std::io::Result<()>> {
            self.deref_pin_mut_for_impl_socket().poll_flush(cx)
        }

        fn poll_close(
            self: std::pin::Pin<&mut Self>,
            ctx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<std::io::Result<()>> {
            self.deref_pin_mut_for_impl_socket().poll_close(ctx)
        }
    };
    ( $t:ident <S> ) => {
        impl<S: $crate::AsyncSocket> $crate::runtime::AsyncRead for $t<S> {
            spi_async_socket_impl_delegate!("AsyncRead");
        }

        impl<S: $crate::AsyncSocket> $crate::runtime::AsyncWrite for $t<S> {
            spi_async_socket_impl_delegate!("AsyncWrite");
        }
    };
    ( $t:ty ) => {
        impl $crate::runtime::AsyncRead for $t {
            spi_async_socket_impl_delegate!("AsyncRead");
        }

        impl $crate::runtime::AsyncWrite for $t {
            spi_async_socket_impl_delegate!("AsyncWrite");
        }
    };
}