pub struct Connection { /* private fields */ }
Expand description
A TCP connection that can be used to send and receive serializable values
Implementations§
Source§impl Connection
impl Connection
Sourcepub async fn dial<A: ToSocketAddrs>(
addr: A,
) -> Result<Connection, ConnectionError>
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(())
}
Sourcepub async fn dial_with_capacity<A: ToSocketAddrs>(
addr: A,
capacity: usize,
) -> Result<Connection, ConnectionError>
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(())
}
Sourcepub fn new(stream: TcpStream) -> Self
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(())
}
Sourcepub fn new_with_capacity(stream: TcpStream, capacity: usize) -> Self
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(())
}
Sourcepub async fn write<T: Serialize>(
&mut self,
value: &T,
) -> Result<(), ConnectionError>
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(())
}
Sourcepub async fn read<T: DeserializeOwned>(
&mut self,
) -> Result<Option<T>, ConnectionError>
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§
impl !Freeze for Connection
impl RefUnwindSafe for Connection
impl Send for Connection
impl Sync for Connection
impl Unpin for Connection
impl UnwindSafe for Connection
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more