connection

Crates.ioconnection
lib.rsconnection
version0.2.5
sourcesrc
created_at2023-03-26 23:24:36.92342
updated_at2023-03-31 06:41:36.125264
descriptionA TCP connection that can read and write serializable data
homepage
repository
max_upload_size
id821560
size13,721
Will Cygan (wcygan)

documentation

README

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();
}
Commit count: 0

cargo fmt