logo
pub struct TextStream<S>(pub S);
Expand description

A potentially infinite stream of text: T: AsRef<str>.

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

Responder

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

Example

Use TextStream! to yield 10 strings, one every second, of the form "n: $k" for $k from 0 to 10 exclusive:

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

#[get("/text")]
fn text() -> TextStream![&'static str] {
    TextStream(repeat("hi"))
}

#[get("/text/stream")]
fn stream() -> TextStream![String] {
    TextStream! {
        let mut interval = time::interval(Duration::from_secs(1));
        for i in 0..10 {
            yield format!("n: {}", i);
            interval.tick().await;
        }
    }
}

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

Tuple Fields

0: S

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 TextStream 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

Converts to this type from the input type.

Returns the argument unchanged.

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.

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)

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.

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