Skip to main content

TcpStream

Struct TcpStream 

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

Native async TCP stream with statistics tracking

Implementations§

Source§

impl TcpStream

Source

pub async fn connect(addr: &str) -> Result<TcpStream, Error>

Connect to a remote address asynchronously.

addr is a literal socket address or host:port; hostnames resolve off the polling thread (see resolve). Each resolved address is tried in order and the first successful connection is returned. Neither resolution nor the TCP handshake blocks the polling thread, so wrapping this future in crate::timeout() or dropping it bounds the wait.

§Errors

Returns the resolution error, or the connect error of the last address tried when none accepts.

Source

pub fn from_std(stream: TcpStream) -> Result<TcpStream, Error>

Wrap an existing TCP stream in the Moirai TCP facade.

Source

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

Read data from the stream

Source

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

Write data to the stream

Source

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

Flush the stream

Source

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

Shutdown the write side of the stream.

Source

pub fn peer_addr(&self) -> Result<SocketAddr, Error>

Get the peer address

Source

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

Get the local address

Source

pub fn set_nodelay(&self, on: bool) -> Result<(), Error>

Configure TCP_NODELAY on the stream.

Trait Implementations§

Source§

impl AsyncRead for TcpStream

Source§

fn poll_read( self: Pin<&mut TcpStream>, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<Result<usize, Error>>

Attempt to read from the source into the provided buffer.
Source§

impl AsyncWrite for TcpStream

Source§

fn poll_write( self: Pin<&mut TcpStream>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize, Error>>

Attempt to write bytes from the buffer to the destination.
Source§

fn poll_flush( self: Pin<&mut TcpStream>, cx: &mut Context<'_>, ) -> Poll<Result<(), Error>>

Attempt to flush pending writes.
Source§

fn poll_shutdown( self: Pin<&mut TcpStream>, _cx: &mut Context<'_>, ) -> Poll<Result<(), Error>>

Attempt to close/shutdown the write side of the stream.
Source§

fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>], ) -> Poll<Result<usize, Error>>

Attempt to write bytes from several buffers in one operation. Read more
Source§

fn is_write_vectored(&self) -> bool

Whether poll_write_vectored is cheaper than one poll_write per buffer.
Source§

impl Drop for TcpStream

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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<R> AsyncReadExt for R
where R: AsyncRead + ?Sized,

Source§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> ⓘ
where Self: Unpin,

Read some bytes asynchronously from the reader.
Source§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> ⓘ
where Self: Unpin,

Read exactly enough bytes to fill the provided buffer.
Source§

impl<W> AsyncWriteExt for W
where W: AsyncWrite + ?Sized,

Source§

fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self> ⓘ
where Self: Unpin,

Write some bytes asynchronously.
Source§

fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAll<'a, Self> ⓘ
where Self: Unpin,

Write all bytes asynchronously.
Source§

fn flush(&mut self) -> Flush<'_, Self> ⓘ
where Self: Unpin,

Flush pending writes asynchronously.
Source§

fn shutdown(&mut self) -> Shutdown<'_, Self> ⓘ
where Self: Unpin,

Shutdown the write side of the stream.
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.