async_datagram/lib.rs
1#![forbid(unsafe_code, future_incompatible)]
2#![deny(missing_debug_implementations, bad_style)]
3#![deny(missing_docs)]
4#![cfg_attr(test, deny(warnings))]
5
6//! Async datagram traits.
7//!
8//! ## Example
9//!
10//! ```rust
11//! use async_datagram::AsyncDatagram;
12//! use std::task::{Context, Poll};
13//! use std::pin::Pin;
14//!
15//! struct Udp;
16//!
17//! impl AsyncDatagram for Udp {
18//! type Sender = std::net::SocketAddr;
19//! type Receiver = std::net::SocketAddr;
20//! type Err = std::io::Error;
21//!
22//! fn poll_send_to(
23//! self: Pin<&mut Self>,
24//! cx: &mut Context<'_>,
25//! buf: &[u8],
26//! target: &Self::Receiver,
27//! ) -> Poll<Result<usize, Self::Err>> {
28//! Poll::Ready(Ok(0))
29//! }
30//!
31//! fn poll_recv_from(
32//! self: Pin<&mut Self>,
33//! cx: &mut Context<'_>,
34//! buf: &mut [u8],
35//! ) -> Poll<Result<(usize, Self::Sender), Self::Err>> {
36//! Poll::Pending
37//! }
38//! }
39//! ```
40
41use std::pin::Pin;
42use std::task::{Context, Poll};
43
44/// Implement a datagram protocol.
45pub trait AsyncDatagram {
46 /// Specifies which target to send the data to.
47 type Sender;
48
49 /// Specifies which target the data was received from.
50 type Receiver;
51
52 /// The type of failures yielded by this trait.
53 type Err;
54
55 /// Sends data on the IO interface to the specified target.
56 ///
57 /// On success, returns the number of bytes written.
58 fn poll_send_to(
59 self: Pin<&mut Self>,
60 cx: &mut Context<'_>,
61 buf: &[u8],
62 receiver: &Self::Receiver,
63 ) -> Poll<Result<usize, Self::Err>>;
64
65 /// Receives data from the IO interface.
66 ///
67 /// On success, returns the number of bytes read and the target from whence
68 /// the data came.
69 #[allow(clippy::type_complexity)]
70 fn poll_recv_from(
71 self: Pin<&mut Self>,
72 cx: &mut Context<'_>,
73 buf: &mut [u8],
74 ) -> Poll<Result<(usize, Self::Sender), Self::Err>>;
75}