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§
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(&mut self) -> ReadU8<&mut Self>where
Self: Unpin,
fn read_u8(&mut self) -> ReadU8<&mut Self>where Self: Unpin,
source§fn read_i8(&mut self) -> ReadI8<&mut Self>where
Self: Unpin,
fn read_i8(&mut self) -> ReadI8<&mut Self>where Self: Unpin,
source§fn read_u16(&mut self) -> ReadU16<&mut Self>where
Self: Unpin,
fn read_u16(&mut self) -> ReadU16<&mut Self>where Self: Unpin,
source§fn read_i16(&mut self) -> ReadI16<&mut Self>where
Self: Unpin,
fn read_i16(&mut self) -> ReadI16<&mut Self>where Self: Unpin,
source§fn read_u32(&mut self) -> ReadU32<&mut Self>where
Self: Unpin,
fn read_u32(&mut self) -> ReadU32<&mut Self>where Self: Unpin,
source§fn read_i32(&mut self) -> ReadI32<&mut Self>where
Self: Unpin,
fn read_i32(&mut self) -> ReadI32<&mut Self>where Self: Unpin,
source§fn read_u64(&mut self) -> ReadU64<&mut Self>where
Self: Unpin,
fn read_u64(&mut self) -> ReadU64<&mut Self>where Self: Unpin,
source§fn read_i64(&mut self) -> ReadI64<&mut Self>where
Self: Unpin,
fn read_i64(&mut self) -> ReadI64<&mut Self>where Self: Unpin,
source§fn read_u128(&mut self) -> ReadU128<&mut Self>where
Self: Unpin,
fn read_u128(&mut self) -> ReadU128<&mut Self>where Self: Unpin,
source§fn read_i128(&mut self) -> ReadI128<&mut Self>where
Self: Unpin,
fn read_i128(&mut self) -> ReadI128<&mut Self>where Self: Unpin,
source§fn read_f32(&mut self) -> ReadF32<&mut Self>where
Self: Unpin,
fn read_f32(&mut self) -> ReadF32<&mut Self>where Self: Unpin,
source§fn read_f64(&mut self) -> ReadF64<&mut Self>where
Self: Unpin,
fn read_f64(&mut self) -> ReadF64<&mut Self>where Self: Unpin,
source§fn read_u16_le(&mut self) -> ReadU16Le<&mut Self>where
Self: Unpin,
fn read_u16_le(&mut self) -> ReadU16Le<&mut Self>where Self: Unpin,
source§fn read_i16_le(&mut self) -> ReadI16Le<&mut Self>where
Self: Unpin,
fn read_i16_le(&mut self) -> ReadI16Le<&mut Self>where Self: Unpin,
source§fn read_u32_le(&mut self) -> ReadU32Le<&mut Self>where
Self: Unpin,
fn read_u32_le(&mut self) -> ReadU32Le<&mut Self>where Self: Unpin,
source§fn read_i32_le(&mut self) -> ReadI32Le<&mut Self>where
Self: Unpin,
fn read_i32_le(&mut self) -> ReadI32Le<&mut Self>where Self: Unpin,
source§fn read_u64_le(&mut self) -> ReadU64Le<&mut Self>where
Self: Unpin,
fn read_u64_le(&mut self) -> ReadU64Le<&mut Self>where Self: Unpin,
source§fn read_i64_le(&mut self) -> ReadI64Le<&mut Self>where
Self: Unpin,
fn read_i64_le(&mut self) -> ReadI64Le<&mut Self>where Self: Unpin,
source§fn read_u128_le(&mut self) -> ReadU128Le<&mut Self>where
Self: Unpin,
fn read_u128_le(&mut self) -> ReadU128Le<&mut Self>where Self: Unpin,
source§fn read_i128_le(&mut self) -> ReadI128Le<&mut Self>where
Self: Unpin,
fn read_i128_le(&mut self) -> ReadI128Le<&mut Self>where Self: Unpin,
source§fn read_f32_le(&mut self) -> ReadF32Le<&mut Self>where
Self: Unpin,
fn read_f32_le(&mut self) -> ReadF32Le<&mut Self>where Self: Unpin,
source§fn read_f64_le(&mut self) -> ReadF64Le<&mut Self>where
Self: Unpin,
fn read_f64_le(&mut self) -> ReadF64Le<&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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
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>
source§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere T: ?Sized,
source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightYellow
.
Example
println!("{}", value.bright_yellow());
source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightMagenta
.
Example
println!("{}", value.bright_magenta());
source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlack
.
Example
println!("{}", value.on_bright_black());
source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightGreen
.
Example
println!("{}", value.on_bright_green());
source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightYellow
.
Example
println!("{}", value.on_bright_yellow());
source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlue
.
Example
println!("{}", value.on_bright_blue());
source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightMagenta
.
Example
println!("{}", value.on_bright_magenta());
source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightCyan
.
Example
println!("{}", value.on_bright_cyan());
source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightWhite
.
Example
println!("{}", value.on_bright_white());
source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
source§fn underline(&self) -> Painted<&T>
fn underline(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::Underline
.
Example
println!("{}", value.underline());
source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::RapidBlink
.
Example
println!("{}", value.rapid_blink());
source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);