From 82a4d5d80901b6762c504f46bd95da9e279a35cf Mon Sep 17 00:00:00 2001 From: Irving Ou Date: Wed, 7 Feb 2024 12:32:01 -0500 Subject: [PATCH] fix doctest --- src/lib.rs | 134 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 85 insertions(+), 49 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9a249d4..9c017ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -343,55 +343,91 @@ impl Event { /// # Examples /// /// ``` - /// #[cfg(target_os = "linux")] - /// fn main() -> io::Result<()> { - /// use std::{io::Write, time::Duration}; - /// - /// std::thread::spawn(|| { - /// let listener = net::TcpListener::bind("0.0.0.0:8080").unwrap(); - /// println!("Listening on {}", listener.local_addr().unwrap()); - /// for stream in listener.incoming() { - /// let mut stream = stream.unwrap(); - /// stream.write(b"Hello, world!\n").unwrap(); - /// } - /// }); - /// std::thread::sleep(Duration::from_millis(100)); - /// let socket = socket2::Socket::new(socket2::Domain::IPV4, Type::STREAM, None)?; - /// let poller = polling::Poller::new()?; - /// unsafe { - /// poller.add(&socket, Event::new(0, true, true))?; - /// } - /// - /// socket.set_nonblocking(true)?; - /// - /// let mut events = polling::Events::new(); - /// poller.wait(&mut events, Some(Duration::from_secs(3)))?; - /// - /// let event = events.iter().next().expect("no event"); - /// - /// assert!(event.is_interrupt()); - /// - /// let addr = net::SocketAddr::new("127.0.0.1".parse().unwrap(), 8080); - /// let err = socket.connect(&addr.into()).unwrap_err(); - /// - /// // EINPROGRESS - /// assert_eq!(115, err.raw_os_error().expect("No OS error")); - /// - /// poller.modify(&socket, Event::writable(0)).expect("modify failed"); - /// events.clear(); - /// poller.wait(&mut events, Some(Duration::from_secs(3)))?; - /// - /// let event = events.iter().next().expect("no event"); - /// - /// assert!(event.writable); - /// assert!(!event.is_interrupt()); - /// assert!(!event.is_err().unwrap()); - /// - /// println!("event: {:?}", event); - /// println!("socket is now writable"); - /// - /// Ok(()) - /// } + ///use std::io; + /// + ///#[cfg(target_os = "linux")] + ///fn main() -> io::Result<()> { + /// use std::net; + /// use std::{io::Write, time::Duration}; + /// + /// use polling::Event; + /// use socket2::Type; + /// + /// std::thread::spawn(|| { + /// let listener = net::TcpListener::bind("0.0.0.0:8080").unwrap(); + /// println!("Listening on {}", listener.local_addr().unwrap()); + /// for stream in listener.incoming() { + /// let mut stream = stream.unwrap(); + /// stream.write_all(b"Hello, world!\n").unwrap(); + /// } + /// }); + /// std::thread::sleep(Duration::from_millis(100)); + /// let socket = socket2::Socket::new(socket2::Domain::IPV4, Type::STREAM, None)?; + /// let poller = polling::Poller::new()?; + /// unsafe { + /// poller.add(&socket, Event::new(0, true, true))?; + /// } + /// + /// socket.set_nonblocking(true)?; + /// + /// let mut events = polling::Events::new(); + /// poller.wait(&mut events, Some(Duration::from_secs(3)))?; + /// + /// let event = events.iter().next().expect("no event"); + /// + /// assert!(event.is_interrupt()); + /// + /// let addr = net::SocketAddr::new("127.0.0.1".parse().unwrap(), 8080); + /// let err = socket.connect(&addr.into()).unwrap_err(); + /// + /// // EINPROGRESS + /// assert_eq!(115, err.raw_os_error().expect("No OS error")); + /// + /// poller + /// .modify(&socket, Event::writable(0)) + /// .expect("modify failed"); + /// events.clear(); + /// poller.wait(&mut events, Some(Duration::from_secs(3)))?; + /// + /// let event = events.iter().next().expect("no event"); + /// + /// assert!(event.writable); + /// assert!(!event.is_interrupt()); + /// assert!(!event.is_err().unwrap()); + /// + /// println!("event: {:?}", event); + /// println!("socket is now writable"); + /// // ======================================================================== + /// // the below is example of a bad socket + /// println!("testing bad socket"); + /// let bad_socket = socket2::Socket::new(socket2::Domain::IPV4, Type::STREAM, None)?; + /// let addr = net::SocketAddr::new("127.0.0.1".parse().unwrap(), 12345); + /// bad_socket.set_nonblocking(true)?; + /// + /// unsafe { + /// poller.add(&bad_socket, Event::writable(0))?; + /// } + /// + /// events.clear(); + /// poller.wait(&mut events, Some(Duration::from_secs(3)))?; + /// + /// let err = bad_socket.connect(&addr.into()).unwrap_err(); + /// assert_eq!(115, err.raw_os_error().expect("No OS error")); + /// + /// poller + /// .modify(&bad_socket, Event::writable(0)) + /// .expect("modify failed"); + /// + /// events.clear(); + /// poller.wait(&mut events, Some(Duration::from_secs(3)))?; + /// + /// let event = events.iter().next().expect("no event"); + /// + /// assert!(event.is_err().unwrap()); + /// println!("bad socket is now in error state"); + /// + /// Ok(()) + ///} /// ``` /// Returns `Some(true)` if the connection has failed, `Some(false)` if there is an error, /// or `None` if the platform does not support detecting this condition.