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
use io;
use RawFd;
use crateevent;
use crate;
/// Adapter for a `RawFd` providing an [`Evented`] implementation.
///
/// `EventedFd` enables registering any type with an file descriptor with
/// [`OsQueue`].
///
/// While only implementations for TCP and UDP are provided, registering any
/// file descriptor, that can be registered with the underlying OS selector, can
/// be registered with `OsQueue`. `EventedFd` provides the necessary bridge.
///
/// Note that `EventedFd` takes a reference to a `RawFd`. This is because
/// `EventedFd` **does not** take ownership of the file descriptor.
/// Specifically, it will not manage any lifecycle related operations, such as
/// closing the file descriptor on drop. It is expected that the `EventedFd` is
/// constructed right before a call to [`OsQueue::register`]. See the examples
/// below for more detail.
///
/// # Deregistering
///
/// The file descriptor doesn't need to be deregistered **iff** the file
/// descriptor is unique (i.e. it is not duplicated via `dup(2)`) and will be
/// deregistered when it is `close`d.
///
/// # Examples
///
/// Basic usage
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use std::net::TcpListener;
/// use std::os::unix::io::AsRawFd;
///
/// use gaea::event;
/// use gaea::os::{Interests, RegisterOption, OsQueue};
/// use gaea::unix::EventedFd;
///
/// // Bind a listener from the standard library.
/// let listener = TcpListener::bind("127.0.0.1:0")?;
///
/// let mut os_queue = OsQueue::new()?;
///
/// // Register the listener using `EventedFd`.
/// os_queue.register(&mut EventedFd(&listener.as_raw_fd()), event::Id(0), Interests::READABLE, RegisterOption::EDGE)?;
/// # Ok(())
/// # }
/// ```
///
/// Implementing `Evented` for a custom type backed by a `RawFd`.
///
/// ```
/// use std::io;
/// use std::os::unix::io::RawFd;
///
/// use gaea::event;
/// use gaea::os::{Evented, Interests, RegisterOption, OsQueue};
/// use gaea::unix::EventedFd;
///
/// # #[allow(dead_code)]
/// pub struct MyIo {
/// fd: RawFd,
/// }
///
/// impl Evented for MyIo {
/// fn register(&mut self, os_queue: &mut OsQueue, id: event::Id, interests: Interests, opt: RegisterOption) -> io::Result<()> {
/// EventedFd(&self.fd).register(os_queue, id, interests, opt)
/// }
///
/// fn reregister(&mut self, os_queue: &mut OsQueue, id: event::Id, interests: Interests, opt: RegisterOption) -> io::Result<()> {
/// EventedFd(&self.fd).reregister(os_queue, id, interests, opt)
/// }
///
/// fn deregister(&mut self, os_queue: &mut OsQueue) -> io::Result<()> {
/// EventedFd(&self.fd).deregister(os_queue)
/// }
/// }
/// ```
;