Struct rocket::response::stream::ReaderStream
source · pub struct ReaderStream<S: Stream> { /* private fields */ }
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§
source§impl<R: Unpin> ReaderStream<One<R>>
impl<R: Unpin> ReaderStream<One<R>>
sourcepub fn one(reader: R) -> Self
pub fn one(reader: R) -> Self
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§
source§impl<S: Stream> From<S> for ReaderStream<S>
impl<S: Stream> From<S> for ReaderStream<S>
source§impl<'r, S> Responder<'r, 'r> for ReaderStream<S>where
S: Send + 'r + Stream,
S::Item: AsyncRead + Send,
impl<'r, S> Responder<'r, 'r> for ReaderStream<S>where S: Send + 'r + Stream, S::Item: AsyncRead + Send,
impl<'__pin, S: Stream> Unpin for ReaderStream<S>where __Origin<'__pin, S>: Unpin,
Auto Trait Implementations§
impl<S> RefUnwindSafe for ReaderStream<S>where S: RefUnwindSafe, <S as Stream>::Item: RefUnwindSafe,
impl<S> Send for ReaderStream<S>where S: Send, <S as Stream>::Item: Send,
impl<S> Sync for ReaderStream<S>where S: Sync, <S as Stream>::Item: Sync,
impl<S> UnwindSafe for ReaderStream<S>where S: UnwindSafe, <S as Stream>::Item: UnwindSafe,
Blanket Implementations§
§impl<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedExplicit<'a> for Twhere T: 'a,
§impl<'a, T> AsTaggedImplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedImplicit<'a> for Twhere T: 'a,
source§impl<R> AsyncReadExt for Rwhere
R: AsyncRead + ?Sized,
impl<R> AsyncReadExt for Rwhere R: AsyncRead + ?Sized,
source§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where Self: Unpin,
source§fn read_buf<B, 'a>(&'a mut self, buf: &'a mut B) -> ReadBuf<'a, Self, B>where
Self: Sized + Unpin,
B: BufMut,
fn read_buf<B, 'a>(&'a mut self, buf: &'a mut B) -> ReadBuf<'a, Self, B>where Self: Sized + Unpin, B: BufMut,
source§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where Self: Unpin,
buf
. Read moresource§fn read_u8<'a>(&'a mut self) -> ReadU8<&'a mut Self>where
Self: Unpin,
fn read_u8<'a>(&'a mut self) -> ReadU8<&'a mut Self>where Self: Unpin,
source§fn read_i8<'a>(&'a mut self) -> ReadI8<&'a mut Self>where
Self: Unpin,
fn read_i8<'a>(&'a mut self) -> ReadI8<&'a mut Self>where Self: Unpin,
source§fn read_u16<'a>(&'a mut self) -> ReadU16<&'a mut Self>where
Self: Unpin,
fn read_u16<'a>(&'a mut self) -> ReadU16<&'a mut Self>where Self: Unpin,
source§fn read_i16<'a>(&'a mut self) -> ReadI16<&'a mut Self>where
Self: Unpin,
fn read_i16<'a>(&'a mut self) -> ReadI16<&'a mut Self>where Self: Unpin,
source§fn read_u32<'a>(&'a mut self) -> ReadU32<&'a mut Self>where
Self: Unpin,
fn read_u32<'a>(&'a mut self) -> ReadU32<&'a mut Self>where Self: Unpin,
source§fn read_i32<'a>(&'a mut self) -> ReadI32<&'a mut Self>where
Self: Unpin,
fn read_i32<'a>(&'a mut self) -> ReadI32<&'a mut Self>where Self: Unpin,
source§fn read_u64<'a>(&'a mut self) -> ReadU64<&'a mut Self>where
Self: Unpin,
fn read_u64<'a>(&'a mut self) -> ReadU64<&'a mut Self>where Self: Unpin,
source§fn read_i64<'a>(&'a mut self) -> ReadI64<&'a mut Self>where
Self: Unpin,
fn read_i64<'a>(&'a mut self) -> ReadI64<&'a mut Self>where Self: Unpin,
source§fn read_u128<'a>(&'a mut self) -> ReadU128<&'a mut Self>where
Self: Unpin,
fn read_u128<'a>(&'a mut self) -> ReadU128<&'a mut Self>where Self: Unpin,
source§fn read_i128<'a>(&'a mut self) -> ReadI128<&'a mut Self>where
Self: Unpin,
fn read_i128<'a>(&'a mut self) -> ReadI128<&'a mut Self>where Self: Unpin,
source§fn read_f32<'a>(&'a mut self) -> ReadF32<&'a mut Self>where
Self: Unpin,
fn read_f32<'a>(&'a mut self) -> ReadF32<&'a mut Self>where Self: Unpin,
source§fn read_f64<'a>(&'a mut self) -> ReadF64<&'a mut Self>where
Self: Unpin,
fn read_f64<'a>(&'a mut self) -> ReadF64<&'a mut Self>where Self: Unpin,
source§fn read_u16_le<'a>(&'a mut self) -> ReadU16Le<&'a mut Self>where
Self: Unpin,
fn read_u16_le<'a>(&'a mut self) -> ReadU16Le<&'a mut Self>where Self: Unpin,
source§fn read_i16_le<'a>(&'a mut self) -> ReadI16Le<&'a mut Self>where
Self: Unpin,
fn read_i16_le<'a>(&'a mut self) -> ReadI16Le<&'a mut Self>where Self: Unpin,
source§fn read_u32_le<'a>(&'a mut self) -> ReadU32Le<&'a mut Self>where
Self: Unpin,
fn read_u32_le<'a>(&'a mut self) -> ReadU32Le<&'a mut Self>where Self: Unpin,
source§fn read_i32_le<'a>(&'a mut self) -> ReadI32Le<&'a mut Self>where
Self: Unpin,
fn read_i32_le<'a>(&'a mut self) -> ReadI32Le<&'a mut Self>where Self: Unpin,
source§fn read_u64_le<'a>(&'a mut self) -> ReadU64Le<&'a mut Self>where
Self: Unpin,
fn read_u64_le<'a>(&'a mut self) -> ReadU64Le<&'a mut Self>where Self: Unpin,
source§fn read_i64_le<'a>(&'a mut self) -> ReadI64Le<&'a mut Self>where
Self: Unpin,
fn read_i64_le<'a>(&'a mut self) -> ReadI64Le<&'a mut Self>where Self: Unpin,
source§fn read_u128_le<'a>(&'a mut self) -> ReadU128Le<&'a mut Self>where
Self: Unpin,
fn read_u128_le<'a>(&'a mut self) -> ReadU128Le<&'a mut Self>where Self: Unpin,
source§fn read_i128_le<'a>(&'a mut self) -> ReadI128Le<&'a mut Self>where
Self: Unpin,
fn read_i128_le<'a>(&'a mut self) -> ReadI128Le<&'a mut Self>where Self: Unpin,
source§fn read_f32_le<'a>(&'a mut self) -> ReadF32Le<&'a mut Self>where
Self: Unpin,
fn read_f32_le<'a>(&'a mut self) -> ReadF32Le<&'a mut Self>where Self: Unpin,
source§fn read_f64_le<'a>(&'a mut self) -> ReadF64Le<&'a mut Self>where
Self: Unpin,
fn read_f64_le<'a>(&'a mut self) -> ReadF64Le<&'a mut Self>where Self: Unpin,
source§fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEnd<'a, Self>where
Self: Unpin,
fn read_to_end<'a>( &'a mut self, buf: &'a mut Vec<u8, Global> ) -> ReadToEnd<'a, Self>where Self: Unpin,
buf
. Read more§impl<T> FromFd for Twhere
T: From<OwnedFd>,
impl<T> FromFd for Twhere T: From<OwnedFd>,
§impl<T> FromFilelike for Twhere
T: From<OwnedFd>,
impl<T> FromFilelike for Twhere T: From<OwnedFd>,
§fn from_filelike(owned: OwnedFd) -> T
fn from_filelike(owned: OwnedFd) -> T
Self
from the given filelike object. Read more§fn from_into_filelike<Owned>(owned: Owned) -> Twhere
Owned: IntoFilelike,
fn from_into_filelike<Owned>(owned: Owned) -> Twhere Owned: IntoFilelike,
Self
from the given filelike object
converted from into_owned
. Read more§impl<T> FromSocketlike for Twhere
T: From<OwnedFd>,
impl<T> FromSocketlike for Twhere T: From<OwnedFd>,
§fn from_socketlike(owned: OwnedFd) -> T
fn from_socketlike(owned: OwnedFd) -> T
Self
from the given socketlike object.§fn from_into_socketlike<Owned>(owned: Owned) -> Twhere
Owned: IntoSocketlike,
fn from_into_socketlike<Owned>(owned: Owned) -> Twhere Owned: IntoSocketlike,
Self
from the given socketlike object
converted from into_owned
.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
§fn into_collection<A>(self) -> SmallVec<A>where
A: Array<Item = T>,
fn into_collection<A>(self) -> SmallVec<A>where A: Array<Item = T>,
self
into a collection.