From d400ddfc97212b4a2844d741b095dab2c6d15543 Mon Sep 17 00:00:00 2001 From: Thomas de Zeeuw Date: Fri, 24 Dec 2021 14:15:54 +0100 Subject: [PATCH] Move Poll::new method into a single impl block Make the documentation a bit nicer. Usually the creation methods, such as new, are the first methods in the first impl block, so that is where people look for them. --- src/poll.rs | 84 ++++++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/src/poll.rs b/src/poll.rs index fd643fdd0..eb89ea6d9 100644 --- a/src/poll.rs +++ b/src/poll.rs @@ -234,6 +234,47 @@ pub struct Registry { } impl Poll { + cfg_os_poll! { + /// Return a new `Poll` handle. + /// + /// This function will make a syscall to the operating system to create + /// the system selector. If this syscall fails, `Poll::new` will return + /// with the error. + /// + /// See [struct] level docs for more details. + /// + /// [struct]: struct.Poll.html + /// + /// # Examples + /// + /// ``` + /// # use std::error::Error; + /// # fn main() -> Result<(), Box> { + /// use mio::{Poll, Events}; + /// use std::time::Duration; + /// + /// let mut poll = match Poll::new() { + /// Ok(poll) => poll, + /// Err(e) => panic!("failed to create Poll instance; err={:?}", e), + /// }; + /// + /// // Create a structure to receive polled events + /// let mut events = Events::with_capacity(1024); + /// + /// // Wait for events, but none will be received because no + /// // `event::Source`s have been registered with this `Poll` instance. + /// poll.poll(&mut events, Some(Duration::from_millis(500)))?; + /// assert!(events.is_empty()); + /// # Ok(()) + /// # } + /// ``` + pub fn new() -> io::Result { + sys::Selector::new().map(|selector| Poll { + registry: Registry { selector }, + }) + } + } + /// Create a separate `Registry` which can be used to register /// `event::Source`s. pub fn registry(&self) -> &Registry { @@ -338,49 +379,6 @@ impl Poll { } } -cfg_os_poll! { - impl Poll { - /// Return a new `Poll` handle. - /// - /// This function will make a syscall to the operating system to create - /// the system selector. If this syscall fails, `Poll::new` will return - /// with the error. - /// - /// See [struct] level docs for more details. - /// - /// [struct]: struct.Poll.html - /// - /// # Examples - /// - /// ``` - /// # use std::error::Error; - /// # fn main() -> Result<(), Box> { - /// use mio::{Poll, Events}; - /// use std::time::Duration; - /// - /// let mut poll = match Poll::new() { - /// Ok(poll) => poll, - /// Err(e) => panic!("failed to create Poll instance; err={:?}", e), - /// }; - /// - /// // Create a structure to receive polled events - /// let mut events = Events::with_capacity(1024); - /// - /// // Wait for events, but none will be received because no - /// // `event::Source`s have been registered with this `Poll` instance. - /// poll.poll(&mut events, Some(Duration::from_millis(500)))?; - /// assert!(events.is_empty()); - /// # Ok(()) - /// # } - /// ``` - pub fn new() -> io::Result { - sys::Selector::new().map(|selector| Poll { - registry: Registry { selector }, - }) - } - } -} - #[cfg(unix)] impl AsRawFd for Poll { fn as_raw_fd(&self) -> RawFd {