pub struct TcpStream { /* private fields */ }
Implementations§
Source§impl TcpStream
impl TcpStream
Sourcepub fn new(std_stream: TcpStream) -> Result<Self, Error>
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
.
Sourcepub async fn connect<A: ToSocketAddrs + Send + 'static>(
addr: A,
) -> Result<Self, Error>
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
.
pub fn inner(&self) -> &TcpStream
pub fn inner_mut(&mut self) -> &mut TcpStream
pub fn into_inner(self) -> TcpStream
Sourcepub async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
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
.
Sourcepub async fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
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
.
Sourcepub async fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
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.
Sourcepub async fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
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
.
Sourcepub async fn read_vectored(
&mut self,
bufs: &mut [IoSliceMut<'_>],
) -> Result<usize, Error>
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
.
Sourcepub async fn peek(&mut self, buf: &mut [u8]) -> Result<usize, Error>
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
.
Sourcepub async fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
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
.
Sourcepub async fn flush(&mut self) -> Result<(), Error>
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.
Sourcepub async fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
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
.
Sourcepub async fn write_vectored(
&mut self,
bufs: &[IoSlice<'_>],
) -> Result<usize, Error>
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
.