Skip to content

Commit

Permalink
Move Poll::new method into a single impl block
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Thomasdezeeuw committed Dec 25, 2021
1 parent 0ffb287 commit d400ddf
Showing 1 changed file with 41 additions and 43 deletions.
84 changes: 41 additions & 43 deletions src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Error>> {
/// 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<Poll> {
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 {
Expand Down Expand Up @@ -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<dyn Error>> {
/// 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<Poll> {
sys::Selector::new().map(|selector| Poll {
registry: Registry { selector },
})
}
}
}

#[cfg(unix)]
impl AsRawFd for Poll {
fn as_raw_fd(&self) -> RawFd {
Expand Down

0 comments on commit d400ddf

Please sign in to comment.