From 4262749c466aa55851153c7e0a6e3b992bebef37 Mon Sep 17 00:00:00 2001 From: Thomas BESSOU Date: Mon, 3 Apr 2023 21:17:09 +0200 Subject: [PATCH 1/6] wip --- tokio/src/net/tcp/listener.rs | 79 +++++++++++++++++++++++++++++++++-- tokio/tests/io_driver_drop.rs | 2 +- tokio/tests/net_panic.rs | 2 +- tokio/tests/no_rt.rs | 4 +- 4 files changed, 81 insertions(+), 6 deletions(-) diff --git a/tokio/src/net/tcp/listener.rs b/tokio/src/net/tcp/listener.rs index 34e393c895f..d9063959ecf 100644 --- a/tokio/src/net/tcp/listener.rs +++ b/tokio/src/net/tcp/listener.rs @@ -191,6 +191,50 @@ impl TcpListener { } } + /// Creates new `TcpListener` from a `std::net::TcpListener`. + /// + /// This function is intended to be used to wrap a TCP listener from the + /// standard library in the Tokio equivalent. + /// + /// This API is typically paired with the `socket2` crate and the `Socket` + /// type to build up and customize a listener before it's shipped off to the + /// backing event loop. This allows configuration of options like + /// `SO_REUSEPORT`, binding to multiple addresses, etc. + /// + /// # Notes + /// + /// This sets the socket to non-blocking mode if not already done, which + /// is necessary for normal operation within Tokio. + /// + /// # Examples + /// + /// ```rust,no_run + /// use std::error::Error; + /// use tokio::net::TcpListener; + /// + /// #[tokio::main] + /// async fn main() -> Result<(), Box> { + /// let std_listener = std::net::TcpListener::bind("127.0.0.1:0")?; + /// std_listener.set_nonblocking(true)?; + /// let listener = TcpListener::from_std(std_listener)?; + /// Ok(()) + /// } + /// ``` + /// + /// # Panics + /// + /// This function panics if it is not called from within a runtime with + /// IO enabled. + /// + /// The runtime is usually set implicitly when this function is called + /// from a future driven by a tokio runtime, otherwise runtime can be set + /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. + #[track_caller] + pub fn from_tcp(listener: net::TcpListener) -> io::Result { + listener.set_nonblocking(true)?; + Self::from_tcp_unchecked(listener) + } + /// Creates new `TcpListener` from a `std::net::TcpListener`. /// /// This function is intended to be used to wrap a TCP listener from the @@ -210,6 +254,9 @@ impl TcpListener { /// /// [`set_nonblocking`]: std::net::TcpListener::set_nonblocking /// + /// You may generally prefer using [`from_tcp`](TcpListener::from_tcp), + /// which does that for you if not already done. + /// /// # Examples /// /// ```rust,no_run @@ -234,12 +281,26 @@ impl TcpListener { /// from a future driven by a tokio runtime, otherwise runtime can be set /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. #[track_caller] - pub fn from_std(listener: net::TcpListener) -> io::Result { + pub fn from_tcp_unchecked(listener: net::TcpListener) -> io::Result { let io = mio::net::TcpListener::from_std(listener); let io = PollEvented::new(io)?; Ok(TcpListener { io }) } + /// Creates new `TcpListener` from a `std::net::TcpListener`. + /// + /// Deprecated because easy to misuse and naming doesn't warn enough about it + /// (you may want to favor using + /// [`from_tcp`](TcpListener::from_tcp) instead of + /// [`from_tcp_unchecked`](TcpListener::from_tcp_unchecked)) + /// + /// This has the same behavior as [`TcpListener::from_tcp_unchecked`]. + #[track_caller] + #[deprecated = "Easy to misuse - use from_tcp or from_tcp_unchecked instead"] + pub fn from_std(listener: net::TcpListener) -> io::Result { + Self::from_tcp_unchecked(listener) + } + /// Turns a [`tokio::net::TcpListener`] into a [`std::net::TcpListener`]. /// /// The returned [`std::net::TcpListener`] will have nonblocking mode set as @@ -384,9 +445,21 @@ impl TryFrom for TcpListener { /// Consumes stream, returning the tokio I/O object. /// /// This is equivalent to - /// [`TcpListener::from_std(stream)`](TcpListener::from_std). + /// [`TcpListener::from_tcp_unchecked(stream)`](TcpListener::from_tcp_unchecked). + /// + /// # Notes + /// + /// The caller is responsible for ensuring that the listener is in + /// non-blocking mode. Otherwise all I/O operations on the listener + /// will block the thread, which will cause unexpected behavior. + /// Non-blocking mode can be set using [`set_nonblocking`]. + /// + /// [`set_nonblocking`]: std::net::TcpListener::set_nonblocking + /// + /// You may generally prefer using [`TcpListener::from_tcp`], + /// which does that for you if not already done. fn try_from(stream: net::TcpListener) -> Result { - Self::from_std(stream) + Self::from_tcp_unchecked(stream) } } diff --git a/tokio/tests/io_driver_drop.rs b/tokio/tests/io_driver_drop.rs index 0a384d4196d..3504a7f9417 100644 --- a/tokio/tests/io_driver_drop.rs +++ b/tokio/tests/io_driver_drop.rs @@ -12,7 +12,7 @@ fn tcp_doesnt_block() { let listener = { let _enter = rt.enter(); let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); - TcpListener::from_std(listener).unwrap() + TcpListener::from_std(listener).unwrap() // Given the name of this test it looks like it hasn't fulfilled its purpose when going from mio 0.6 to mio 0.7 -> TODO investigate why }; drop(rt); diff --git a/tokio/tests/net_panic.rs b/tokio/tests/net_panic.rs index fc2209ad4b0..4c73d8d58d0 100644 --- a/tokio/tests/net_panic.rs +++ b/tokio/tests/net_panic.rs @@ -40,7 +40,7 @@ fn tcp_listener_from_std_panic_caller() -> Result<(), Box> { let panic_location_file = test_panic(|| { let rt = runtime_without_io(); rt.block_on(async { - let _ = TcpListener::from_std(std_listener); + let _ = TcpListener::from_tcp_unchecked(std_listener); }); }); diff --git a/tokio/tests/no_rt.rs b/tokio/tests/no_rt.rs index 747fab6af7d..5b70c7952da 100644 --- a/tokio/tests/no_rt.rs +++ b/tokio/tests/no_rt.rs @@ -37,5 +37,7 @@ async fn timeout_value() { expected = "there is no reactor running, must be called from the context of a Tokio 1.x runtime" )] fn io_panics_when_no_tokio_context() { - let _ = tokio::net::TcpListener::from_std(std::net::TcpListener::bind("127.0.0.1:0").unwrap()); + let _ = tokio::net::TcpListener::from_tcp_unchecked( + std::net::TcpListener::bind("127.0.0.1:0").unwrap(), + ); } From 05dc864d46ef8e59c467f3139cd3494b9568f6b4 Mon Sep 17 00:00:00 2001 From: Thomas BESSOU Date: Wed, 5 Apr 2023 13:34:02 +0200 Subject: [PATCH 2/6] improve wording --- tokio/src/net/tcp/listener.rs | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/tokio/src/net/tcp/listener.rs b/tokio/src/net/tcp/listener.rs index d9063959ecf..8160bcea3b7 100644 --- a/tokio/src/net/tcp/listener.rs +++ b/tokio/src/net/tcp/listener.rs @@ -215,8 +215,7 @@ impl TcpListener { /// #[tokio::main] /// async fn main() -> Result<(), Box> { /// let std_listener = std::net::TcpListener::bind("127.0.0.1:0")?; - /// std_listener.set_nonblocking(true)?; - /// let listener = TcpListener::from_std(std_listener)?; + /// let listener = TcpListener::from_tcp(std_listener)?; /// Ok(()) /// } /// ``` @@ -235,20 +234,15 @@ impl TcpListener { Self::from_tcp_unchecked(listener) } - /// Creates new `TcpListener` from a `std::net::TcpListener`. + /// Creates new `TcpListener` from a `std::net::TcpListener` without + /// checking that it's non-blocking. /// /// This function is intended to be used to wrap a TCP listener from the - /// standard library in the Tokio equivalent. + /// standard library in the Tokio equivalent. However, it does not check + /// that it's already in non-blocking mode. /// - /// This API is typically paired with the `socket2` crate and the `Socket` - /// type to build up and customize a listener before it's shipped off to the - /// backing event loop. This allows configuration of options like - /// `SO_REUSEPORT`, binding to multiple addresses, etc. - /// - /// # Notes - /// - /// The caller is responsible for ensuring that the listener is in - /// non-blocking mode. Otherwise all I/O operations on the listener + /// Instead, the caller is responsible for ensuring that the listener is in + /// non-blocking mode, otherwise all I/O operations on the listener /// will block the thread, which will cause unexpected behavior. /// Non-blocking mode can be set using [`set_nonblocking`]. /// @@ -267,7 +261,7 @@ impl TcpListener { /// async fn main() -> Result<(), Box> { /// let std_listener = std::net::TcpListener::bind("127.0.0.1:0")?; /// std_listener.set_nonblocking(true)?; - /// let listener = TcpListener::from_std(std_listener)?; + /// let listener = TcpListener::from_tcp_unchecked(std_listener)?; /// Ok(()) /// } /// ``` @@ -289,12 +283,15 @@ impl TcpListener { /// Creates new `TcpListener` from a `std::net::TcpListener`. /// - /// Deprecated because easy to misuse and naming doesn't warn enough about it - /// (you may want to favor using + /// This function is deprecated because it's easy to misuse, + /// and naming doesn't warn enough about it. + /// + /// You should typically favor using /// [`from_tcp`](TcpListener::from_tcp) instead of - /// [`from_tcp_unchecked`](TcpListener::from_tcp_unchecked)) + /// [`from_tcp_unchecked`](TcpListener::from_tcp_unchecked. /// - /// This has the same behavior as [`TcpListener::from_tcp_unchecked`]. + /// This function however has the same behavior as + /// [`TcpListener::from_tcp_unchecked`]. #[track_caller] #[deprecated = "Easy to misuse - use from_tcp or from_tcp_unchecked instead"] pub fn from_std(listener: net::TcpListener) -> io::Result { From 9e990274a25a6ef39395db72fb0ab09bcc4d17d8 Mon Sep 17 00:00:00 2001 From: Thomas BESSOU Date: Sun, 6 Aug 2023 14:58:38 +0200 Subject: [PATCH 3/6] comments and naming improvements --- tokio/src/net/tcp/listener.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tokio/src/net/tcp/listener.rs b/tokio/src/net/tcp/listener.rs index 6b07aed344f..8d318f35d45 100644 --- a/tokio/src/net/tcp/listener.rs +++ b/tokio/src/net/tcp/listener.rs @@ -215,7 +215,7 @@ impl TcpListener { /// #[tokio::main] /// async fn main() -> Result<(), Box> { /// let std_listener = std::net::TcpListener::bind("127.0.0.1:0")?; - /// let listener = TcpListener::from_tcp(std_listener)?; + /// let listener = TcpListener::from_std_set_nonblocking(std_listener)?; /// Ok(()) /// } /// ``` @@ -229,7 +229,7 @@ impl TcpListener { /// from a future driven by a tokio runtime, otherwise runtime can be set /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. #[track_caller] - pub fn from_tcp(listener: net::TcpListener) -> io::Result { + pub fn from_std_set_nonblocking(listener: net::TcpListener) -> io::Result { listener.set_nonblocking(true)?; Self::from_tcp_unchecked(listener) } @@ -248,8 +248,9 @@ impl TcpListener { /// /// [`set_nonblocking`]: std::net::TcpListener::set_nonblocking /// - /// You may generally prefer using [`from_tcp`](TcpListener::from_tcp), - /// which does that for you if not already done. + /// It may be preferrable to use + /// [`from_std_set_nonblocking`](TcpListener::from_std_set_nonblocking), + /// which sets `nonblocking`. /// /// # Examples /// @@ -286,14 +287,14 @@ impl TcpListener { /// This function is deprecated because it's easy to misuse, /// and naming doesn't warn enough about it. /// - /// You should typically favor using - /// [`from_tcp`](TcpListener::from_tcp) instead of - /// [`from_tcp_unchecked`](TcpListener::from_tcp_unchecked. + /// It may be preferrable to use + /// [`from_std_set_nonblocking`](TcpListener::from_std_set_nonblocking), + /// which sets `nonblocking`. /// /// This function however has the same behavior as /// [`TcpListener::from_tcp_unchecked`]. #[track_caller] - #[deprecated = "Easy to misuse - use from_tcp or from_tcp_unchecked instead"] + #[deprecated = "Easy to misuse - use from_std_set_nonblocking or from_tcp_unchecked instead"] pub fn from_std(listener: net::TcpListener) -> io::Result { Self::from_tcp_unchecked(listener) } @@ -453,8 +454,9 @@ impl TryFrom for TcpListener { /// /// [`set_nonblocking`]: std::net::TcpListener::set_nonblocking /// - /// You may generally prefer using [`TcpListener::from_tcp`], - /// which does that for you if not already done. + /// It may be preferrable to use + /// [`from_std_set_nonblocking`](TcpListener::from_std_set_nonblocking), + /// which sets `nonblocking`. fn try_from(stream: net::TcpListener) -> Result { Self::from_tcp_unchecked(stream) } From b80291d916f73005d018d570076390ab1404b44c Mon Sep 17 00:00:00 2001 From: Thomas BESSOU Date: Sun, 6 Aug 2023 19:01:45 +0200 Subject: [PATCH 4/6] finish for TcpListener --- tokio/src/macros/debug_check_std_blocking.rs | 88 ++++++++++++++++++++ tokio/src/macros/mod.rs | 3 + tokio/src/net/tcp/listener.rs | 26 +++--- tokio/tests/net_panic.rs | 2 +- tokio/tests/no_rt.rs | 2 +- 5 files changed, 108 insertions(+), 13 deletions(-) create mode 100644 tokio/src/macros/debug_check_std_blocking.rs diff --git a/tokio/src/macros/debug_check_std_blocking.rs b/tokio/src/macros/debug_check_std_blocking.rs new file mode 100644 index 00000000000..b364d6db28e --- /dev/null +++ b/tokio/src/macros/debug_check_std_blocking.rs @@ -0,0 +1,88 @@ +//! Debug assertions that from_std_unchecked is not used on blocking sockets. +//! +//! These are displayed warnings in debug mode, panics in test mode +//! (so that nothing slips through in the tokio test suite), and no-ops in release mode. + +#[cfg(all(debug_assertions, not(test)))] +/// Debug assertions that from_std_unchecked is not used on blocking sockets. +/// +/// These are displayed warnings in debug mode, panics in test mode +/// (so that nothing slips through in the tokio test suite), and no-ops in release mode. +macro_rules! debug_check_non_blocking { + ($std_socket: expr, $method: expr, $fallback_method: expr) => {{ + // Make sure the provided item is in non-blocking mode, otherwise warn. + static HAS_WARNED_BLOCKING: std::sync::atomic::AtomicBool = + std::sync::atomic::AtomicBool::new(false); + match socket2::SockRef::from(&$std_socket).nonblocking() { + Ok(true) => {} + Ok(false) => { + if !HAS_WARNED_BLOCKING.swap(true, std::sync::atomic::Ordering::Relaxed) { + println!(concat!( + "WARNING: `", + $method, + "` was called on a socket that is \ + not in non-blocking mode. This is unexpected, and may cause the \ + thread to block indefinitely. Use `", + $fallback_method, + "` instead." + )); + } + } + Err(io_error) => { + if !HAS_WARNED_BLOCKING.swap(true, std::sync::atomic::Ordering::Relaxed) { + println!( + concat!( + "WARNING: `", + $method, + "` was called on a socket which we \ + could not determine whether was in non-blocking mode: {}" + ), + io_error + ); + } + } + } + }}; +} + +#[cfg(test)] +/// Debug assertions that from_std_unchecked is not used on blocking sockets. +/// +/// These are displayed warnings in debug mode, panics in test mode +/// (so that nothing slips through in the tokio test suite), and no-ops in release mode. +macro_rules! debug_check_non_blocking { + ($std_socket: expr, $method: expr, $fallback_method: expr) => {{ + match socket2::SockRef::from(&$std_socket).nonblocking() { + Ok(true) => {} + Ok(false) => { + panic!(concat!( + $method, + "` was called on a socket that is \ + not in non-blocking mode. This is unexpected, and may cause the \ + thread to block indefinitely. Use `", + $fallback_method, + "` instead." + )) + } + Err(io_error) => { + panic!( + concat!( + $method, + "` was called on a socket which we \ + could not determine whether was in non-blocking mode: {}" + ), + io_error + ); + } + } + }}; +} + +#[cfg(not(debug_assertions))] +/// Debug assertions that from_std_unchecked is not used on blocking sockets. +/// +/// These are displayed warnings in debug mode, panics in test mode +/// (so that nothing slips through in the tokio test suite), and no-ops in release mode. +macro_rules! debug_check_non_blocking { + ($($tts:tt)+) => {}; +} diff --git a/tokio/src/macros/mod.rs b/tokio/src/macros/mod.rs index 82f42dbff35..979ec1f736e 100644 --- a/tokio/src/macros/mod.rs +++ b/tokio/src/macros/mod.rs @@ -37,3 +37,6 @@ cfg_macros! { // Includes re-exports needed to implement macros #[doc(hidden)] pub mod support; + +#[macro_use] +mod debug_check_std_blocking; diff --git a/tokio/src/net/tcp/listener.rs b/tokio/src/net/tcp/listener.rs index 8d318f35d45..a8c32ea4155 100644 --- a/tokio/src/net/tcp/listener.rs +++ b/tokio/src/net/tcp/listener.rs @@ -203,8 +203,7 @@ impl TcpListener { /// /// # Notes /// - /// This sets the socket to non-blocking mode if not already done, which - /// is necessary for normal operation within Tokio. + /// This sets the socket to non-blocking mode if it isn't already non-blocking. /// /// # Examples /// @@ -231,7 +230,7 @@ impl TcpListener { #[track_caller] pub fn from_std_set_nonblocking(listener: net::TcpListener) -> io::Result { listener.set_nonblocking(true)?; - Self::from_tcp_unchecked(listener) + Self::from_std_unchecked(listener) } /// Creates new `TcpListener` from a `std::net::TcpListener` without @@ -262,7 +261,7 @@ impl TcpListener { /// async fn main() -> Result<(), Box> { /// let std_listener = std::net::TcpListener::bind("127.0.0.1:0")?; /// std_listener.set_nonblocking(true)?; - /// let listener = TcpListener::from_tcp_unchecked(std_listener)?; + /// let listener = TcpListener::from_std_unchecked(std_listener)?; /// Ok(()) /// } /// ``` @@ -276,15 +275,16 @@ impl TcpListener { /// from a future driven by a tokio runtime, otherwise runtime can be set /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. #[track_caller] - pub fn from_tcp_unchecked(listener: net::TcpListener) -> io::Result { + pub fn from_std_unchecked(listener: net::TcpListener) -> io::Result { let io = mio::net::TcpListener::from_std(listener); let io = PollEvented::new(io)?; Ok(TcpListener { io }) } + #[doc(hidden)] /// Creates new `TcpListener` from a `std::net::TcpListener`. /// - /// This function is deprecated because it's easy to misuse, + /// This function is doc-hidden because it's easy to misuse, /// and naming doesn't warn enough about it. /// /// It may be preferrable to use @@ -292,11 +292,15 @@ impl TcpListener { /// which sets `nonblocking`. /// /// This function however has the same behavior as - /// [`TcpListener::from_tcp_unchecked`]. + /// [`TcpListener::from_std_unchecked`]. #[track_caller] - #[deprecated = "Easy to misuse - use from_std_set_nonblocking or from_tcp_unchecked instead"] pub fn from_std(listener: net::TcpListener) -> io::Result { - Self::from_tcp_unchecked(listener) + debug_check_non_blocking!( + listener, + "TcpListener::from_std", + "TcpListener::from_std_set_nonblocking" + ); + Self::from_std_unchecked(listener) } /// Turns a [`tokio::net::TcpListener`] into a [`std::net::TcpListener`]. @@ -443,7 +447,7 @@ impl TryFrom for TcpListener { /// Consumes stream, returning the tokio I/O object. /// /// This is equivalent to - /// [`TcpListener::from_tcp_unchecked(stream)`](TcpListener::from_tcp_unchecked). + /// [`TcpListener::from_std_unchecked(stream)`](TcpListener::from_std_unchecked). /// /// # Notes /// @@ -458,7 +462,7 @@ impl TryFrom for TcpListener { /// [`from_std_set_nonblocking`](TcpListener::from_std_set_nonblocking), /// which sets `nonblocking`. fn try_from(stream: net::TcpListener) -> Result { - Self::from_tcp_unchecked(stream) + Self::from_std(stream) } } diff --git a/tokio/tests/net_panic.rs b/tokio/tests/net_panic.rs index 7ff95d87c3e..2b39f2bc8d5 100644 --- a/tokio/tests/net_panic.rs +++ b/tokio/tests/net_panic.rs @@ -40,7 +40,7 @@ fn tcp_listener_from_std_panic_caller() -> Result<(), Box> { let panic_location_file = test_panic(|| { let rt = runtime_without_io(); rt.block_on(async { - let _ = TcpListener::from_tcp_unchecked(std_listener); + let _ = TcpListener::from_std_unchecked(std_listener); }); }); diff --git a/tokio/tests/no_rt.rs b/tokio/tests/no_rt.rs index ba16a7134c0..b5ffd0b78f4 100644 --- a/tokio/tests/no_rt.rs +++ b/tokio/tests/no_rt.rs @@ -37,7 +37,7 @@ async fn timeout_value() { expected = "there is no reactor running, must be called from the context of a Tokio 1.x runtime" )] fn io_panics_when_no_tokio_context() { - let _ = tokio::net::TcpListener::from_tcp_unchecked( + let _ = tokio::net::TcpListener::from_std_unchecked( std::net::TcpListener::bind("127.0.0.1:0").unwrap(), ); } From 37c5af5be1949afcdd8182b7199d8aefd63aa0fd Mon Sep 17 00:00:00 2001 From: Thomas BESSOU Date: Sun, 6 Aug 2023 19:10:37 +0200 Subject: [PATCH 5/6] actually put the check in from_std_unchecked --- tokio/src/net/tcp/listener.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tokio/src/net/tcp/listener.rs b/tokio/src/net/tcp/listener.rs index a8c32ea4155..d0c0a19fef2 100644 --- a/tokio/src/net/tcp/listener.rs +++ b/tokio/src/net/tcp/listener.rs @@ -276,6 +276,11 @@ impl TcpListener { /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. #[track_caller] pub fn from_std_unchecked(listener: net::TcpListener) -> io::Result { + debug_check_non_blocking!( + listener, + "TcpListener::from_std", + "TcpListener::from_std_set_nonblocking" + ); let io = mio::net::TcpListener::from_std(listener); let io = PollEvented::new(io)?; Ok(TcpListener { io }) @@ -295,11 +300,6 @@ impl TcpListener { /// [`TcpListener::from_std_unchecked`]. #[track_caller] pub fn from_std(listener: net::TcpListener) -> io::Result { - debug_check_non_blocking!( - listener, - "TcpListener::from_std", - "TcpListener::from_std_set_nonblocking" - ); Self::from_std_unchecked(listener) } @@ -462,7 +462,7 @@ impl TryFrom for TcpListener { /// [`from_std_set_nonblocking`](TcpListener::from_std_set_nonblocking), /// which sets `nonblocking`. fn try_from(stream: net::TcpListener) -> Result { - Self::from_std(stream) + Self::from_std_unchecked(stream) } } From 67078dde7e194cef031bf5d58756a63abaa9049b Mon Sep 17 00:00:00 2001 From: Thomas BESSOU Date: Sun, 6 Aug 2023 19:11:43 +0200 Subject: [PATCH 6/6] make it explicit with regards to what is unchecked --- tokio/src/macros/debug_check_std_blocking.rs | 8 ++++---- tokio/src/net/tcp/listener.rs | 14 +++++++------- tokio/tests/net_panic.rs | 2 +- tokio/tests/no_rt.rs | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/tokio/src/macros/debug_check_std_blocking.rs b/tokio/src/macros/debug_check_std_blocking.rs index b364d6db28e..50ed9e135dc 100644 --- a/tokio/src/macros/debug_check_std_blocking.rs +++ b/tokio/src/macros/debug_check_std_blocking.rs @@ -1,10 +1,10 @@ -//! Debug assertions that from_std_unchecked is not used on blocking sockets. +//! Debug assertions that from_std_assume_nonblocking is not used on blocking sockets. //! //! These are displayed warnings in debug mode, panics in test mode //! (so that nothing slips through in the tokio test suite), and no-ops in release mode. #[cfg(all(debug_assertions, not(test)))] -/// Debug assertions that from_std_unchecked is not used on blocking sockets. +/// Debug assertions that from_std_assume_nonblocking is not used on blocking sockets. /// /// These are displayed warnings in debug mode, panics in test mode /// (so that nothing slips through in the tokio test suite), and no-ops in release mode. @@ -46,7 +46,7 @@ macro_rules! debug_check_non_blocking { } #[cfg(test)] -/// Debug assertions that from_std_unchecked is not used on blocking sockets. +/// Debug assertions that from_std_assume_nonblocking is not used on blocking sockets. /// /// These are displayed warnings in debug mode, panics in test mode /// (so that nothing slips through in the tokio test suite), and no-ops in release mode. @@ -79,7 +79,7 @@ macro_rules! debug_check_non_blocking { } #[cfg(not(debug_assertions))] -/// Debug assertions that from_std_unchecked is not used on blocking sockets. +/// Debug assertions that from_std_assume_nonblocking is not used on blocking sockets. /// /// These are displayed warnings in debug mode, panics in test mode /// (so that nothing slips through in the tokio test suite), and no-ops in release mode. diff --git a/tokio/src/net/tcp/listener.rs b/tokio/src/net/tcp/listener.rs index d0c0a19fef2..ac5e9cc4f77 100644 --- a/tokio/src/net/tcp/listener.rs +++ b/tokio/src/net/tcp/listener.rs @@ -230,7 +230,7 @@ impl TcpListener { #[track_caller] pub fn from_std_set_nonblocking(listener: net::TcpListener) -> io::Result { listener.set_nonblocking(true)?; - Self::from_std_unchecked(listener) + Self::from_std_assume_nonblocking(listener) } /// Creates new `TcpListener` from a `std::net::TcpListener` without @@ -261,7 +261,7 @@ impl TcpListener { /// async fn main() -> Result<(), Box> { /// let std_listener = std::net::TcpListener::bind("127.0.0.1:0")?; /// std_listener.set_nonblocking(true)?; - /// let listener = TcpListener::from_std_unchecked(std_listener)?; + /// let listener = TcpListener::from_std_assume_nonblocking(std_listener)?; /// Ok(()) /// } /// ``` @@ -275,7 +275,7 @@ impl TcpListener { /// from a future driven by a tokio runtime, otherwise runtime can be set /// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function. #[track_caller] - pub fn from_std_unchecked(listener: net::TcpListener) -> io::Result { + pub fn from_std_assume_nonblocking(listener: net::TcpListener) -> io::Result { debug_check_non_blocking!( listener, "TcpListener::from_std", @@ -297,10 +297,10 @@ impl TcpListener { /// which sets `nonblocking`. /// /// This function however has the same behavior as - /// [`TcpListener::from_std_unchecked`]. + /// [`TcpListener::from_std_assume_nonblocking`]. #[track_caller] pub fn from_std(listener: net::TcpListener) -> io::Result { - Self::from_std_unchecked(listener) + Self::from_std_assume_nonblocking(listener) } /// Turns a [`tokio::net::TcpListener`] into a [`std::net::TcpListener`]. @@ -447,7 +447,7 @@ impl TryFrom for TcpListener { /// Consumes stream, returning the tokio I/O object. /// /// This is equivalent to - /// [`TcpListener::from_std_unchecked(stream)`](TcpListener::from_std_unchecked). + /// [`TcpListener::from_std_assume_nonblocking(stream)`](TcpListener::from_std_assume_nonblocking). /// /// # Notes /// @@ -462,7 +462,7 @@ impl TryFrom for TcpListener { /// [`from_std_set_nonblocking`](TcpListener::from_std_set_nonblocking), /// which sets `nonblocking`. fn try_from(stream: net::TcpListener) -> Result { - Self::from_std_unchecked(stream) + Self::from_std_assume_nonblocking(stream) } } diff --git a/tokio/tests/net_panic.rs b/tokio/tests/net_panic.rs index 2b39f2bc8d5..b2954b350d7 100644 --- a/tokio/tests/net_panic.rs +++ b/tokio/tests/net_panic.rs @@ -40,7 +40,7 @@ fn tcp_listener_from_std_panic_caller() -> Result<(), Box> { let panic_location_file = test_panic(|| { let rt = runtime_without_io(); rt.block_on(async { - let _ = TcpListener::from_std_unchecked(std_listener); + let _ = TcpListener::from_std_assume_nonblocking(std_listener); }); }); diff --git a/tokio/tests/no_rt.rs b/tokio/tests/no_rt.rs index b5ffd0b78f4..306132f3f13 100644 --- a/tokio/tests/no_rt.rs +++ b/tokio/tests/no_rt.rs @@ -37,7 +37,7 @@ async fn timeout_value() { expected = "there is no reactor running, must be called from the context of a Tokio 1.x runtime" )] fn io_panics_when_no_tokio_context() { - let _ = tokio::net::TcpListener::from_std_unchecked( + let _ = tokio::net::TcpListener::from_std_assume_nonblocking( std::net::TcpListener::bind("127.0.0.1:0").unwrap(), ); }