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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Driver trait.

// UNSAFETY: Needed to define and implement the unsafe new_dyn_overlapped_file method.
#![cfg_attr(windows, allow(unsafe_code))]

#[cfg(unix)]
use crate::fd::FdReadyDriver;
#[cfg(unix)]
use crate::fd::PollFdReady;
use crate::socket::PollSocketReady;
use crate::socket::SocketReadyDriver;
#[cfg(windows)]
use crate::sys::overlapped::IoOverlapped;
#[cfg(windows)]
use crate::sys::overlapped::OverlappedIoDriver;
use crate::task::Spawn;
use crate::timer::PollTimer;
use crate::timer::TimerDriver;
use crate::wait::PollWait;
use crate::wait::WaitDriver;
use smallbox::space::S4;
use smallbox::SmallBox;
use std::io;
#[cfg(unix)]
use std::os::unix::prelude::*;
#[cfg(windows)]
use std::os::windows::prelude::*;
use std::sync::Arc;

/// A generic `Box`-like container of one of the polled types.
pub type PollImpl<T> = SmallBox<T, S4>;

/// A driver that supports polled IO.
pub trait Driver: 'static + Send + Sync {
    /// Returns a new timer.
    fn new_dyn_timer(&self) -> PollImpl<dyn PollTimer>;

    /// Returns a new object for polling file descriptor readiness.
    #[cfg(unix)]
    fn new_dyn_fd_ready(&self, fd: RawFd) -> io::Result<PollImpl<dyn PollFdReady>>;

    /// Creates a new object for polling socket readiness.
    #[cfg(windows)]
    fn new_dyn_socket_ready(&self, socket: RawSocket) -> io::Result<PollImpl<dyn PollSocketReady>>;

    /// Creates a new object for polling socket readiness.
    #[cfg(unix)]
    fn new_dyn_socket_ready(&self, socket: RawFd) -> io::Result<PollImpl<dyn PollSocketReady>>;

    /// Creates a new wait.
    #[cfg(windows)]
    fn new_dyn_wait(&self, handle: RawHandle) -> io::Result<PollImpl<dyn PollWait>>;

    /// Creates a new wait.
    ///
    /// Signals will be consumed using reads of `read_size` bytes, with 8-byte
    /// buffer alignment. `read_size` must be at most
    /// [`MAXIMUM_WAIT_READ_SIZE`](super::wait::MAXIMUM_WAIT_READ_SIZE) bytes.
    #[cfg(unix)]
    fn new_dyn_wait(&self, fd: RawFd, read_size: usize) -> io::Result<PollImpl<dyn PollWait>>;

    /// Creates a new overlapped file handler.
    ///
    /// # Safety
    /// The caller must ensure that they exclusively own `handle`, and that
    /// `handle` stays alive until the new handler is dropped.
    #[cfg(windows)]
    unsafe fn new_dyn_overlapped_file(
        &self,
        handle: RawHandle,
    ) -> io::Result<PollImpl<dyn IoOverlapped>>;
}

#[cfg(unix)]
impl<T> Driver for T
where
    T: 'static + Send + Sync + FdReadyDriver + TimerDriver + SocketReadyDriver + WaitDriver,
{
    fn new_dyn_timer(&self) -> PollImpl<dyn PollTimer> {
        smallbox::smallbox!(self.new_timer())
    }

    fn new_dyn_fd_ready(&self, fd: RawFd) -> io::Result<PollImpl<dyn PollFdReady>> {
        Ok(smallbox::smallbox!(self.new_fd_ready(fd)?))
    }

    fn new_dyn_socket_ready(&self, socket: RawFd) -> io::Result<PollImpl<dyn PollSocketReady>> {
        Ok(smallbox::smallbox!(self.new_socket_ready(socket)?))
    }

    fn new_dyn_wait(&self, fd: RawFd, read_size: usize) -> io::Result<PollImpl<dyn PollWait>> {
        Ok(smallbox::smallbox!(self.new_wait(fd, read_size)?))
    }
}

