-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update naming around TcpListener::from_std so that it's harder to misuse #5597
base: master
Are you sure you want to change the base?
Changes from 1 commit
4262749
05dc864
5c19066
9e99027
b80291d
37c5af5
67078dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
/// | ||||||||
/// # Examples | ||||||||
/// | ||||||||
/// ```rust,no_run | ||||||||
/// use std::error::Error; | ||||||||
/// use tokio::net::TcpListener; | ||||||||
/// | ||||||||
/// #[tokio::main] | ||||||||
/// async fn main() -> Result<(), Box<dyn Error>> { | ||||||||
/// 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<TcpListener> { | ||||||||
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. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small nit: for reference documentation, I think it's better to avoid "you". I'd rewrite this as "It may be preferable to use |
||||||||
/// | ||||||||
/// # 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<TcpListener> { | ||||||||
pub fn from_tcp_unchecked(listener: net::TcpListener) -> io::Result<TcpListener> { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The documentation of this method should have a bigger focus on the fact that this is an unchecked conversion. Please see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the the fact that the underlying file descriptor might not be set to (I'm assuming that's the naming feedback you're looking for.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my original suggestion it would also return an error if the underlying fd is not actually a tcp socket, but we can debate whether we want that. |
||||||||
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<TcpListener> { | ||||||||
Ten0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
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<net::TcpListener> 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, Self::Error> { | ||||||||
Self::from_std(stream) | ||||||||
Self::from_tcp_unchecked(stream) | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests seem to be related to runtime shutdown. Not sure what the idea with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should fix it on all IO types if we're fixing it on one of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha the trap ^^
Does the wording seem good now? (Before I update them all following the same pattern.)
There's something that makes me feel a but uneasy however: it seems like the naming doesn't make as much sense as
from_std
did...from_tcp
doesn't feel explanatory enough since there is TcpStream, TcpListener...Now to fix that we could name it
TcpStream::from_tcp_stream
, however it is not as obvious in what it does asTcpFile::from_std
.With pipe there was only File so
Pipe::from_file
is explanatory enough. Also, the fact you're asserting that the file is a pipe is the main thing you're interested in in that context so it makes sense that it's named this way.However with our function the main thing we're interested in about that function is that it goes from std to tokio.
=> How about naming it
TcpFile::from_std_checked
? That's more verbose, but at least it's more self-explanatory, you understand it right away when reading code that uses it, and it makes it pretty clear that it should be renamed tofrom_std
in the next major release.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right that it's a worse name than
from_std
. Your other suggestions could also work, but I don't think that they are clearly better thanfrom_tcp
.