@@ -1331,6 +1331,11 @@ impl UdpSocket {
13311331 /// Make sure to always use a sufficiently large buffer to hold the
13321332 /// maximum UDP packet size, which can be up to 65536 bytes in size.
13331333 ///
1334+ /// MacOS will return an error if you pass a zero-sized buffer.
1335+ ///
1336+ /// If you're merely interested in learning the sender of the data at the head of the queue,
1337+ /// try [`peek_sender`].
1338+ ///
13341339 /// # Examples
13351340 ///
13361341 /// ```no_run
@@ -1349,6 +1354,8 @@ impl UdpSocket {
13491354 /// Ok(())
13501355 /// }
13511356 /// ```
1357+ ///
1358+ /// [`peek_sender`]: method@Self::peek_sender
13521359 pub async fn peek_from ( & self , buf : & mut [ u8 ] ) -> io:: Result < ( usize , SocketAddr ) > {
13531360 self . io
13541361 . registration ( )
@@ -1371,6 +1378,11 @@ impl UdpSocket {
13711378 /// Make sure to always use a sufficiently large buffer to hold the
13721379 /// maximum UDP packet size, which can be up to 65536 bytes in size.
13731380 ///
1381+ /// MacOS will return an error if you pass a zero-sized buffer.
1382+ ///
1383+ /// If you're merely interested in learning the sender of the data at the head of the queue,
1384+ /// try [`poll_peek_sender`].
1385+ ///
13741386 /// # Return value
13751387 ///
13761388 /// The function returns:
@@ -1382,6 +1394,8 @@ impl UdpSocket {
13821394 /// # Errors
13831395 ///
13841396 /// This function may encounter any standard I/O error except `WouldBlock`.
1397+ ///
1398+ /// [`poll_peek_sender`]: method@Self::poll_peek_sender
13851399 pub fn poll_peek_from (
13861400 & self ,
13871401 cx : & mut Context < ' _ > ,
@@ -1404,6 +1418,50 @@ impl UdpSocket {
14041418 Poll :: Ready ( Ok ( addr) )
14051419 }
14061420
1421+ /// Retrieve the sender of the data at the head of the input queue, waiting if empty.
1422+ ///
1423+ /// This is equivalent to calling [`peek_from`] with a zero-sized buffer,
1424+ /// but suppresses the `WSAEMSGSIZE` error on Windows and the "invalid argument" error on macOS.
1425+ ///
1426+ /// [`peek_from`]: method@Self::peek_from
1427+ pub async fn peek_sender ( & self ) -> io:: Result < SocketAddr > {
1428+ self . io
1429+ . registration ( )
1430+ . async_io ( Interest :: READABLE , || self . try_peek_sender ( ) )
1431+ . await
1432+ }
1433+
1434+ /// Retrieve the sender of the data at the head of the input queue,
1435+ /// scheduling a wakeup if empty.
1436+ ///
1437+ /// This is equivalent to calling [`poll_peek_from`] with a zero-sized buffer,
1438+ /// but suppresses the `WSAEMSGSIZE` error on Windows and the "invalid argument" error on macOS.
1439+ ///
1440+ /// # Notes
1441+ ///
1442+ /// Note that on multiple calls to a `poll_*` method in the recv direction, only the
1443+ /// `Waker` from the `Context` passed to the most recent call will be scheduled to
1444+ /// receive a wakeup.
1445+ ///
1446+ /// [`poll_peek_from`]: method@Self::poll_peek_from
1447+ pub fn poll_peek_sender ( & self , cx : & mut Context < ' _ > ) -> Poll < io:: Result < SocketAddr > > {
1448+ self . io
1449+ . registration ( )
1450+ . poll_read_io ( cx, || self . try_peek_sender ( ) )
1451+ }
1452+
1453+ #[ inline]
1454+ fn try_peek_sender ( & self ) -> io:: Result < SocketAddr > {
1455+ self
1456+ . as_socket ( )
1457+ . peek_sender ( ) ?
1458+ // May be `None` if the platform doesn't populate the sender for some reason.
1459+ // In testing, that only occurred on macOS if you pass a zero-sized buffer,
1460+ // but the implementation of `Socket::peek_sender()` covers that.
1461+ . as_socket ( )
1462+ . ok_or_else ( || io:: Error :: new ( io:: ErrorKind :: Other , "sender not available" ) )
1463+ }
1464+
14071465 /// Gets the value of the `SO_BROADCAST` option for this socket.
14081466 ///
14091467 /// For more information about this option, see [`set_broadcast`].
0 commit comments