char/avril

(wip) why'd you have to go and make things so complicaaaaated?

git clone https://git.t4t.associates/char/avril

Charlotte Somrearrange libs into lib and extensions into extsf4a0f5b

main
2.5 KiB88 linesraw
1use std::io::{self, IoSlice};
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
6
7pub enum Socket {
8    Tcp(tokio::net::TcpStream),
9    #[cfg(unix)]
10    Unix(tokio::net::UnixStream),
11}
12
13impl From<tokio::net::TcpStream> for Socket {
14    fn from(socket: tokio::net::TcpStream) -> Self {
15        Self::Tcp(socket)
16    }
17}
18
19#[cfg(unix)]
20impl From<tokio::net::UnixStream> for Socket {
21    fn from(socket: tokio::net::UnixStream) -> Self {
22        Self::Unix(socket)
23    }
24}
25
26impl AsyncRead for Socket {
27    fn poll_read(
28        self: Pin<&mut Self>,
29        cx: &mut Context<'_>,
30        buffer: &mut ReadBuf<'_>,
31    ) -> Poll<io::Result<()>> {
32        match self.get_mut() {
33            Self::Tcp(socket) => Pin::new(socket).poll_read(cx, buffer),
34            #[cfg(unix)]
35            Self::Unix(socket) => Pin::new(socket).poll_read(cx, buffer),
36        }
37    }
38}
39
40impl AsyncWrite for Socket {
41    fn poll_write(
42        self: Pin<&mut Self>,
43        cx: &mut Context<'_>,
44        buffer: &[u8],
45    ) -> Poll<io::Result<usize>> {
46        match self.get_mut() {
47            Self::Tcp(socket) => Pin::new(socket).poll_write(cx, buffer),
48            #[cfg(unix)]
49            Self::Unix(socket) => Pin::new(socket).poll_write(cx, buffer),
50        }
51    }
52
53    fn poll_write_vectored(
54        self: Pin<&mut Self>,
55        cx: &mut Context<'_>,
56        buffers: &[IoSlice<'_>],
57    ) -> Poll<io::Result<usize>> {
58        match self.get_mut() {
59            Self::Tcp(socket) => Pin::new(socket).poll_write_vectored(cx, buffers),
60            #[cfg(unix)]
61            Self::Unix(socket) => Pin::new(socket).poll_write_vectored(cx, buffers),
62        }
63    }
64
65    fn is_write_vectored(&self) -> bool {
66        match self {
67            Self::Tcp(socket) => socket.is_write_vectored(),
68            #[cfg(unix)]
69            Self::Unix(socket) => socket.is_write_vectored(),
70        }
71    }
72
73    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
74        match self.get_mut() {
75            Self::Tcp(socket) => Pin::new(socket).poll_flush(cx),
76            #[cfg(unix)]
77            Self::Unix(socket) => Pin::new(socket).poll_flush(cx),
78        }
79    }
80
81    fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
82        match self.get_mut() {
83            Self::Tcp(socket) => Pin::new(socket).poll_shutdown(cx),
84            #[cfg(unix)]
85            Self::Unix(socket) => Pin::new(socket).poll_shutdown(cx),
86        }
87    }
88}