Skip to content

Commit

Permalink
net: update datagram docs on splitting (#3448)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darksonn authored Jan 20, 2021
1 parent 6f8a4d7 commit cc0911a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
18 changes: 12 additions & 6 deletions tokio/src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ cfg_net! {
/// * one to one: [`connect`](`UdpSocket::connect`) and associate with a single address, using [`send`](`UdpSocket::send`)
/// and [`recv`](`UdpSocket::recv`) to communicate only with that remote address
///
/// `UdpSocket` can also be used concurrently to `send_to` and `recv_from` in different tasks,
/// all that's required is that you `Arc<UdpSocket>` and clone a reference for each task.
/// 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`.
///
/// [`Arc`]: std::sync::Arc
///
/// # Streams
///
Expand Down Expand Up @@ -78,11 +83,12 @@ cfg_net! {
/// }
/// ```
///
/// # Example: Sending/Receiving concurrently
/// # Example: Splitting with `Arc`
///
/// Because `send_to` and `recv_from` take `&self`. It's perfectly alright to `Arc<UdpSocket>`
/// and share the references to multiple tasks, in order to send/receive concurrently. Here is
/// a similar "echo" example but that supports concurrent sending/receiving:
/// Because `send_to` and `recv_from` take `&self`. It's perfectly alright
/// to use an `Arc<UdpSocket>` and share the references to multiple tasks.
/// Here is a similar "echo" example that supports concurrent
/// sending/receiving:
///
/// ```no_run
/// use tokio::{net::UdpSocket, sync::mpsc};
Expand Down
8 changes: 8 additions & 0 deletions tokio/src/net/unix/datagram/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ cfg_net_unix! {
/// A socket can be either named (associated with a filesystem path) or
/// unnamed.
///
/// 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 `UnixDatagram` — an `Arc<UnixDatagram>`
/// is enough. This is because all of the methods take `&self` instead of
/// `&mut self`.
///
/// **Note:** named sockets are persisted even after the object is dropped
/// and the program has exited, and cannot be reconnected. It is advised
/// that you either check for and unlink the existing socket if it exists,
/// or use a temporary file that is guaranteed to not already exist.
///
/// [`Arc`]: std::sync::Arc
///
/// # Examples
/// Using named sockets, associated with a filesystem path:
/// ```
Expand Down

0 comments on commit cc0911a

Please sign in to comment.