Skip to content

Commit

Permalink
require &mut self in AsyncUdpSocket methods (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
carver authored Aug 8, 2023
1 parent 5c2fe67 commit e783076
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<P> UtpSocket<P>
where
P: ConnectionPeer + 'static,
{
pub fn with_socket<S>(socket: S) -> Self
pub fn with_socket<S>(mut socket: S) -> Self
where
S: AsyncUdpSocket<P> + 'static,
{
Expand All @@ -65,7 +65,6 @@ where
socket_events: socket_event_tx.clone(),
};

let socket = Arc::new(socket);
tokio::spawn(async move {
let mut buf = [0; MAX_UDP_PAYLOAD_SIZE];
loop {
Expand Down
13 changes: 7 additions & 6 deletions src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ use crate::cid::ConnectionPeer;
#[async_trait]
pub trait AsyncUdpSocket<P: ConnectionPeer>: Send + Sync {
/// Attempts to send data on the socket to a given address.
async fn send_to(&self, buf: &[u8], target: &P) -> io::Result<usize>;
/// Note that this should return nearly immediately, rather than awaiting something internally.
async fn send_to(&mut self, buf: &[u8], target: &P) -> io::Result<usize>;
/// Attempts to receive a single datagram on the socket.
async fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, P)>;
async fn recv_from(&mut self, buf: &mut [u8]) -> io::Result<(usize, P)>;
}

#[async_trait]
impl AsyncUdpSocket<SocketAddr> for UdpSocket {
async fn send_to(&self, buf: &[u8], target: &SocketAddr) -> io::Result<usize> {
self.send_to(buf, *target).await
async fn send_to(&mut self, buf: &[u8], target: &SocketAddr) -> io::Result<usize> {
UdpSocket::send_to(self, buf, target).await
}

async fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
self.recv_from(buf).await
async fn recv_from(&mut self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
UdpSocket::recv_from(self, buf).await
}
}

0 comments on commit e783076

Please sign in to comment.