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

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

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

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

Performs the conversion.

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

Performs the conversion.

Should always be Self

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