-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Update mio to 0.8 #4270
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 mio to 0.8 #4270
Changes from all commits
e4a3617
931357c
7433f98
f45cf87
4ee75c0
5326df1
216baeb
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| use crate::net::{TcpListener, TcpStream}; | ||
|
|
||
| use std::convert::TryInto; | ||
| use std::fmt; | ||
| use std::io; | ||
| use std::net::SocketAddr; | ||
|
|
@@ -83,7 +84,7 @@ cfg_net! { | |
| /// [`socket2`]: https://docs.rs/socket2/ | ||
| #[cfg_attr(docsrs, doc(alias = "connect_std"))] | ||
| pub struct TcpSocket { | ||
| inner: mio::net::TcpSocket, | ||
| inner: socket2::Socket, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -118,7 +119,11 @@ impl TcpSocket { | |
| /// } | ||
| /// ``` | ||
| pub fn new_v4() -> io::Result<TcpSocket> { | ||
| let inner = mio::net::TcpSocket::new_v4()?; | ||
| let inner = socket2::Socket::new( | ||
| socket2::Domain::IPV4, | ||
| socket2::Type::STREAM, | ||
| Some(socket2::Protocol::TCP), | ||
| )?; | ||
| Ok(TcpSocket { inner }) | ||
| } | ||
|
|
||
|
|
@@ -152,7 +157,11 @@ impl TcpSocket { | |
| /// } | ||
| /// ``` | ||
| pub fn new_v6() -> io::Result<TcpSocket> { | ||
| let inner = mio::net::TcpSocket::new_v6()?; | ||
| let inner = socket2::Socket::new( | ||
| socket2::Domain::IPV6, | ||
| socket2::Type::STREAM, | ||
| Some(socket2::Protocol::TCP), | ||
| )?; | ||
| Ok(TcpSocket { inner }) | ||
| } | ||
|
|
||
|
|
@@ -183,7 +192,7 @@ impl TcpSocket { | |
| /// } | ||
| /// ``` | ||
| pub fn set_reuseaddr(&self, reuseaddr: bool) -> io::Result<()> { | ||
| self.inner.set_reuseaddr(reuseaddr) | ||
| self.inner.set_reuse_address(reuseaddr) | ||
| } | ||
|
|
||
| /// Retrieves the value set for `SO_REUSEADDR` on this socket. | ||
|
|
@@ -209,7 +218,7 @@ impl TcpSocket { | |
| /// } | ||
| /// ``` | ||
| pub fn reuseaddr(&self) -> io::Result<bool> { | ||
| self.inner.get_reuseaddr() | ||
| self.inner.reuse_address() | ||
| } | ||
|
|
||
| /// Allows the socket to bind to an in-use port. Only available for unix systems | ||
|
|
@@ -243,7 +252,7 @@ impl TcpSocket { | |
| doc(cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))) | ||
| )] | ||
| pub fn set_reuseport(&self, reuseport: bool) -> io::Result<()> { | ||
| self.inner.set_reuseport(reuseport) | ||
| self.inner.set_reuse_port(reuseport) | ||
| } | ||
|
|
||
| /// Allows the socket to bind to an in-use port. Only available for unix systems | ||
|
|
@@ -278,14 +287,14 @@ impl TcpSocket { | |
| doc(cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))) | ||
| )] | ||
| pub fn reuseport(&self) -> io::Result<bool> { | ||
| self.inner.get_reuseport() | ||
| self.inner.reuse_port() | ||
| } | ||
|
|
||
| /// Sets the size of the TCP send buffer on this socket. | ||
| /// | ||
| /// On most operating systems, this sets the `SO_SNDBUF` socket option. | ||
| pub fn set_send_buffer_size(&self, size: u32) -> io::Result<()> { | ||
| self.inner.set_send_buffer_size(size) | ||
| self.inner.set_send_buffer_size(size as usize) | ||
| } | ||
|
|
||
| /// Returns the size of the TCP send buffer for this socket. | ||
|
|
@@ -312,14 +321,14 @@ impl TcpSocket { | |
| /// | ||
| /// [`set_send_buffer_size`]: #method.set_send_buffer_size | ||
| pub fn send_buffer_size(&self) -> io::Result<u32> { | ||
| self.inner.get_send_buffer_size() | ||
| self.inner.send_buffer_size().map(|n| n as u32) | ||
|
Comment on lines
-315
to
+324
Contributor
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. There are several casts in this PR. Do we risk them truncating any data here?
Member
Author
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. There are 5 casts:
Member
Author
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. fixed in 5326df1 |
||
| } | ||
|
|
||
| /// Sets the size of the TCP receive buffer on this socket. | ||
| /// | ||
| /// On most operating systems, this sets the `SO_RCVBUF` socket option. | ||
| pub fn set_recv_buffer_size(&self, size: u32) -> io::Result<()> { | ||
| self.inner.set_recv_buffer_size(size) | ||
| self.inner.set_recv_buffer_size(size as usize) | ||
| } | ||
|
|
||
| /// Returns the size of the TCP receive buffer for this socket. | ||
|
|
@@ -346,7 +355,7 @@ impl TcpSocket { | |
| /// | ||
| /// [`set_recv_buffer_size`]: #method.set_recv_buffer_size | ||
| pub fn recv_buffer_size(&self) -> io::Result<u32> { | ||
| self.inner.get_recv_buffer_size() | ||
| self.inner.recv_buffer_size().map(|n| n as u32) | ||
| } | ||
|
|
||
| /// Gets the local address of this socket. | ||
|
|
@@ -372,7 +381,9 @@ impl TcpSocket { | |
| /// } | ||
| /// ``` | ||
| pub fn local_addr(&self) -> io::Result<SocketAddr> { | ||
| self.inner.get_localaddr() | ||
| self.inner | ||
| .local_addr() | ||
| .map(|addr| addr.as_socket().unwrap()) | ||
| } | ||
|
|
||
| /// Binds the socket to the given address. | ||
|
|
@@ -404,7 +415,7 @@ impl TcpSocket { | |
| /// } | ||
| /// ``` | ||
| pub fn bind(&self, addr: SocketAddr) -> io::Result<()> { | ||
| self.inner.bind(addr) | ||
| self.inner.bind(&addr.into()) | ||
| } | ||
|
|
||
| /// Establishes a TCP connection with a peer at the specified socket address. | ||
|
|
@@ -440,7 +451,13 @@ impl TcpSocket { | |
| /// } | ||
| /// ``` | ||
| pub async fn connect(self, addr: SocketAddr) -> io::Result<TcpStream> { | ||
| let mio = self.inner.connect(addr)?; | ||
| self.inner.connect(&addr.into())?; | ||
|
|
||
| #[cfg(windows)] | ||
| let mio = unsafe { mio::net::TcpStream::from_raw_socket(self.inner.into_raw_socket()) }; | ||
| #[cfg(unix)] | ||
| let mio = unsafe { mio::net::TcpStream::from_raw_fd(self.inner.into_raw_fd()) }; | ||
|
|
||
| TcpStream::connect_mio(mio).await | ||
| } | ||
|
|
||
|
|
@@ -480,7 +497,14 @@ impl TcpSocket { | |
| /// } | ||
| /// ``` | ||
| pub fn listen(self, backlog: u32) -> io::Result<TcpListener> { | ||
| let mio = self.inner.listen(backlog)?; | ||
| let backlog = backlog.try_into().unwrap_or(i32::MAX); | ||
| self.inner.listen(backlog)?; | ||
|
|
||
| #[cfg(windows)] | ||
| let mio = unsafe { mio::net::TcpListener::from_raw_socket(self.inner.into_raw_socket()) }; | ||
| #[cfg(unix)] | ||
| let mio = unsafe { mio::net::TcpListener::from_raw_fd(self.inner.into_raw_fd()) }; | ||
|
|
||
| TcpListener::new(mio) | ||
| } | ||
|
|
||
|
|
@@ -500,7 +524,7 @@ impl TcpSocket { | |
| /// | ||
| /// #[tokio::main] | ||
| /// async fn main() -> std::io::Result<()> { | ||
| /// | ||
| /// | ||
| /// let socket2_socket = Socket::new(Domain::IPV4, Type::STREAM, None)?; | ||
| /// | ||
| /// let socket = TcpSocket::from_std_stream(socket2_socket.into()); | ||
|
Contributor
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. How about
Member
Author
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. We don't want to make a crate with version 0.x a public dependency of a crate with version 1.x. See also api-guidelines: https://rust-lang.github.io/api-guidelines/necessities.html#public-dependencies-of-a-stable-crate-are-stable-c-stable
Member
Author
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. Also, once this PR is merged, adding the options that socket2 has to the TcpSocket will be easy. So the cases where code like this example is needed should be greatly reduced in the near future. |
||
|
|
@@ -511,16 +535,12 @@ impl TcpSocket { | |
| pub fn from_std_stream(std_stream: std::net::TcpStream) -> TcpSocket { | ||
| #[cfg(unix)] | ||
| { | ||
| use std::os::unix::io::{FromRawFd, IntoRawFd}; | ||
|
|
||
| let raw_fd = std_stream.into_raw_fd(); | ||
| unsafe { TcpSocket::from_raw_fd(raw_fd) } | ||
| } | ||
|
|
||
| #[cfg(windows)] | ||
| { | ||
| use std::os::windows::io::{FromRawSocket, IntoRawSocket}; | ||
|
|
||
| let raw_socket = std_stream.into_raw_socket(); | ||
| unsafe { TcpSocket::from_raw_socket(raw_socket) } | ||
| } | ||
|
|
@@ -549,7 +569,7 @@ impl FromRawFd for TcpSocket { | |
| /// The caller is responsible for ensuring that the socket is in | ||
| /// non-blocking mode. | ||
| unsafe fn from_raw_fd(fd: RawFd) -> TcpSocket { | ||
| let inner = mio::net::TcpSocket::from_raw_fd(fd); | ||
| let inner = socket2::Socket::from_raw_fd(fd); | ||
| TcpSocket { inner } | ||
| } | ||
| } | ||
|
|
@@ -584,7 +604,7 @@ impl FromRawSocket for TcpSocket { | |
| /// The caller is responsible for ensuring that the socket is in | ||
| /// non-blocking mode. | ||
| unsafe fn from_raw_socket(socket: RawSocket) -> TcpSocket { | ||
| let inner = mio::net::TcpSocket::from_raw_socket(socket); | ||
| let inner = socket2::Socket::from_raw_socket(socket); | ||
| TcpSocket { inner } | ||
| } | ||
| } | ||
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.
I'm not too familiar with the features of socket2, but can we be more precise here?
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.
socket2's
allfeature means enabling APIs that are not available in some OSes, not means enabling all feature flags.https://github.com/rust-lang/socket2/blob/6601ed132b37d6e9d178b34918bfb0b236800232/Cargo.toml#L42-L43
In our case, this feature is needed to use reuse_port/set_reuse_port, which is not available in Solaris & Illumos.