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
Attempt to read from the AsyncRead into bufs using vectored IO operations. Read more
Attempts to read from the AsyncRead into buf. Read more
Attempt to write bytes from buf into the object. Read more
Attempt to flush the object, ensuring that any buffered data reach their destination. Read more
Attempt to close the object. Read more
Attempt to write bytes from bufs into the object using vectored IO operations. Read more
Attempt to write bytes from buf into the object. Read more
Attempts to flush the object, ensuring that any buffered data reach their destination. Read more
Initiates or attempts to shut down this writer, returning success when the I/O connection has completely shut down. Read more
Like poll_write, except that it writes from a slice of buffers. Read more
Determines if this writer has an efficient poll_write_vectored implementation. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
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
Creates a future which will read from the AsyncRead into bufs using vectored IO operations. Read more
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
Creates a future which will read all the bytes from this AsyncRead. Read more
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
Creates an AsyncRead adapter which will read at most limit bytes from the underlying reader. Read more
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
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
Available on crate feature sink only.
Allow using an [AsyncWrite] as a Sink<Item: AsRef<[u8]>>. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Wraps self with a compatibility layer that implements tokio_io::AsyncRead. Read more
Wraps self with a compatibility layer that implements tokio::io::AsyncWrite. Read more
Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
Wraps self with a compatibility layer that implements futures_io::AsyncRead. Read more
Wraps self with a compatibility layer that implements futures_io::AsyncWrite. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
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