1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Networking

use core::{
    pin::Pin,
    task::{Context, Poll},
};

mod addr;
mod ip;
mod parser;
#[cfg(feature = "std")]
mod std_impl;

pub use self::addr::{GetSocketAddrs, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
pub use self::ip::{IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope};
pub use self::parser::AddrParseError;
#[cfg(feature = "std")]
pub use self::std_impl::*;

/// This is intended for async datagram IO.
pub trait AsyncSendTo {
    /// The associated error type.
    type Error;

    /// A non-blocking, poll-based variant of [`std::net::UdpSocket::send_to`].
    ///
    /// This doesn't _have_ to be implemented as async. The implementation
    /// for [`std::net::UdpSocket`] is blocking.
    fn poll_send_to(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
        addr: SocketAddr,
    ) -> Poll<Result<usize, Self::Error>>;
}

/// This is intended for async datagram IO.
pub trait AsyncRecvFrom {
    /// The associated error type.
    type Error;

    /// A non-blocking, poll-based varient of [`std::net::UdpSocket::recv_from`].
    ///
    /// This doesn't _have_ to be implemented as async. The implementation
    /// for [`std::net::UdpSocket`] is blocking.
    fn poll_recv_from(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<Result<(usize, SocketAddr), Self::Error>>;
}

pub trait MulticastSocket {
    /// The associated error type.
    type Error;

    /// Attempts to join the given multicast group.
    fn join_multicast(&self, addr: IpAddr) -> Result<(), Self::Error>;

    /// Attempts to leave the given multicast group.
    fn leave_multicast(&self, addr: IpAddr) -> Result<(), Self::Error>;
}