Struct rocket::response::stream::ReaderStream[][src]

pub struct ReaderStream<S: Stream> { /* fields omitted */ }
Expand description

An async reader that reads from a stream of async readers.

A ReaderStream can be constructed from any Stream of items of type T where T: AsyncRead, or from a single AsyncRead type using ReaderStream::one(). The AsyncRead implementation of ReaderStream progresses the stream forward, returning the contents of the inner readers. Thus, a ReaderStream can be thought of as a flattening of async readers.

ReaderStream is designed to be used as a building-block for stream-based responders by acting as the streamed_body of a Response, though it may also be used as a responder itself.

use std::io::Cursor;

use rocket::{Request, Response};
use rocket::futures::stream::{Stream, StreamExt};
use rocket::response::{self, Responder, stream::ReaderStream};
use rocket::http::ContentType;

struct MyStream<S>(S);

impl<'r, S: Stream<Item = String>> Responder<'r, 'r> for MyStream<S>
    where S: Send + 'r
{
    fn respond_to(self, _: &'r Request<'_>) -> response::Result<'r> {
        Response::build()
            .header(ContentType::Text)
            .streamed_body(ReaderStream::from(self.0.map(Cursor::new)))
            .ok()
    }
}

Responder

ReaderStream is a (potentially infinite) responder. No Content-Type is set. The body is unsized, and values are sent as soon as they are yielded by the internal stream.

Example

use rocket::response::stream::ReaderStream;
use rocket::futures::stream::{repeat, StreamExt};
use rocket::tokio::time::{self, Duration};
use rocket::tokio::fs::File;

// Stream the contents of `safe/path` followed by `another/safe/path`.
#[get("/reader/stream")]
fn stream() -> ReaderStream![File] {
    ReaderStream! {
        let paths = &["safe/path", "another/safe/path"];
        for path in paths {
            if let Ok(file) = File::open(path).await {
                yield file;
            }
        }
    }
}

// Stream the contents of the file `safe/path`. This is identical to
// returning `File` directly; Rocket responders stream and never buffer.
#[get("/reader/stream/one")]
async fn stream_one() -> std::io::Result<ReaderStream![File]> {
    let file = File::open("safe/path").await?;
    Ok(ReaderStream::one(file))
}

The syntax of ReaderStream! as an expression is identical to that of stream!.

Implementations

Create a ReaderStream that yields exactly one reader, streaming the contents of the reader itself.

Example

Stream the bytes from a remote TCP connection:

use std::io;
use std::net::SocketAddr;

use rocket::tokio::net::TcpStream;
use rocket::response::stream::ReaderStream;

#[get("/stream")]
async fn stream() -> io::Result<ReaderStream![TcpStream]> {
    let addr = SocketAddr::from(([127, 0, 0, 1], 9999));
    let stream = TcpStream::connect(addr).await?;
    Ok(ReaderStream::one(stream))
}

Trait Implementations

Attempts to read from the AsyncRead into buf. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

Returns Ok if a Response could be generated successfully. Otherwise, returns an Err with a failing Status. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Creates a new AsyncRead instance that chains this stream with next. Read more

Pulls some bytes from this source into the specified buffer, returning how many bytes were read. Read more

Pulls some bytes from this source into the specified buffer, advancing the buffer’s internal cursor. Read more

Reads the exact number of bytes required to fill buf. Read more

Reads an unsigned 8 bit integer from the underlying reader. Read more

Reads a signed 8 bit integer from the underlying reader. Read more

Reads an unsigned 16-bit integer in big-endian order from the underlying reader. Read more

Reads a signed 16-bit integer in big-endian order from the underlying reader. Read more

Reads an unsigned 32-bit integer in big-endian order from the underlying reader. Read more

Reads a signed 32-bit integer in big-endian order from the underlying reader. Read more

Reads an unsigned 64-bit integer in big-endian order from the underlying reader. Read more

Reads an signed 64-bit integer in big-endian order from the underlying reader. Read more

Reads an unsigned 128-bit integer in big-endian order from the underlying reader. Read more

Reads an signed 128-bit integer in big-endian order from the underlying reader. Read more

Reads an unsigned 16-bit integer in little-endian order from the underlying reader. Read more

Reads a signed 16-bit integer in little-endian order from the underlying reader. Read more

Reads an unsigned 32-bit integer in little-endian order from the underlying reader. Read more

Reads a signed 32-bit integer in little-endian order from the underlying reader. Read more

Reads an unsigned 64-bit integer in little-endian order from the underlying reader. Read more

Reads an signed 64-bit integer in little-endian order from the underlying reader. Read more

Reads an unsigned 128-bit integer in little-endian order from the underlying reader. Read more

Reads an signed 128-bit integer in little-endian order from the underlying reader. Read more

Reads all bytes until EOF in this source, placing them into buf. Read more

Reads all bytes until EOF in this source, appending them to buf. Read more

Creates an adaptor which reads at most limit bytes from it. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

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.

Converts self into a collection.

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.