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

impl<S: Clone> Clone for ByteStream<S>[src]

fn clone(&self) -> ByteStream<S>[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<S: Debug> Debug for ByteStream<S>[src]

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

Formats the value using the given formatter. Read more

impl<S> From<S> for ByteStream<S>[src]

fn from(stream: S) -> Self[src]

Creates a ByteStream from any S: Stream.

impl<'r, S: Stream> Responder<'r, 'r> for ByteStream<S> where
    S: Send + 'r,
    S::Item: AsRef<[u8]> + Send + Unpin + 'r, 
[src]

fn respond_to(self, _: &'r Request<'_>) -> Result<'r>[src]

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

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<!> for T[src]

pub fn from(t: !) -> T[src]

Performs the conversion.

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

fn in_current_span(self) -> Instrumented<Self>[src]

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> IntoCollection<T> for T

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

Converts self into a collection.

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

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.

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

pub fn vzip(self) -> V