Debian Codegraph @

debian/rust-vendor/mio/src/sys/unix/sourcefd.rs

raw
1 use std::io;
2 #[cfg(not(target_os = "hermit"))]
3 use std::os::fd::RawFd;
4 // TODO: once <https://github.com/rust-lang/rust/issues/126198> is fixed this
5 // can use `std::os::fd` and be merged with the above.
6 #[cfg(target_os = "hermit")]
7 use std::os::hermit::io::RawFd;
8
9 use crate::{event, Interest, Registry, Token};
10
11 /// Adapter for [`RawFd`] providing an [`event::Source`] implementation.
12 ///
13 /// `SourceFd` enables registering any type with an FD with [`Poll`].
14 ///
15 /// While only implementations for TCP and UDP are provided, Mio supports
16 /// registering any FD that can be registered with the underlying OS selector.
17 /// `SourceFd` provides the necessary bridge.
18 ///
19 /// Note that `SourceFd` takes a `&RawFd`. This is because `SourceFd` **does
20 /// not** take ownership of the FD. Specifically, it will not manage any
21 /// lifecycle related operations, such as closing the FD on drop. It is expected
22 /// that the `SourceFd` is constructed right before a call to
23 /// [`Registry::register`]. See the examples for more detail.
24 ///
25 /// [`event::Source`]: ../event/trait.Source.html
26 /// [`Poll`]: ../struct.Poll.html
27 /// [`Registry::register`]: ../struct.Registry.html#method.register
28 ///
29 /// # Examples
30 ///
31 /// Basic usage.
32 ///
33 #[cfg_attr(
34 all(feature = "os-poll", feature = "net", feature = "os-ext"),
35 doc = "```"
36 )]
37 #[cfg_attr(
38 not(all(feature = "os-poll", feature = "net", feature = "os-ext")),
39 doc = "```ignore"
40 )]
41 /// # use std::error::Error;
42 /// # fn main() -> Result<(), Box<dyn Error>> {
43 /// use mio::{Interest, Poll, Token};
44 /// #[cfg(unix)]
45 /// use mio::unix::SourceFd;
46 /// #[cfg(target_os = "wasi")]
47 /// use mio::wasi::SourceFd;
48 ///
49 /// use std::os::fd::AsRawFd;
50 /// use std::net::TcpListener;
51 ///
52 /// // Bind a std listener
53 /// let listener = TcpListener::bind("127.0.0.1:0")?;
54 ///
55 /// let poll = Poll::new()?;
56 ///
57 /// // Register the listener
58 /// poll.registry().register(
59 /// &mut SourceFd(&listener.as_raw_fd()),
60 /// Token(0),
61 /// Interest::READABLE)?;
62 /// # Ok(())
63 /// # }
64 /// ```
65 ///
66 /// Implementing [`event::Source`] for a custom type backed by a [`RawFd`].
67 ///
68 #[cfg_attr(all(feature = "os-poll", feature = "os-ext"), doc = "```")]
69 #[cfg_attr(not(all(feature = "os-poll", feature = "os-ext")), doc = "```ignore")]
70 /// use mio::{event, Interest, Registry, Token};
71 /// #[cfg(unix)]
72 /// use mio::unix::SourceFd;
73 /// #[cfg(target_os = "wasi")]
74 /// use mio::wasi::SourceFd;
75 ///
76 /// use std::os::fd::RawFd;
77 /// use std::io;
78 ///
79 /// # #[allow(dead_code)]
80 /// pub struct MyIo {
81 /// fd: RawFd,
82 /// }
83 ///
84 /// impl event::Source for MyIo {
85 /// fn register(&mut self, registry: &Registry, token: Token, interests: Interest)
86 /// -> io::Result<()>
87 /// {
88 /// SourceFd(&self.fd).register(registry, token, interests)
89 /// }
90 ///
91 /// fn reregister(&mut self, registry: &Registry, token: Token, interests: Interest)
92 /// -> io::Result<()>
93 /// {
94 /// SourceFd(&self.fd).reregister(registry, token, interests)
95 /// }
96 ///
97 /// fn deregister(&mut self, registry: &Registry) -> io::Result<()> {
98 /// SourceFd(&self.fd).deregister(registry)
99 /// }
100 /// }
101 /// ```
102 #[derive(Debug)]
103 pub struct SourceFd<'a>(pub &'a RawFd);
104
105 impl<'a> event::Source for SourceFd<'a> {
106 fn register(
107 &mut self,
108 registry: &Registry,
109 token: Token,
110 interests: Interest,
111 ) -> io::Result<()> {
112 registry.selector().register(*self.0, token, interests)
113 }
114
115 fn reregister(
116 &mut self,
117 registry: &Registry,
118 token: Token,
119 interests: Interest,
120 ) -> io::Result<()> {
121 registry.selector().reregister(*self.0, token, interests)
122 }
123
124 fn deregister(&mut self, registry: &Registry) -> io::Result<()> {
125 registry.selector().deregister(*self.0)
126 }
127 }