6 releases

0.2.5 Mar 31, 2023
0.2.4 Mar 30, 2023
0.1.0 Mar 26, 2023

#1709 in Asynchronous

MIT license

14KB
133 lines

Connection 💌

github crates.io docs.rs build status codecov

A TCP-based connection that can send & receive serializable objects.

Usage

Add this to your Cargo.toml:

[dependencies]
connection = "0.2.5"

You can create a Connection by connecting like so:

use connection::Connection;

#[tokio::main]
async fn main() {
  let mut conn = Connection::connect("127.0.0.1:8080").await.unwrap();
}

You can use the Connection to send and receive serializable objects:

use connection::Connection;
use serde::{Serialize, Deserialize};

/// A (de)serializable type shared between client and server
#[derive(Serialize, Deserialize)]
struct Message {
  id: u32,
  data: String,
}

/// Code running client side
async fn client_side(mut client_conn: Connection) {
  let message = Message {
    id: 1,
    data: "Hello, world!".to_string(),
  };

  client_conn.write::<Message>(&message).await.unwrap();
}

/// Code running server side
async fn server_side(mut server_conn: Connection) {
  let message: Message = server_conn.read::<Message>().await.unwrap().unwrap();
}

Dependencies

~3–12MB
~126K SLoC