Struct rocket::data::DataStream
source · pub struct DataStream<'r> { /* private fields */ }
Expand description
Raw data stream of a request body.
This stream can only be obtained by calling
Data::open()
with a data limit. The stream
contains all of the data in the body of the request.
Reading from a DataStream
is accomplished via the various methods on the
structure. In general, methods exists in two variants: those that check
whether the entire stream was read and those that don’t. The former either
directly or indirectly (via Capped
) return an N
which allows
checking if the stream was read to completion while the latter do not.
Read Into | Method | Notes |
---|---|---|
String | DataStream::into_string() | Completeness checked. Preferred. |
String | AsyncReadExt::read_to_string() | Unchecked w/existing String . |
Vec<u8> | DataStream::into_bytes() | Checked. Preferred. |
Vec<u8> | DataStream::stream_to(&mut vec) | Checked w/existing Vec . |
Vec<u8> | DataStream::stream_precise_to() | Unchecked w/existing Vec . |
File | DataStream::into_file() | Checked. Preferred. |
File | DataStream::stream_to(&mut file) | Checked w/ existing File . |
File | DataStream::stream_precise_to() | Unchecked w/ existing File . |
T | DataStream::stream_to() | Checked. Any T: AsyncWrite . |
T | DataStream::stream_precise_to() | Unchecked. Any T: AsyncWrite . |
Implementations§
source§impl<'r> DataStream<'r>
impl<'r> DataStream<'r>
sourcepub fn hint(&self) -> usize
pub fn hint(&self) -> usize
Number of bytes a full read from self
will definitely read.
Example
use rocket::data::{Data, ToByteUnit};
async fn f(data: Data<'_>) {
let definitely_have_n_bytes = data.open(1.kibibytes()).hint();
}
sourcepub async fn stream_to<W>(self, writer: W) -> Result<N>where
W: AsyncWrite + Unpin,
pub async fn stream_to<W>(self, writer: W) -> Result<N>where W: AsyncWrite + Unpin,
A helper method to write the body of the request to any AsyncWrite
type. Returns an N
which indicates how many bytes were written and
whether the entire stream was read. An additional read from self
may
be required to check if all of the stream has been read. If that
information is not needed, use DataStream::stream_precise_to()
.
This method is identical to tokio::io::copy(&mut self, &mut writer)
except in that it returns an N
to check for completeness.
Example
use std::io;
use rocket::data::{Data, ToByteUnit};
async fn data_guard(mut data: Data<'_>) -> io::Result<String> {
// write all of the data to stdout
let written = data.open(512.kibibytes())
.stream_to(tokio::io::stdout()).await?;
Ok(format!("Wrote {} bytes.", written))
}
sourcepub async fn stream_precise_to<W>(self, writer: W) -> Result<u64>where
W: AsyncWrite + Unpin,
pub async fn stream_precise_to<W>(self, writer: W) -> Result<u64>where W: AsyncWrite + Unpin,
Like DataStream::stream_to()
except that no end-of-stream check is
conducted and thus read/write completeness is unknown.
Example
use std::io;
use rocket::data::{Data, ToByteUnit};
async fn data_guard(mut data: Data<'_>) -> io::Result<String> {
// write all of the data to stdout
let written = data.open(512.kibibytes())
.stream_precise_to(tokio::io::stdout()).await?;
Ok(format!("Wrote {} bytes.", written))
}
sourcepub async fn into_bytes(self) -> Result<Capped<Vec<u8>>>
pub async fn into_bytes(self) -> Result<Capped<Vec<u8>>>
A helper method to write the body of the request to a Vec<u8>
.
Example
use std::io;
use rocket::data::{Data, ToByteUnit};
async fn data_guard(data: Data<'_>) -> io::Result<Vec<u8>> {
let bytes = data.open(4.kibibytes()).into_bytes().await?;
if !bytes.is_complete() {
println!("there are bytes remaining in the stream");
}
Ok(bytes.into_inner())
}
sourcepub async fn into_string(self) -> Result<Capped<String>>
pub async fn into_string(self) -> Result<Capped<String>>
A helper method to write the body of the request to a String
.
Example
use std::io;
use rocket::data::{Data, ToByteUnit};
async fn data_guard(data: Data<'_>) -> io::Result<String> {
let string = data.open(10.bytes()).into_string().await?;
if !string.is_complete() {
println!("there are bytes remaining in the stream");
}
Ok(string.into_inner())
}
sourcepub async fn into_file<P: AsRef<Path>>(self, path: P) -> Result<Capped<File>>
pub async fn into_file<P: AsRef<Path>>(self, path: P) -> Result<Capped<File>>
A helper method to write the body of the request to a file at the path
determined by path
. If a file at the path already exists, it is
overwritten. The opened file is returned.
Example
use std::io;
use rocket::data::{Data, ToByteUnit};
async fn data_guard(mut data: Data<'_>) -> io::Result<String> {
let file = data.open(1.megabytes()).into_file("/static/file").await?;
if !file.is_complete() {
println!("there are bytes remaining in the stream");
}
Ok(format!("Wrote {} bytes to /static/file", file.n))
}
Trait Implementations§
Auto Trait Implementations§
impl<'r> !RefUnwindSafe for DataStream<'r>
impl<'r> Send for DataStream<'r>
impl<'r> Sync for DataStream<'r>
impl<'r> Unpin for DataStream<'r>
impl<'r> !UnwindSafe for DataStream<'r>
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 moresource§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.