Struct tor_proto::stream::DataStream
source · [−]pub struct DataStream { /* private fields */ }
Expand description
An anonymized stream over the Tor network.
For most purposes, you can think of this type as an anonymized TCP stream: it can read and write data, and get closed when it’s done.
DataStream
implements [futures::io::AsyncRead
] and
[futures::io::AsyncWrite
], so you can use it anywhere that those
traits are expected.
Examples
Connecting to an HTTP server and sending a request, using
AsyncWriteExt::write_all
:
let mut stream = tor_client.connect(("icanhazip.com", 80), None).await?;
use futures::io::AsyncWriteExt;
stream
.write_all(b"GET / HTTP/1.1\r\nHost: icanhazip.com\r\nConnection: close\r\n\r\n")
.await?;
// Flushing the stream is important; see below!
stream.flush().await?;
Reading the result, using AsyncReadExt::read_to_end
:
use futures::io::AsyncReadExt;
let mut buf = Vec::new();
stream.read_to_end(&mut buf).await?;
println!("{}", String::from_utf8_lossy(&buf));
Usage with Tokio
If the tokio
crate feature is enabled, this type also implements
tokio::io::AsyncRead
and
tokio::io::AsyncWrite
for easier integration
with code that expects those traits.
Remember to call flush
!
DataStream buffers data internally, in order to write as few cells
as possible onto the network. In order to make sure that your
data has actually been sent, you need to make sure that
[AsyncWrite::poll_flush
] runs to completion: probably via
AsyncWriteExt::flush
.
Splitting the type
This type is internally composed of a DataReader
and a DataWriter
; the
DataStream::split
method can be used to split it into those two parts, for more
convenient usage with e.g. stream combinators.
Implementations
Divide this DataStream into its constituent parts.
Trait Implementations
Attempt to read from the AsyncRead
into buf
. Read more
Auto Trait Implementations
impl !RefUnwindSafe for DataStream
impl Send for DataStream
impl !Sync for DataStream
impl Unpin for DataStream
impl !UnwindSafe for DataStream
Blanket Implementations
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
Creates an adaptor which will chain this stream with another. Read more
Tries to read some bytes directly into the given buf
in asynchronous
manner, returning a future type. Read more
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectored<'a, Self> where
Self: Unpin,
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectored<'a, Self> where
Self: Unpin,
Creates a future which will read from the AsyncRead
into bufs
using vectored
IO operations. Read more
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> where
Self: Unpin,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> where
Self: Unpin,
Creates a future which will read exactly enough bytes to fill buf
,
returning an error if end of file (EOF) is hit sooner. Read more
fn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEnd<'a, Self> where
Self: Unpin,
fn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEnd<'a, Self> where
Self: Unpin,
Creates a future which will read all the bytes from this AsyncRead
. Read more
fn read_to_string(&'a mut self, buf: &'a mut String) -> ReadToString<'a, Self> where
Self: Unpin,
fn read_to_string(&'a mut self, buf: &'a mut String) -> ReadToString<'a, Self> where
Self: Unpin,
Creates a future which will read all the bytes from this AsyncRead
. Read more
Helper method for splitting this read/write object into two halves. Read more
impl<W> AsyncWriteExt for W where
W: AsyncWrite + ?Sized,
impl<W> AsyncWriteExt for W where
W: AsyncWrite + ?Sized,
Creates a future which will entirely flush this AsyncWrite
. Read more
Creates a future which will entirely close this AsyncWrite
.
Creates a future which will write bytes from buf
into the object. Read more
fn write_vectored(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectored<'a, Self> where
Self: Unpin,
fn write_vectored(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectored<'a, Self> where
Self: Unpin,
Creates a future which will write bytes from bufs
into the object using vectored
IO operations. Read more
Write data into this object. Read more
Mutably borrows from an owned value. Read more
pub fn vzip(self) -> V
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more