#traits #offers #message #async-trait #async-std #zero-cost #obj-comms

qcomms

qcomms is a small library that offers a simple, zero-cost message passing trait. no async-trait

5 releases (2 stable)

1.0.1 Jul 31, 2021
0.3.0 May 19, 2021
0.2.0 May 17, 2021
0.1.0 Apr 19, 2021

#1821 in Rust patterns

MPL-2.0 license

14KB
327 lines

QCOMMS

qcomms is a small library that offers a simple message passing trait. it also offers keepalive and other stream helpers.

use qcomms::ObjComms;
use serde::{Serialize, Deserialize};
use async_std::task;
use async_std::task::sleep;
use async_std::net::{TcpListener, TcpStream};

#[derive(Serialize, Deserialize, Debug)]
pub struct Message {
    hello: String,
    val: u32,
}

#[async_std::main]
async fn main() {
    task::spawn(async move {
        let listener = TcpListener::bind("127.0.0.1:3022").await.unwrap();
        let (mut stream, _) = listener.accept().unwrap();
        let message: Message = stream.rx().await.unwrap();
        println!("{:?}", message);
    });

    let m = Message {
        hello: "hello".to_string(),
        val: 12,
    };

    task::sleep(Duration::from_secs(1)).await;
    let mut stream = TcpStream::connect("127.0.0.1:3022").await.unwrap();
    stream.tx(&m).await.unwrap();
}

lib.rs:

qcomms is only compatible with async std. this decision was made so users aren't locked in the tokio ecosystem

Dependencies

~5–14MB
~189K SLoC