async_stream_connection/
listener.rs1use tokio::net::TcpListener;
2#[cfg(unix)]
3use tokio::net::UnixListener;
4
5use std::io;
6#[cfg(unix)]
7use std::os::unix::io::{AsRawFd, RawFd};
8
9use crate::{Addr, Stream};
10
11pub enum Listener {
15 Inet(TcpListener),
17 #[cfg(unix)]
18 Unix(UnixListener),
20}
21impl Listener {
22 pub async fn bind(s: &Addr) -> io::Result<Listener> {
26 match s {
27 Addr::Inet(s) => TcpListener::bind(s).await.map(Listener::Inet),
28 #[cfg(unix)]
29 Addr::Unix(s) => UnixListener::bind(s).map(Listener::Unix),
30 }
31 }
32 pub async fn accept(&self) -> io::Result<(Stream, Addr)> {
37 match self {
38 Listener::Inet(s) => s
39 .accept()
40 .await
41 .map(|(s, a)| (Stream::Inet(s), Addr::Inet(a))),
42 #[cfg(unix)]
43 Listener::Unix(s) => s
44 .accept()
45 .await
46 .map(|(s, a)| (Stream::Unix(s), Addr::from(a))),
47 }
48 }
49}
50#[cfg(unix)]
51impl AsRawFd for Listener {
52 fn as_raw_fd(&self) -> RawFd {
53 match self {
54 Listener::Inet(s) => s.as_raw_fd(),
55 #[cfg(unix)]
56 Listener::Unix(s) => s.as_raw_fd(),
57 }
58 }
59}
60#[cfg(unix)]
61impl Drop for Listener {
62 fn drop(&mut self) {
63 if let Listener::Unix(l) = self {
64 if let Ok(a) = l.local_addr() {
65 if let Some(path) = a.as_pathname() {
66 std::fs::remove_file(path).unwrap();
67 }
68 }
69 }
70 }
71}