From 6dd7b4f2a653a84450b50d2beab01f3f0d23c4ca Mon Sep 17 00:00:00 2001 From: jean-airoldie <25088801+jean-airoldie@users.noreply.github.com> Date: Sun, 22 Dec 2019 23:22:45 -0500 Subject: [PATCH] Added try_send{,_to} & try_recv{,from} for split UnixDatagram --- tokio/src/net/unix/split.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/tokio/src/net/unix/split.rs b/tokio/src/net/unix/split.rs index 2b9cc4dcabf..b6d27e73a0c 100644 --- a/tokio/src/net/unix/split.rs +++ b/tokio/src/net/unix/split.rs @@ -19,9 +19,9 @@ //! This split method has no overhead and enforces all invariants at the type //! level. +use crate::future::poll_fn; use crate::io::{AsyncRead, AsyncWrite}; use crate::net::{UnixDatagram, UnixStream}; -use crate::future::poll_fn; use std::io; use std::mem::MaybeUninit; @@ -105,10 +105,20 @@ impl RecvHalf<'_> { poll_fn(|cx| self.0.poll_recv_priv(cx, buf)).await } + /// Try to receive a datagram from the peer without waiting. + pub fn try_recv(&mut self, buf: &mut [u8]) -> io::Result { + self.0.try_recv(buf) + } + /// Receives a datagram with the source address from the socket. pub async fn recv_from(&mut self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> { poll_fn(|cx| self.0.poll_recv_from_priv(cx, buf)).await } + + /// Try to receive data from the socket without waiting. + pub fn try_recv_from(&mut self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> { + self.0.try_recv_from(buf) + } } impl SendHalf<'_> { @@ -117,6 +127,11 @@ impl SendHalf<'_> { poll_fn(|cx| self.0.poll_send_priv(cx, buf)).await } + /// Try to send a datagram to the peer without waiting. + pub fn try_send(&mut self, buf: &[u8]) -> io::Result { + self.0.try_send(buf) + } + /// Sends a datagram to the specified address. pub async fn send_to

(&mut self, buf: &[u8], target: P) -> io::Result where @@ -124,5 +139,12 @@ impl SendHalf<'_> { { poll_fn(|cx| self.0.poll_send_to_priv(cx, buf, target.as_ref())).await } -} + /// Try to send a datagram to the peer without waiting. + pub fn try_send_to

(&mut self, buf: &[u8], target: P) -> io::Result + where + P: AsRef, + { + self.0.try_send_to(buf, target) + } +}