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

pub struct ByteStream<S>(pub S);
Expand description

A potentially infinite stream of bytes: any T: AsRef<[u8]>.

A ByteStream can be constructed from any Stream of items of type T where T: AsRef<[u8]>. This includes Vec<u8>, &[u8], &str, &RawStr, and more. The stream can be constructed directly, via ByteStream(..) or ByteStream::from(), or through generator syntax via ByteStream!.

Responder

ByteStream is a (potentially infinite) responder. The response Content-Type is set to Binary. The body is unsized, and values are sent as soon as they are yielded by the internal iterator.

Example

Use ByteStream! to yield 10 3-byte vectors, one every second, of the form vec![i, i + 1, i + 2] for i from 0 to 10 exclusive:

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

#[get("/bytes")]
fn bytes() -> ByteStream![&'static [u8]] {
    ByteStream(repeat(&[1, 2, 3][..]))
}

#[get("/byte/stream")]
fn stream() -> ByteStream![Vec<u8>] {
    ByteStream! {
        let mut interval = time::interval(Duration::from_secs(1));
        for i in 0..10u8 {
            yield vec![i, i + 1, i + 2];
            interval.tick().await;
        }
    }
}

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

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Creates a ByteStream from any S: Stream.

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

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 resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. 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.