UdpSocket

Struct UdpSocket 

Source
pub struct UdpSocket { /* private fields */ }
Expand description

A high-level UDP Socket that allows for writing and reading (de)serializable values

Implementations§

Source§

impl UdpSocket

Source

pub async fn bind<A: ToSocketAddrs>(addr: A) -> Result<Self, UdpSocketError>

Attempt to create a new UdpSocket by binding it to the provided address

§Example
use sockit::UdpSocket;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let socket = UdpSocket::bind("127.0.0.1:0").await?;
  Ok(())
}
Source

pub fn new(socket: UdpSocket) -> Self

Create a new UDP socket from an existing tokio::net::UdpSocket

§Example
use sockit::UdpSocket;
use tokio::net::UdpSocket as TokioUdpSocket;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let tokio_socket = TokioUdpSocket::bind("127.0.0.1:0").await?;
  let mut sockit_socket = UdpSocket::new(tokio_socket);
  Ok(())
}
Source

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

Write a serializable value to the socket

§Example
use sockit::UdpSocket;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let mut socket = UdpSocket::bind("127.0.0.1:0").await?;
  socket.write(&"Hello World!", "127.0.0.1:9090".parse()?).await?;
  Ok(())
}
Source

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

Read a deserializable value from a single datagram on the socket

This method returns an error when it isn’t possible to deserialize the value from the datagram. This can happen if the value doesn’t fit into a single datagram.

§Example
use sockit::UdpSocket;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let mut socket = UdpSocket::bind("127.0.0.1:0").await?;
  let message = socket.read::<String>().await?;
  Ok(())
}
Source

pub fn local_addr(&self) -> Result<SocketAddr, UdpSocketError>

Get the local address of the socket

§Example
use sockit::UdpSocket;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
 let socket = UdpSocket::bind("127.0.0.1:0").await?;
 let addr = socket.local_addr()?;
 Ok(())
}

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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 T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.