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
Auto Trait Implementations
impl<S> RefUnwindSafe for ByteStream<S> where
S: RefUnwindSafe,
impl<S> Send for ByteStream<S> where
S: Send,
impl<S> Sync for ByteStream<S> where
S: Sync,
impl<S> Unpin for ByteStream<S> where
S: Unpin,
impl<S> UnwindSafe for ByteStream<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