Struct TcpStream

Source
pub struct TcpStream { /* private fields */ }

Implementations§

Source§

impl TcpStream

Source

pub fn new(std_stream: TcpStream) -> Result<Self, Error>

Wraps std_stream so we can perform async operations on it.

§Errors

Returns any Err(std::io::Error) returned by std::net::TcpStream::set_nonblocking.

Source

pub async fn connect<A: ToSocketAddrs + Send + 'static>( addr: A, ) -> Result<Self, Error>

Opens a TCP connection to addr.

Uses safina_executor::schedule_blocking to perform the blocking connect call. Panics if the caller is not running on an Executor.

§Errors

Returns any Err(std::io::Error) returned by std::net::TcpStream::connect.

Source

pub fn inner(&self) -> &TcpStream

Source

pub fn inner_mut(&mut self) -> &mut TcpStream

Source

pub fn into_inner(self) -> TcpStream

Source

pub async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>

Reads some bytes from the socket and places them in buf. Returns the number of bytes read.

This is an async version of std::io::Read::read.

§Errors

Returns any Err(std::io::Error) returned by std::net::TcpStream::read.

Source

pub async fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes until the socket is shutdown for reading. Appends the bytes to buf.

This is an async version of std::io::Read::read_to_end.

§Errors

Returns any Err(std::io::Error) returned by std::net::TcpStream::read.

Source

pub async fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until the socket is shutdown for reading. Interprets the bytes as a single UTF-8 string and appends it to buf.

This is an async version of std::io::Read::read_to_string.

§Errors

Returns any Err(std::io::Error) returned by std::net::TcpStream::read.

Returns ErrorKind::InvalidData if the bytes are not valid UTF-8.

Source

pub async fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf.

This is an async version of std::io::Read::read_exact.

§Errors

Returns any Err(std::io::Error) returned by std::net::TcpStream::read.

Source

pub async fn read_vectored( &mut self, bufs: &mut [IoSliceMut<'_>], ) -> Result<usize, Error>

Reads bytes into bufs, filling each buffer in order. The final buffer written to may be partially filled.

Returns the total number of bytes read.

This is an async version of std::net::TcpStream::read_vectored.

§Errors

Returns any Err(std::io::Error) returned by std::net::TcpStream::read_vectored.

Source

pub async fn peek(&mut self, buf: &mut [u8]) -> Result<usize, Error>

Waits to receive some data on the socket, then copies it into buf.

Returns the number of bytes copied.

Repeated calls return the same data. Call read to remove data from the socket.

This is an async version of std::net::TcpStream::peek.

§Errors

Returns any Err(std::io::Error) returned by std::net::TcpStream::peek.

Source

pub async fn write(&mut self, buf: &[u8]) -> Result<usize, Error>

Writes the bytes in buf to the socket. Returns the number of bytes written.

§Errors

Returns any Err(std::io::Error) returned by std::net::TcpStream::write.

Source

pub async fn flush(&mut self) -> Result<(), Error>

Sends all buffered data that was previously written on this socket and waits for receipt confirmation by the remote machine.

§Errors

Returns std::io::Error if the connection failed or the remote machine did not respond within a timeout period.

Source

pub async fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Writes all bytes in buf to the socket.

§Errors

Returns any Err(std::io::Error) returned by std::net::TcpStream::write.

Source

pub async fn write_vectored( &mut self, bufs: &[IoSlice<'_>], ) -> Result<usize, Error>

Writes data from a slice of buffers.

Takes data from each buffer in order. May partially read the last buffer read.

Returns the number of bytes written.

§Errors

Returns any Err(std::io::Error) returned by std::net::TcpStream::write_vectored.

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.