1use async_std::io;
2use async_std::net;
3#[cfg(unix)]
4use async_std::os::unix::net as unix;
5use crate::Incoming;
6use crate::SocketAddr;
7use crate::Stream;
8
9#[derive(Debug)]
10pub enum Listener {
11 Inet(net::TcpListener),
12 #[cfg(unix)]
13 Unix(unix::UnixListener)
14}
15
16impl From<net::TcpListener> for Listener {
17 fn from(s: net::TcpListener) -> Listener {
18 Listener::Inet(s)
19 }
20}
21
22#[cfg(unix)]
23impl From<unix::UnixListener> for Listener {
24 fn from(s: unix::UnixListener) -> Listener {
25 Listener::Unix(s)
26 }
27}
28
29impl Listener {
30 pub async fn bind(s: &SocketAddr) -> io::Result<Listener> {
31 match s {
32 SocketAddr::Inet(s) => net::TcpListener::bind(s).await.map(Listener::Inet),
33 #[cfg(unix)]
34 SocketAddr::Unix(s) => unix::UnixListener::bind(s).await.map(Listener::Unix)
35 }
36 }
37
38 pub async fn accept(&self) -> io::Result<(Stream, SocketAddr)> {
39 match self {
40 Listener::Inet(l) => l.accept().await.map(|(s,e)| (s.into(), e.into())),
41 #[cfg(unix)]
42 Listener::Unix(l) => l.accept().await.map(|(s,e)| (s.into(), e.into()))
43 }
44 }
45
46 pub fn incoming(&self) -> Incoming<'_> {
47 match self {
48 Listener::Inet(l) => Incoming::from(l.incoming()),
49 #[cfg(unix)]
50 Listener::Unix(l) => Incoming::from(l.incoming()),
51 }
52 }
53}