diff options
author | Kevin Leimkuhler <kevin@kleimkuhler.com> | 2020-07-28 17:09:56 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-28 17:09:56 -0700 |
commit | 1562bb314482215eb7517e6b8b8bdecbacf10e79 (patch) | |
tree | d0e476d5f88d94265e1a1f7c434f0511282abf1b /tokio/src/net | |
parent | 0366a3e6d1aa4e7bf4a1c717680dd0947589264b (diff) |
add: Add `UdpSocket::{try_send,try_send_to}` methods (#1979)
Diffstat (limited to 'tokio/src/net')
-rw-r--r-- | tokio/src/net/udp/socket.rs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/tokio/src/net/udp/socket.rs b/tokio/src/net/udp/socket.rs index 97090a20..16e53773 100644 --- a/tokio/src/net/udp/socket.rs +++ b/tokio/src/net/udp/socket.rs @@ -116,6 +116,20 @@ impl UdpSocket { poll_fn(|cx| self.poll_send(cx, buf)).await } + /// Try to send data on the socket to the remote address to which it is + /// connected. + /// + /// # Returns + /// + /// If successfull, the number of bytes sent is returned. Users + /// should ensure that when the remote cannot receive, the + /// [`ErrorKind::WouldBlock`] is properly handled. + /// + /// [`ErrorKind::WouldBlock`]: std::io::error::ErrorKind::WouldBlock + pub fn try_send(&self, buf: &[u8]) -> io::Result<usize> { + self.io.get_ref().send(buf) + } + // Poll IO functions that takes `&self` are provided for the split API. // // They are not public because (taken from the doc of `PollEvented`): @@ -185,6 +199,21 @@ impl UdpSocket { } } + /// Try to send data on the socket to the given address. + /// + /// # Returns + /// + /// If successfull, the future resolves to the number of bytes sent. + /// + /// Users should ensure that when the remote cannot receive, the + /// [`ErrorKind::WouldBlock`] is properly handled. An error can also occur + /// if the IP version of the socket does not match that of `target`. + /// + /// [`ErrorKind::WouldBlock`]: std::io::error::ErrorKind::WouldBlock + pub fn try_send_to(&self, buf: &[u8], target: SocketAddr) -> io::Result<usize> { + self.io.get_ref().send_to(buf, &target) + } + // TODO: Public or not? #[doc(hidden)] pub fn poll_send_to( |