Struct connection::Connection

source ·
pub struct Connection { /* private fields */ }
Expand description

A TCP connection that can be used to send and receive serializable values

Implementations§

source§

impl Connection

source

pub async fn dial<A: ToSocketAddrs>( addr: A ) -> Result<Connection, ConnectionError>

Connect to a socket address and return a new connection with the default buffer capacity

Examples
use connection::{Connection};
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Connect to a peer
    let mut conn = Connection::dial("127.0.0.1:8080").await?;

    // Send a message
    conn.write(&"Hello, world!").await?;

    Ok(())
}
source

pub async fn dial_with_capacity<A: ToSocketAddrs>( addr: A, capacity: usize ) -> Result<Connection, ConnectionError>

Connect to a socket address and return a new connection with a custom buffer capacity

Examples
use connection::{Connection};
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Connect to a peer
    let buffer_size = 4096;
    let mut conn = Connection::dial_with_capacity("127.0.0.1:8080", buffer_size).await?;

    // Send a message
    conn.write(&"Hello, world!").await?;

    Ok(())
}
source

pub fn new(stream: TcpStream) -> Self

Create a new connection with the default buffer capacity

Examples
use connection::{Connection};
use tokio::net::TcpStream;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Connect to a peer
    let stream = TcpStream::connect("127.0.0.1:8080").await?;
    let mut conn = Connection::new(stream);

    // Send a message
    conn.write(&"Hello, world!").await?;

    Ok(())
}
source

pub fn new_with_capacity(stream: TcpStream, capacity: usize) -> Self

Create a new connection with a custom buffer capacity

Examples
use connection::{Connection};
use tokio::net::TcpStream;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Connect to a peer
    let stream = TcpStream::connect("127.0.0.1:8080").await?;
    let buffer_size = 4096;
    let mut conn = Connection::new_with_capacity(stream, buffer_size);

    // Send a message
    conn.write(&"Hello, world!").await?;

    Ok(())
}
source

pub async fn write<T: Serialize>( &mut self, value: &T ) -> Result<(), ConnectionError>

Write a serializable value into the stream

Examples
use serde::{Serialize, Deserialize};
use connection::{Connection};
use std::error::Error;

#[derive(Serialize, Deserialize)]
struct Message { id: usize, text: String }

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Connect to a peer
    let mut conn = Connection::dial("127.0.0.1:8080").await?;

    // Create a message
    let message = Message { id: 1, text: "Hello, world!".to_string() };

    // Send a message
    conn.write::<Message>(&message).await?;

    Ok(())
}
source

pub async fn read<T: DeserializeOwned>( &mut self ) -> Result<Option<T>, ConnectionError>

Reads from the socket until a complete message is received, or an error occurs

Examples
use serde::{Serialize, Deserialize};
use connection::{Connection};
use std::error::Error;

#[derive(Serialize, Deserialize)]
struct Message { id: usize, text: String }

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Connect to a peer
    let mut conn = Connection::dial("127.0.0.1:8080").await?;

    // Read a message
    let message: Message = conn.read::<Message>().await?.unwrap();

    Ok(())
}

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.