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