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
use std::io;
use std::net::{TcpListener, UdpSocket};
#[cfg(not(windows))]
use std::os::unix::net::UnixListener;
#[cfg(not(windows))]
use unix as imp;
#[cfg(windows)]
use windows as imp;
pub struct ListenFd {
fds: Vec<Option<imp::FdType>>,
}
impl ListenFd {
pub fn from_env() -> ListenFd {
match imp::get_fds() {
Some(fds) => ListenFd {
fds: fds.into_iter().map(Some).collect(),
},
None => ListenFd::empty(),
}
}
pub fn empty() -> ListenFd {
ListenFd { fds: vec![] }
}
pub fn len(&self) -> usize {
self.fds.len()
}
fn with_fd<R, F: FnOnce(imp::FdType) -> io::Result<R>>(
&mut self,
idx: usize,
f: F,
) -> io::Result<Option<R>> {
let bucket = match self.fds.get_mut(idx) {
Some(None) | None => return Ok(None),
Some(mut bucket) => bucket,
};
f(*bucket.as_ref().unwrap()).map(|rv| {
bucket.take();
Some(rv)
})
}
pub fn take_tcp_listener(&mut self, idx: usize) -> io::Result<Option<TcpListener>> {
self.with_fd(idx, imp::make_tcp_listener)
}
#[cfg(not(windows))]
pub fn take_unix_listener(&mut self, idx: usize) -> io::Result<Option<UnixListener>> {
self.with_fd(idx, imp::make_unix_listener)
}
pub fn take_udp_socket(&mut self, idx: usize) -> io::Result<Option<UdpSocket>> {
let _idx = idx;
self.with_fd(idx, imp::make_udp_socket)
}
#[cfg(not(windows))]
pub fn take_raw_fd(&mut self, idx: usize) -> io::Result<Option<imp::FdType>> {
let _idx = idx;
self.with_fd(idx, |fd| Ok(fd))
}
#[cfg(windows)]
pub fn take_raw_socket(&mut self, idx: usize) -> io::Result<Option<imp::RawSocket>> {
let _idx = idx;
Ok(None)
}
}