aboutsummaryrefslogtreecommitdiff
path: root/src/net/udp.rs
diff options
context:
space:
mode:
authorJeff Vander Stoep <jeffv@google.com>2021-04-21 15:58:31 +0200
committerJeff Vander Stoep <jeffv@google.com>2021-04-21 15:59:26 +0200
commit28f5548b7bc0734d62d4624bb87b2bc60c78d49a (patch)
treec39809fba43deb9ee717b07b2afaeb71ef36ca59 /src/net/udp.rs
parent714f4488dbef671e82a443b31cc99ea7216dcebd (diff)
downloadtokio-28f5548b7bc0734d62d4624bb87b2bc60c78d49a.tar.gz
Update to 1.50.0
Test: atest Change-Id: I94e6acadea178b0b957fbf853a590f155d1bd973
Diffstat (limited to 'src/net/udp.rs')
-rw-r--r--src/net/udp.rs52
1 files changed, 48 insertions, 4 deletions
diff --git a/src/net/udp.rs b/src/net/udp.rs
index 86b4fe9..6e63355 100644
--- a/src/net/udp.rs
+++ b/src/net/udp.rs
@@ -23,10 +23,12 @@ cfg_net! {
/// and [`recv`](`UdpSocket::recv`) to communicate only with that remote address
///
/// This type does not provide a `split` method, because this functionality
- /// can be achieved by wrapping the socket in an [`Arc`]. Note that you do
- /// not need a `Mutex` to share the `UdpSocket` — an `Arc<UdpSocket>` is
- /// enough. This is because all of the methods take `&self` instead of `&mut
- /// self`.
+ /// can be achieved by instead wrapping the socket in an [`Arc`]. Note that
+ /// you do not need a `Mutex` to share the `UdpSocket` — an `Arc<UdpSocket>`
+ /// is enough. This is because all of the methods take `&self` instead of
+ /// `&mut self`. Once you have wrapped it in an `Arc`, you can call
+ /// `.clone()` on the `Arc<UdpSocket>` to get multiple shared handles to the
+ /// same socket. An example of such usage can be found further down.
///
/// [`Arc`]: std::sync::Arc
///
@@ -209,6 +211,48 @@ impl UdpSocket {
UdpSocket::new(io)
}
+ /// Turn a [`tokio::net::UdpSocket`] into a [`std::net::UdpSocket`].
+ ///
+ /// The returned [`std::net::UdpSocket`] will have nonblocking mode set as
+ /// `true`. Use [`set_nonblocking`] to change the blocking mode if needed.
+ ///
+ /// # Examples
+ ///
+ /// ```rust,no_run
+ /// use std::error::Error;
+ ///
+ /// #[tokio::main]
+ /// async fn main() -> Result<(), Box<dyn Error>> {
+ /// let tokio_socket = tokio::net::UdpSocket::bind("127.0.0.1:0").await?;
+ /// let std_socket = tokio_socket.into_std()?;
+ /// std_socket.set_nonblocking(false)?;
+ /// Ok(())
+ /// }
+ /// ```
+ ///
+ /// [`tokio::net::UdpSocket`]: UdpSocket
+ /// [`std::net::UdpSocket`]: std::net::UdpSocket
+ /// [`set_nonblocking`]: fn@std::net::UdpSocket::set_nonblocking
+ pub fn into_std(self) -> io::Result<std::net::UdpSocket> {
+ #[cfg(unix)]
+ {
+ use std::os::unix::io::{FromRawFd, IntoRawFd};
+ self.io
+ .into_inner()
+ .map(|io| io.into_raw_fd())
+ .map(|raw_fd| unsafe { std::net::UdpSocket::from_raw_fd(raw_fd) })
+ }
+
+ #[cfg(windows)]
+ {
+ use std::os::windows::io::{FromRawSocket, IntoRawSocket};
+ self.io
+ .into_inner()
+ .map(|io| io.into_raw_socket())
+ .map(|raw_socket| unsafe { std::net::UdpSocket::from_raw_socket(raw_socket) })
+ }
+ }
+
/// Returns the local address that this socket is bound to.
///
/// # Example