#[cfg(windows)]
impl<T> Driver for T
where
    T: 'static + Send + Sync + TimerDriver + SocketReadyDriver + WaitDriver + OverlappedIoDriver,
{
    fn new_dyn_timer(&self) -> PollImpl<dyn PollTimer> {
        smallbox::smallbox!(self.new_timer())
    }

    fn new_dyn_socket_ready(&self, socket: RawSocket) -> io::Result<PollImpl<dyn PollSocketReady>> {
        Ok(smallbox::smallbox!(self.new_socket_ready(socket)?))
    }

    fn new_dyn_wait(&self, handle: RawHandle) -> io::Result<PollImpl<dyn PollWait>> {
        Ok(smallbox::smallbox!(self.new_wait(handle)?))
    }

    unsafe fn new_dyn_overlapped_file(
        &self,
        handle: RawHandle,
    ) -> io::Result<PollImpl<dyn IoOverlapped>> {
        // SAFETY: caller guarantees contract
        Ok(smallbox::smallbox!(unsafe {
            self.new_overlapped_file(handle)
        }?))
    }
}

#[cfg(unix)]
impl Driver for Box<dyn Driver> {
    fn new_dyn_timer(&self) -> PollImpl<dyn PollTimer> {
        self.as_ref().new_dyn_timer()
    }

    fn new_dyn_fd_ready(&self, fd: RawFd) -> io::Result<PollImpl<dyn PollFdReady>> {
        self.as_ref().new_dyn_fd_ready(fd)
    }

    fn new_dyn_socket_ready(&self, socket: RawFd) -> io::Result<PollImpl<dyn PollSocketReady>> {
        self.as_ref().new_dyn_socket_ready(socket)
    }

    fn new_dyn_wait(&self, fd: RawFd, read_size: usize) -> io::Result<PollImpl<dyn PollWait>> {
        self.as_ref().new_dyn_wait(fd, read_size)
    }
}

#[cfg(windows)]
impl Driver for Box<dyn Driver> {
    fn new_dyn_timer(&self) -> PollImpl<dyn PollTimer> {
        self.as_ref().new_dyn_timer()
    }

    fn new_dyn_socket_ready(&self, socket: RawSocket) -> io::Result<PollImpl<dyn PollSocketReady>> {
        self.as_ref().new_dyn_socket_ready(socket)
    }

    fn new_dyn_wait(&self, handle: RawHandle) -> io::Result<PollImpl<dyn PollWait>> {
        self.as_ref().new_dyn_wait(handle)
    }

    unsafe fn new_dyn_overlapped_file(
        &self,
        handle: RawHandle,
    ) -> io::Result<PollImpl<dyn IoOverlapped>> {
        // SAFETY: caller guarantees contract
        unsafe { self.as_ref().new_dyn_overlapped_file(handle) }
    }
}

#[cfg(unix)]
impl Driver for Arc<dyn Driver> {
    fn new_dyn_timer(&self) -> PollImpl<dyn PollTimer> {
        self.as_ref().new_dyn_timer()
    }

    fn new_dyn_fd_ready(&self, fd: RawFd) -> io::Result<PollImpl<dyn PollFdReady>> {
        self.as_ref().new_dyn_fd_ready(fd)
    }

    fn new_dyn_socket_ready(&self, socket: RawFd) -> io::Result<PollImpl<dyn PollSocketReady>> {
        self.as_ref().new_dyn_socket_ready(socket)
    }

    fn new_dyn_wait(&self, fd: RawFd, read_size: usize) -> io::Result<PollImpl<dyn PollWait>> {
        self.as_ref().new_dyn_wait(fd, read_size)
    }
}

#[cfg(windows)]
impl Driver for Arc<dyn Driver> {
    fn new_dyn_timer(&self) -> PollImpl<dyn PollTimer> {
        self.as_ref().new_dyn_timer()
    }

    fn new_dyn_socket_ready(&self, socket: RawSocket) -> io::Result<PollImpl<dyn PollSocketReady>> {
        self.as_ref().new_dyn_socket_ready(socket)
    }

    fn new_dyn_wait(&self, handle: RawHandle) -> io::Result<PollImpl<dyn PollWait>> {
        self.as_ref().new_dyn_wait(handle)
    }

    unsafe fn new_dyn_overlapped_file(
        &self,
        handle: RawHandle,
    ) -> io::Result<PollImpl<dyn IoOverlapped>> {
        // SAFETY: caller guarantees contract
        unsafe { self.as_ref().new_dyn_overlapped_file(handle) }
    }
}

/// Trait for [`Driver`]s that also implement [`Spawn`].
pub trait SpawnDriver: Spawn + Driver {}

impl<T: Spawn + Driver> SpawnDriver for T {}