Struct rocket::response::stream::TextStream [−][src]
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!
.
Trait Implementations
Auto Trait Implementations
impl<S> RefUnwindSafe for TextStream<S> where
S: RefUnwindSafe,
impl<S> Send for TextStream<S> where
S: Send,
impl<S> Sync for TextStream<S> where
S: Sync,
impl<S> Unpin for TextStream<S> where
S: Unpin,
impl<S> UnwindSafe for TextStream<S> where
S: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
Converts self
into a collection.
type Output = T
type Output = T
Should always be Self
pub fn vzip(self) -> V