Skip to content

Commit

Permalink
Added try_send{,_to} & try_recv{,_from} for split UnixDatagram
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-airoldie committed Dec 23, 2019
1 parent 9416160 commit ec880e6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
31 changes: 31 additions & 0 deletions tokio/src/net/unix/datagram.rs
Expand Up @@ -103,6 +103,13 @@ impl UnixDatagram {
self.io.get_ref().send(buf)
}

// Poll IO functions that takes `&self` are provided for the split API.
//
// See `poll_send_priv` for more info.
pub(crate) fn try_send_priv(&self, buf: &[u8]) -> io::Result<usize> {
self.io.get_ref().send(buf)
}

/// Try to send a datagram to the peer without waiting.
///
/// ```
Expand Down Expand Up @@ -143,6 +150,16 @@ impl UnixDatagram {
self.io.get_ref().send_to(buf, target)
}

// Poll IO functions that takes `&self` are provided for the split API.
//
// See `poll_send_priv` for more info.
pub(crate) fn try_send_to_priv<P>(&self, buf: &[u8], target: P) -> io::Result<usize>
where
P: AsRef<Path>
{
self.io.get_ref().send_to(buf, target)
}

// Poll IO functions that takes `&self` are provided for the split API.
//
// They are not public because (taken from the doc of `PollEvented`):
Expand Down Expand Up @@ -179,6 +196,13 @@ impl UnixDatagram {
self.io.get_ref().recv(buf)
}

// Poll IO functions that takes `&self` are provided for the split API.
//
// See `poll_send_priv` for more info.
pub(crate) fn try_recv_priv(&self, buf: &mut [u8]) -> io::Result<usize> {
self.io.get_ref().recv(buf)
}

pub(crate) fn poll_recv_priv(
&self,
cx: &mut Context<'_>,
Expand Down Expand Up @@ -230,6 +254,13 @@ impl UnixDatagram {
self.io.get_ref().recv_from(buf)
}

// Poll IO functions that takes `&self` are provided for the split API.
//
// See `poll_send_priv` for more info.
pub(crate) fn try_recv_from_priv(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
self.io.get_ref().recv_from(buf)
}

pub(crate) fn poll_recv_from_priv(
&self,
cx: &mut Context<'_>,
Expand Down
24 changes: 23 additions & 1 deletion tokio/src/net/unix/split.rs
Expand Up @@ -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<usize> {
self.0.try_recv_priv(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_priv(buf)
}
}

impl SendHalf<'_> {
Expand All @@ -117,12 +127,24 @@ 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<usize> {
self.0.try_send_priv(buf)
}

/// Sends a datagram to the specified address.
pub async fn send_to<P>(&mut self, buf: &[u8], target: P) -> io::Result<usize>
where
P: AsRef<Path> + Unpin,
{
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<P>(&mut self, buf: &[u8], target: P) -> io::Result<usize>
where
P: AsRef<Path>,
{
self.0.try_send_to_priv(buf, target)
}
}

0 comments on commit ec880e6

Please sign in to comment.