use crate::Result;
use alloc::boxed::Box;
use core::net::SocketAddr;
#[async_trait::async_trait]
pub trait UdpSocket: Send + Sync + 'static {
async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>;
async fn send_to(&self, buf: &[u8], addr: SocketAddr) -> Result<usize>;
fn local_addr(&self) -> Result<SocketAddr>;
}
#[cfg(feature = "platform-tokio")]
pub mod tokio_impl {
use super::UdpSocket;
use crate::Result;
use core::net::SocketAddr;
#[async_trait::async_trait]
impl UdpSocket for tokio::net::UdpSocket {
async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)> {
Ok(tokio::net::UdpSocket::recv_from(self, buf).await?)
}
async fn send_to(&self, buf: &[u8], addr: SocketAddr) -> Result<usize> {
Ok(tokio::net::UdpSocket::send_to(self, buf, addr).await?)
}
fn local_addr(&self) -> Result<SocketAddr> {
Ok(tokio::net::UdpSocket::local_addr(self)?)
}
}
}