From 82a4d5d80901b6762c504f46bd95da9e279a35cf Mon Sep 17 00:00:00 2001 From: Irving Ou Date: Wed, 7 Feb 2024 12:32:01 -0500 Subject: [PATCH 1/8] 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. From aaef907b7f3e91bb9a5d3da9e109a52ea768fb5d Mon Sep 17 00:00:00 2001 From: Irving Ou Date: Wed, 7 Feb 2024 12:34:52 -0500 Subject: [PATCH 2/8] CI --- examples/tcp_client.rs | 1 + src/lib.rs | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/tcp_client.rs b/examples/tcp_client.rs index b95fd11..809a84a 100644 --- a/examples/tcp_client.rs +++ b/examples/tcp_client.rs @@ -1,3 +1,4 @@ +#![cfg(any(target_os = "linux", target_os = "windows"))] use std::io; #[cfg(target_os = "linux")] diff --git a/src/lib.rs b/src/lib.rs index 9c017ce..aed06be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -343,10 +343,9 @@ impl Event { /// # Examples /// /// ``` - ///use std::io; /// ///#[cfg(target_os = "linux")] - ///fn main() -> io::Result<()> { + ///fn main() -> std::io::Result<()> { /// use std::net; /// use std::{io::Write, time::Duration}; /// From 52702119bb2bdc3e0e664079ffdd8978755cf944 Mon Sep 17 00:00:00 2001 From: Irving Ou Date: Wed, 7 Feb 2024 12:44:39 -0500 Subject: [PATCH 3/8] doc test --- src/lib.rs | 117 +++++++++++++++-------------------------------------- 1 file changed, 33 insertions(+), 84 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index aed06be..c376929 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -343,90 +343,39 @@ impl Event { /// # Examples /// /// ``` - /// - ///#[cfg(target_os = "linux")] - ///fn main() -> std::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(()) - ///} + /// fn main() -> std::io::Result<()> { + /// use std::net; + /// use std::time::Duration; + /// + /// use polling::Event; + /// use socket2::Type; + /// let poller = polling::Poller::new()?; + /// 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)?; + /// let mut events = polling::Events::new(); + /// + /// let _ = bad_socket.connect(&addr.into()).unwrap_err(); + /// + /// unsafe { + /// poller.add(&bad_socket, Event::writable(0))?; + /// } + /// + /// poller.wait(&mut events, Some(Duration::from_secs(3)))?; + /// + /// let event = events.iter().next().expect("no event"); + /// let is_err = match event.is_err() { + /// Some(is_err) => is_err, + /// None => { + /// println!("not supported in this platform"); + /// return Ok(()); + /// }, + /// }; + /// assert!(is_err); + /// 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. From 27f4d52c3d9ffbb1019a37d7f4049d50823db87d Mon Sep 17 00:00:00 2001 From: Irving Ou Date: Wed, 7 Feb 2024 12:46:30 -0500 Subject: [PATCH 4/8] fix macos --- examples/tcp_client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/tcp_client.rs b/examples/tcp_client.rs index 809a84a..acc1eb9 100644 --- a/examples/tcp_client.rs +++ b/examples/tcp_client.rs @@ -151,7 +151,7 @@ fn main() -> io::Result<()> { Ok(()) } -#[cfg(not(any(target_os = "linux", target_os = "windows")))] +#[cfg(target_os = "macos")] fn main() { println!("This example is not yet supported on this platform."); } From aa248fb837f2d9b88ab733cd56757ce688d7aabc Mon Sep 17 00:00:00 2001 From: Irving Ou Date: Wed, 7 Feb 2024 12:48:44 -0500 Subject: [PATCH 5/8] add andriod --- examples/tcp_client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/tcp_client.rs b/examples/tcp_client.rs index acc1eb9..0048225 100644 --- a/examples/tcp_client.rs +++ b/examples/tcp_client.rs @@ -151,7 +151,7 @@ fn main() -> io::Result<()> { Ok(()) } -#[cfg(target_os = "macos")] +#[cfg(any(target_os = "macos", target_os = "android"))] fn main() { println!("This example is not yet supported on this platform."); } From 9f8f960dd943bc7006751b73525f36e4daf4e4e2 Mon Sep 17 00:00:00 2001 From: Irving Ou Date: Wed, 7 Feb 2024 12:50:00 -0500 Subject: [PATCH 6/8] fix test --- examples/tcp_client.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/examples/tcp_client.rs b/examples/tcp_client.rs index 0048225..0e1f80a 100644 --- a/examples/tcp_client.rs +++ b/examples/tcp_client.rs @@ -1,8 +1,5 @@ -#![cfg(any(target_os = "linux", target_os = "windows"))] -use std::io; - #[cfg(target_os = "linux")] -fn main() -> io::Result<()> { +fn main() -> std::io::Result<()> { use std::net; use std::{io::Write, time::Duration}; @@ -86,7 +83,7 @@ fn main() -> io::Result<()> { } #[cfg(target_os = "windows")] -fn main() -> io::Result<()> { +fn main() -> std::io::Result<()> { use polling::Event; use std::{io::Write, time::Duration}; @@ -151,7 +148,7 @@ fn main() -> io::Result<()> { Ok(()) } -#[cfg(any(target_os = "macos", target_os = "android"))] +#[cfg(not(any(target_os = "linux", target_os = "windows")))] fn main() { println!("This example is not yet supported on this platform."); } From 5a0fd369040cfec5354609502f36292243eaea48 Mon Sep 17 00:00:00 2001 From: Irving Ou Date: Wed, 7 Feb 2024 12:51:59 -0500 Subject: [PATCH 7/8] fix test --- examples/tcp_client.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/tcp_client.rs b/examples/tcp_client.rs index 0e1f80a..621e7c9 100644 --- a/examples/tcp_client.rs +++ b/examples/tcp_client.rs @@ -86,6 +86,7 @@ fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> { use polling::Event; use std::{io::Write, time::Duration}; + use std::io; std::thread::spawn(|| { let listener = std::net::TcpListener::bind("0.0.0.0:8080").unwrap(); From c04e8ee40415cad551fe044457270f4a2d7c491d Mon Sep 17 00:00:00 2001 From: Irving Ou Date: Wed, 7 Feb 2024 12:53:10 -0500 Subject: [PATCH 8/8] fmt --- examples/tcp_client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/tcp_client.rs b/examples/tcp_client.rs index 621e7c9..3a6a5ed 100644 --- a/examples/tcp_client.rs +++ b/examples/tcp_client.rs @@ -85,8 +85,8 @@ fn main() -> std::io::Result<()> { #[cfg(target_os = "windows")] fn main() -> std::io::Result<()> { use polling::Event; - use std::{io::Write, time::Duration}; use std::io; + use std::{io::Write, time::Duration}; std::thread::spawn(|| { let listener = std::net::TcpListener::bind("0.0.0.0:8080").unwrap();