tokio-udp-framed

Crates.iotokio-udp-framed
lib.rstokio-udp-framed
version0.1.0
sourcesrc
created_at2021-02-18 02:43:54.193557
updated_at2021-02-18 02:43:54.193557
descriptionBased on UdpFramed from tokio-util but with some quality of life modifications to support shared ownership of `UdpSocket` and to split Send/Recv into their own types.
homepage
repositoryhttps://github.com/leshow/tokio-udp-framed
max_upload_size
id356878
size31,595
Evan Cameron (leshow)

documentation

README

tokio-udp-framed

This started from a copy of UdpFramed from tokio-util with a few modifications that provides a somewhat different API:

  • All UdpFramed types take a Borrow<UdpSocket> so you can pass an Arc<UdpSocket> or &UdpSocket
  • There are UpdFramedRecv and UdpFramedSend types for specifically send and recv in Sink/Stream
  • Because of Borrow<UdpSocket> you can't use get_mut anymore

The main benefit can be easily explained in an example:

let a_soc = Arc::new(UdpSocket::bind("127.0.0.1:0").await?);
let b_soc = a_soc.clone();

let a_addr = a_soc.local_addr()?;
let b_addr = b_soc.local_addr()?;

let mut a = UdpFramed::new(a_soc, ByteCodec);
let mut b = UdpFramed::new(b_soc, LinesCodec::new());

let msg = b"1\r\n2\r\n3\r\n".to_vec();
a.send((&msg, b_addr)).await?;

let msg = b"4\r\n5\r\n6\r\n".to_vec();
a.send((&msg, b_addr)).await?;

assert_eq!(b.next().await.unwrap().unwrap(), ("1".to_string(), a_addr));
assert_eq!(b.next().await.unwrap().unwrap(), ("2".to_string(), a_addr));
assert_eq!(b.next().await.unwrap().unwrap(), ("3".to_string(), a_addr));

assert_eq!(b.next().await.unwrap().unwrap(), ("4".to_string(), a_addr));
assert_eq!(b.next().await.unwrap().unwrap(), ("5".to_string(), a_addr));
assert_eq!(b.next().await.unwrap().unwrap(), ("6".to_string(), a_addr));
Commit count: 11

cargo fmt