pub struct ByteStream { /* private fields */ }
Expand description
A potentially infinite, interruptible stream of bytes.
To create a ByteStream
, you can use any of the following methods:
read
: takes any type that implementsRead
.file
: takes aFile
.from_iter
: takes anIterator
whose items implementAsRef<[u8]>
.from_result_iter
: same asfrom_iter
, but each item is aResult<T, ShellError>
.from_fn
: uses a generator function to fill a buffer whenever it is empty. This has high performance because it doesn’t need to allocate for each chunk of data, and can just reuse the same buffer.
Byte streams have a type which is used to preserve type compatibility when they
are the result of an internal command. It is important that this be set to the correct value.
Unknown
is used only for external sources where the type can not
be inherently determined, and having it automatically act as a string or binary depending on
whether it parses as UTF-8 or not is desirable.
The data of a ByteStream
can be accessed using one of the following methods:
reader
: returns aRead
-able type to get the raw bytes in the stream.lines
: splits the bytes on lines and returns anIterator
where each item is aResult<String, ShellError>
.chunks
: returns anIterator
ofValue
s where each value is either a string or binary. Try not to use this method if possible. Rather, please usereader
(orlines
if it matches the situation).
Additionally, there are few methods to collect a ByteStream
into memory:
into_bytes
: collects all bytes into aVec<u8>
.into_string
: collects all bytes into aString
, erroring if utf-8 decoding failed.into_value
: collects all bytes into a value typed appropriately for the type of this stream. If the type isUnknown
, it will produce a string value if the data is valid UTF-8, or a binary value otherwise.
There are also a few other methods to consume all the data of a ByteStream
:
drain
: consumes all bytes and outputs nothing.write_to
: writes all bytes to the givenWrite
destination.print
: a convenience wrapper aroundwrite_to
. It prints all bytes to stdout or stderr.
Internally, ByteStream
s currently come in three flavors according to ByteStreamSource
.
See its documentation for more information.
Implementations§
Source§impl ByteStream
impl ByteStream
Sourcepub fn new(
stream: ByteStreamSource,
span: Span,
signals: Signals,
type_: ByteStreamType,
) -> ByteStream
pub fn new( stream: ByteStreamSource, span: Span, signals: Signals, type_: ByteStreamType, ) -> ByteStream
Create a new ByteStream
from a ByteStreamSource
.
Sourcepub fn read(
reader: impl Read + Send + 'static,
span: Span,
signals: Signals,
type_: ByteStreamType,
) -> ByteStream
pub fn read( reader: impl Read + Send + 'static, span: Span, signals: Signals, type_: ByteStreamType, ) -> ByteStream
Create a ByteStream
from an arbitrary reader. The type must be provided.
Sourcepub fn read_string(string: String, span: Span, signals: Signals) -> ByteStream
pub fn read_string(string: String, span: Span, signals: Signals) -> ByteStream
Create a ByteStream
from a string. The type of the stream is always String
.
Sourcepub fn read_binary(bytes: Vec<u8>, span: Span, signals: Signals) -> ByteStream
pub fn read_binary(bytes: Vec<u8>, span: Span, signals: Signals) -> ByteStream
Create a ByteStream
from a byte vector. The type of the stream is always Binary
.
Sourcepub fn file(file: File, span: Span, signals: Signals) -> ByteStream
pub fn file(file: File, span: Span, signals: Signals) -> ByteStream
Create a ByteStream
from a file.
The type is implicitly Unknown
, as it’s not typically known whether files will
return text or binary.
Sourcepub fn child(child: ChildProcess, span: Span) -> ByteStream
pub fn child(child: ChildProcess, span: Span) -> ByteStream
Create a ByteStream
from a child process’s stdout and stderr.
The type is implicitly Unknown
, as it’s not typically known whether child processes will
return text or binary.
Sourcepub fn stdin(span: Span) -> Result<ByteStream, ShellError>
pub fn stdin(span: Span) -> Result<ByteStream, ShellError>
Create a ByteStream
that reads from stdin.
The type is implicitly Unknown
, as it’s not typically known whether stdin is text or
binary.
Sourcepub fn from_fn(
span: Span,
signals: Signals,
type_: ByteStreamType,
generator: impl FnMut(&mut Vec<u8>) -> Result<bool, ShellError> + Send + 'static,
) -> ByteStream
pub fn from_fn( span: Span, signals: Signals, type_: ByteStreamType, generator: impl FnMut(&mut Vec<u8>) -> Result<bool, ShellError> + Send + 'static, ) -> ByteStream
Create a ByteStream
from a generator function that writes data to the given buffer
when called, and returns Ok(false)
on end of stream.
pub fn with_type(self, type_: ByteStreamType) -> ByteStream
Sourcepub fn from_iter<I>(
iter: I,
span: Span,
signals: Signals,
type_: ByteStreamType,
) -> ByteStreamwhere
I: IntoIterator,
<I as IntoIterator>::IntoIter: Send + 'static,
<I as IntoIterator>::Item: AsRef<[u8]> + Default + Send + 'static,
pub fn from_iter<I>(
iter: I,
span: Span,
signals: Signals,
type_: ByteStreamType,
) -> ByteStreamwhere
I: IntoIterator,
<I as IntoIterator>::IntoIter: Send + 'static,
<I as IntoIterator>::Item: AsRef<[u8]> + Default + Send + 'static,
Create a new ByteStream
from an Iterator
of bytes slices.
The returned ByteStream
will have a ByteStreamSource
of Read
.
Sourcepub fn from_result_iter<I, T>(
iter: I,
span: Span,
signals: Signals,
type_: ByteStreamType,
) -> ByteStreamwhere
I: IntoIterator<Item = Result<T, ShellError>>,
<I as IntoIterator>::IntoIter: Send + 'static,
T: AsRef<[u8]> + Default + Send + 'static,
pub fn from_result_iter<I, T>(
iter: I,
span: Span,
signals: Signals,
type_: ByteStreamType,
) -> ByteStreamwhere
I: IntoIterator<Item = Result<T, ShellError>>,
<I as IntoIterator>::IntoIter: Send + 'static,
T: AsRef<[u8]> + Default + Send + 'static,
Create a new ByteStream
from an Iterator
of Result
bytes slices.
The returned ByteStream
will have a ByteStreamSource
of Read
.
Sourcepub fn with_known_size(self, size: Option<u64>) -> ByteStream
pub fn with_known_size(self, size: Option<u64>) -> ByteStream
Set the known size, in number of bytes, of the ByteStream
.
Sourcepub fn source(&self) -> &ByteStreamSource
pub fn source(&self) -> &ByteStreamSource
Get a reference to the inner ByteStreamSource
of the ByteStream
.
Sourcepub fn source_mut(&mut self) -> &mut ByteStreamSource
pub fn source_mut(&mut self) -> &mut ByteStreamSource
Get a mutable reference to the inner ByteStreamSource
of the ByteStream
.
Sourcepub fn span(&self) -> Span
pub fn span(&self) -> Span
Returns the Span
associated with the ByteStream
.
Sourcepub fn with_span(self, span: Span) -> ByteStream
pub fn with_span(self, span: Span) -> ByteStream
Changes the Span
associated with the ByteStream
.
Sourcepub fn type_(&self) -> ByteStreamType
pub fn type_(&self) -> ByteStreamType
Returns the ByteStreamType
associated with the ByteStream
.
Sourcepub fn known_size(&self) -> Option<u64>
pub fn known_size(&self) -> Option<u64>
Returns the known size, in number of bytes, of the ByteStream
.
Sourcepub fn reader(self) -> Option<Reader>
pub fn reader(self) -> Option<Reader>
Convert the ByteStream
into its Reader
which allows one to Read
the raw bytes of the stream.
Reader
is buffered and also implements BufRead
.
If the source of the ByteStream
is ByteStreamSource::Child
and the child has no stdout,
then the stream is considered empty and None
will be returned.
Sourcepub fn lines(self) -> Option<Lines>
pub fn lines(self) -> Option<Lines>
Convert the ByteStream
into a Lines
iterator where each element is a Result<String, ShellError>
.
There is no limit on how large each line will be. Ending new lines (\n
or \r\n
) are
stripped from each line. If a line fails to be decoded as utf-8, then it will become a ShellError
.
If the source of the ByteStream
is ByteStreamSource::Child
and the child has no stdout,
then the stream is considered empty and None
will be returned.
Sourcepub fn chunks(self) -> Option<Chunks>
pub fn chunks(self) -> Option<Chunks>
Convert the ByteStream
into a Chunks
iterator where each element is a Result<Value, ShellError>
.
Each call to next
reads the currently available data from the byte stream source,
up to a maximum size. The values are typed according to the type of the
stream, and if that type is Unknown
, string values will be
produced as long as the stream continues to parse as valid UTF-8, but binary values will
be produced instead of the stream fails to parse as UTF-8 instead at any point.
Any and all newlines are kept intact in each chunk.
Where possible, prefer reader
or lines
over this method.
Those methods are more likely to be used in a semantically correct way
(and reader
is more efficient too).
If the source of the ByteStream
is ByteStreamSource::Child
and the child has no stdout,
then the stream is considered empty and None
will be returned.
Sourcepub fn into_source(self) -> ByteStreamSource
pub fn into_source(self) -> ByteStreamSource
Convert the ByteStream
into its inner ByteStreamSource
.
Sourcepub fn into_stdio(self) -> Result<Stdio, ByteStream>
pub fn into_stdio(self) -> Result<Stdio, ByteStream>
Attempt to convert the ByteStream
into a Stdio
.
This will succeed if the ByteStreamSource
of the ByteStream
is either:
All other cases return an Err
with the original ByteStream
in it.
Sourcepub fn into_child(self) -> Result<ChildProcess, ByteStream>
pub fn into_child(self) -> Result<ChildProcess, ByteStream>
Attempt to convert the ByteStream
into a ChildProcess
.
This will only succeed if the ByteStreamSource
of the ByteStream
is Child
.
All other cases return an Err
with the original ByteStream
in it.
Sourcepub fn into_bytes(self) -> Result<Vec<u8>, ShellError>
pub fn into_bytes(self) -> Result<Vec<u8>, ShellError>
Collect all the bytes of the ByteStream
into a Vec<u8>
.
Any trailing new lines are kept in the returned Vec
.
Sourcepub fn into_string(self) -> Result<String, ShellError>
pub fn into_string(self) -> Result<String, ShellError>
Collect the stream into a String
in-memory. This can only succeed if the data contained is
valid UTF-8.
The trailing new line (\n
or \r\n
), if any, is removed from the String
prior to
being returned, if this is a stream coming from an external process or file.
If the type is specified as Binary
, this operation always fails, even if the
data would have been valid UTF-8.
Sourcepub fn into_value(self) -> Result<Value, ShellError>
pub fn into_value(self) -> Result<Value, ShellError>
Collect all the bytes of the ByteStream
into a Value
.
If this is a String
stream, the stream is decoded to UTF-8. If the stream came from an
external process or file, the trailing new line (\n
or \r\n
), if any, is removed from
the String
prior to being returned.
If this is a Binary
stream, a Value::Binary
is returned with any trailing new lines
preserved.
If this is an Unknown
stream, the behavior depends on whether the stream parses as valid
UTF-8 or not. If it does, this is uses the String
behavior; if not, it uses the Binary
behavior.
Sourcepub fn drain(self) -> Result<(), ShellError>
pub fn drain(self) -> Result<(), ShellError>
Consume and drop all bytes of the ByteStream
.
Sourcepub fn print(self, to_stderr: bool) -> Result<(), ShellError>
pub fn print(self, to_stderr: bool) -> Result<(), ShellError>
Print all bytes of the ByteStream
to stdout or stderr.
Sourcepub fn write_to(self, dest: impl Write) -> Result<(), ShellError>
pub fn write_to(self, dest: impl Write) -> Result<(), ShellError>
Write all bytes of the ByteStream
to dest
.
Trait Implementations§
Source§impl Debug for ByteStream
impl Debug for ByteStream
Source§impl From<ByteStream> for PipelineData
impl From<ByteStream> for PipelineData
Source§fn from(stream: ByteStream) -> PipelineData
fn from(stream: ByteStream) -> PipelineData
Auto Trait Implementations§
impl Freeze for ByteStream
impl !RefUnwindSafe for ByteStream
impl Send for ByteStream
impl !Sync for ByteStream
impl Unpin for ByteStream
impl !UnwindSafe for ByteStream
Blanket Implementations§
Source§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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoSpanned for T
impl<T> IntoSpanned for T
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg
or
a color-specific method, such as OwoColorize::green
, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg
or
a color-specific method, such as OwoColorize::on_yellow
, Read more