tls_api/
socket.rs

1use crate::runtime::AsyncRead;
2use crate::runtime::AsyncWrite;
3use std::fmt;
4
5/// Type alias for necessary socket async traits.
6///
7/// Type alias exists to avoid repetition of traits in function signatures.
8///
9/// This type cannot be implemented directly, and there's no need to.
10pub trait AsyncSocket: AsyncRead + AsyncWrite + fmt::Debug + Unpin + Send + 'static {}
11
12/// Auto-implement for all socket types.
13impl<A: AsyncRead + AsyncWrite + fmt::Debug + Unpin + Send + 'static> AsyncSocket for A {}
14
15/// Delegate [`AsyncSocket`] implementation to the underlying socket.
16///
17/// This is meant to be used only by API implementations.
18///
19/// # See also
20/// * [PR in tokio](https://github.com/tokio-rs/tokio/pull/3540)
21/// * [PR in futures](https://github.com/rust-lang/futures-rs/pull/2352)
22#[cfg(feature = "runtime-tokio")]
23#[macro_export]
24macro_rules! spi_async_socket_impl_delegate {
25    ( "AsyncRead" ) => {
26        fn poll_read(
27            self: std::pin::Pin<&mut Self>,
28            cx: &mut std::task::Context<'_>,
29            buf: &mut $crate::runtime::ReadBuf,
30        ) -> std::task::Poll<std::io::Result<()>> {
31            self.deref_pin_mut_for_impl_socket().poll_read(cx, buf)
32        }
33    };
34    ( "AsyncWrite" ) => {
35        fn poll_write(
36            self: std::pin::Pin<&mut Self>,
37            cx: &mut std::task::Context<'_>,
38            buf: &[u8],
39        ) -> std::task::Poll<std::io::Result<usize>> {
40            self.deref_pin_mut_for_impl_socket().poll_write(cx, buf)
41        }
42
43        fn poll_write_vectored(
44            self: std::pin::Pin<&mut Self>,
45            cx: &mut std::task::Context<'_>,
46            bufs: &[std::io::IoSlice<'_>],
47        ) -> std::task::Poll<std::result::Result<usize, std::io::Error>> {
48            self.deref_pin_mut_for_impl_socket()
49                .poll_write_vectored(cx, bufs)
50        }
51
52        fn is_write_vectored(&self) -> bool {
53            self.deref_for_impl_socket().is_write_vectored()
54        }
55
56        fn poll_flush(
57            self: std::pin::Pin<&mut Self>,
58            cx: &mut std::task::Context<'_>,
59        ) -> std::task::Poll<std::io::Result<()>> {
60            self.deref_pin_mut_for_impl_socket().poll_flush(cx)
61        }
62
63        fn poll_shutdown(
64            self: std::pin::Pin<&mut Self>,
65            ctx: &mut std::task::Context<'_>,
66        ) -> std::task::Poll<std::io::Result<()>> {
67            self.deref_pin_mut_for_impl_socket().poll_shutdown(ctx)
68        }
69    };
70    ( $t:ident <S> ) => {
71        impl<S: $crate::AsyncSocket> $crate::runtime::AsyncRead for $t<S> {
72            spi_async_socket_impl_delegate!("AsyncRead");
73        }
74
75        impl<S: $crate::AsyncSocket> $crate::runtime::AsyncWrite for $t<S> {
76            spi_async_socket_impl_delegate!("AsyncWrite");
77        }
78    };
79    ( $t:ty ) => {
80        impl $crate::runtime::AsyncRead for $t {
81            spi_async_socket_impl_delegate!("AsyncRead");
82        }
83
84        impl $crate::runtime::AsyncWrite for $t {
85            spi_async_socket_impl_delegate!("AsyncWrite");
86        }
87    };
88}
89
90/// Delegate [`AsyncSocket`] implementation to the underlying socket.
91///
92/// This is meant to be used only by API implementations.
93///
94/// # See also
95/// * [PR in tokio](https://github.com/tokio-rs/tokio/pull/3540)
96/// * [PR in futures](https://github.com/rust-lang/futures-rs/pull/2352)
97#[cfg(feature = "runtime-async-std")]
98#[macro_export]
99macro_rules! spi_async_socket_impl_delegate {
100    ( "AsyncRead" ) => {
101        fn poll_read(
102            self: std::pin::Pin<&mut Self>,
103            cx: &mut std::task::Context<'_>,
104            buf: &mut [u8],
105        ) -> std::task::Poll<std::io::Result<usize>> {
106            self.deref_pin_mut_for_impl_socket().poll_read(cx, buf)
107        }
108    };
109    ( "AsyncWrite" ) => {
110        fn poll_write(
111            self: std::pin::Pin<&mut Self>,
112            cx: &mut std::task::Context<'_>,
113            buf: &[u8],
114        ) -> std::task::Poll<std::io::Result<usize>> {
115            self.deref_pin_mut_for_impl_socket().poll_write(cx, buf)
116        }
117
118        fn poll_flush(
119            self: std::pin::Pin<&mut Self>,
120            cx: &mut std::task::Context<'_>,
121        ) -> std::task::Poll<std::io::Result<()>> {
122            self.deref_pin_mut_for_impl_socket().poll_flush(cx)
123        }
124
125        fn poll_close(
126            self: std::pin::Pin<&mut Self>,
127            ctx: &mut std::task::Context<'_>,
128        ) -> std::task::Poll<std::io::Result<()>> {
129            self.deref_pin_mut_for_impl_socket().poll_close(ctx)
130        }
131    };
132    ( $t:ident <S> ) => {
133        impl<S: $crate::AsyncSocket> $crate::runtime::AsyncRead for $t<S> {
134            spi_async_socket_impl_delegate!("AsyncRead");
135        }
136
137        impl<S: $crate::AsyncSocket> $crate::runtime::AsyncWrite for $t<S> {
138            spi_async_socket_impl_delegate!("AsyncWrite");
139        }
140    };
141    ( $t:ty ) => {
142        impl $crate::runtime::AsyncRead for $t {
143            spi_async_socket_impl_delegate!("AsyncRead");
144        }
145
146        impl $crate::runtime::AsyncWrite for $t {
147            spi_async_socket_impl_delegate!("AsyncWrite");
148        }
149    };
150}