Struct rocket::response::Stream

source ·
pub struct Stream<T: Read>(/* private fields */);
Expand description

Streams a response to a client from an arbitrary Reader type.

The client is sent a “chunked” response, where the chunk size is at most 4KiB. This means that at most 4KiB are stored in memory while the response is being sent. This type should be used when sending responses that are arbitrarily large in size, such as when streaming from a local socket.

Implementations§

source§

impl<T: Read> Stream<T>

source

pub fn chunked(reader: T, chunk_size: u64) -> Stream<T>

Create a new stream from the given reader and sets the chunk size for each streamed chunk to chunk_size bytes.

§Example

Stream a response from whatever is in stdin with a chunk size of 10 bytes. Note: you probably shouldn’t do this.

use std::io;
use rocket::response::Stream;

let response = Stream::chunked(io::stdin(), 10);
§Buffering and blocking

Normally, data will be buffered and sent only in complete chunk_size chunks.

With the feature sse enabled, the Reader may signal that data sent so far should be transmitted in a timely fashion (e.g. it is responding to a Server-Side Events (JavaScript EventSource) request. To do this it should return an io::Error of kind WouldBlock (which should not normally occur), after returning a collection of data. This will cause a flush of data seen so far, rather than being treated as an error.

Note that long-running responses may easily exhaust Rocket’s thread pool, so consider increasing the number of threads. If doing SSE, also note the ‘maximum open connections’ browser limitation which is described in the EventSource documentation on the Mozilla Developer Network.

Without the sse feature, a WouldBlock error is treated as an actual error.

Trait Implementations§

source§

impl<T: Read + Debug> Debug for Stream<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Read> From<T> for Stream<T>

Create a new stream from the given reader.

§Example

Stream a response from whatever is in stdin. Note: you probably shouldn’t do this.

use std::io;
use rocket::response::Stream;

let response = Stream::from(io::stdin());
source§

fn from(reader: T) -> Self

Converts to this type from the input type.
source§

impl<'r, T: Read + 'r> Responder<'r> for Stream<T>

Sends a response to the client using the “Chunked” transfer encoding. The maximum chunk size is 4KiB.

§Failure

If reading from the input stream fails at any point during the response, the response is abandoned, and the response ends abruptly. An error is printed to the console with an indication of what went wrong.

source§

fn respond_to(self, _: &Request<'_>) -> Result<Response<'r>, Status>

Returns Ok if a Response could be generated successfully. Otherwise, returns an Err with a failing Status. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Stream<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Stream<T>
where T: RefUnwindSafe,

§

impl<T> Send for Stream<T>
where T: Send,

§

impl<T> Sync for Stream<T>
where T: Sync,

§

impl<T> Unpin for Stream<T>
where T: Unpin,

§

impl<T> UnwindSafe for Stream<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T, I> AsResult<T, I> for T
where I: Input,

source§

fn as_result(self) -> Result<T, ParseErr<I>>

source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> IntoCollection<T> for T

§

fn into_collection<A>(self) -> SmallVec<A>
where A: Array<Item = T>,

Converts self into a collection.
§

fn mapped<U, F, A>(self, f: F) -> SmallVec<A>
where F: FnMut(T) -> U, A: Array<Item = U>,

source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> Typeable for T
where T: Any,

source§

fn get_type(&self) -> TypeId

Get the TypeId of this object.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V