nu_protocol::byte_stream

Struct ByteStream

Source
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 implements Read.
  • file: takes a File.
  • from_iter: takes an Iterator whose items implement AsRef<[u8]>.
  • from_result_iter: same as from_iter, but each item is a Result<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 a Read-able type to get the raw bytes in the stream.
  • lines: splits the bytes on lines and returns an Iterator where each item is a Result<String, ShellError>.
  • chunks: returns an Iterator of Values where each value is either a string or binary. Try not to use this method if possible. Rather, please use reader (or lines if it matches the situation).

Additionally, there are few methods to collect a ByteStream into memory:

  • into_bytes: collects all bytes into a Vec<u8>.
  • into_string: collects all bytes into a String, 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 is Unknown, 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 given Write destination.
  • print: a convenience wrapper around write_to. It prints all bytes to stdout or stderr.

Internally, ByteStreams currently come in three flavors according to ByteStreamSource. See its documentation for more information.

Implementations§

Source§

impl ByteStream

Source

pub fn new( stream: ByteStreamSource, span: Span, signals: Signals, type_: ByteStreamType, ) -> Self

Create a new ByteStream from a ByteStreamSource.

Source

pub fn read( reader: impl Read + Send + 'static, span: Span, signals: Signals, type_: ByteStreamType, ) -> Self

Create a ByteStream from an arbitrary reader. The type must be provided.

Source

pub fn read_string(string: String, span: Span, signals: Signals) -> Self

Create a ByteStream from a string. The type of the stream is always String.

Source

pub fn read_binary(bytes: Vec<u8>, span: Span, signals: Signals) -> Self

Create a ByteStream from a byte vector. The type of the stream is always Binary.

Source

pub fn file(file: File, span: Span, signals: Signals) -> Self

Create a ByteStream from a file.

The type is implicitly Unknown, as it’s not typically known whether files will return text or binary.

Source

pub fn child(child: ChildProcess, span: Span) -> Self

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.

Source

pub fn stdin(span: Span) -> Result<Self, 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.

Source

pub fn from_fn( span: Span, signals: Signals, type_: ByteStreamType, generator: impl FnMut(&mut Vec<u8>) -> Result<bool, ShellError> + Send + 'static, ) -> Self

Create a ByteStream from a generator function that writes data to the given buffer when called, and returns Ok(false) on end of stream.

Source

pub fn with_type(self, type_: ByteStreamType) -> Self

Source

pub fn from_iter<I>( iter: I, span: Span, signals: Signals, type_: ByteStreamType, ) -> Self
where I: IntoIterator, I::IntoIter: Send + 'static, I::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.

Source

pub fn from_result_iter<I, T>( iter: I, span: Span, signals: Signals, type_: ByteStreamType, ) -> Self
where I: IntoIterator<Item = Result<T, ShellError>>, I::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.

Source

pub fn with_known_size(self, size: Option<u64>) -> Self

Set the known size, in number of bytes, of the ByteStream.

Source

pub fn source(&self) -> &ByteStreamSource

Get a reference to the inner ByteStreamSource of the ByteStream.

Source

pub fn source_mut(&mut self) -> &mut ByteStreamSource

Get a mutable reference to the inner ByteStreamSource of the ByteStream.

Source

pub fn span(&self) -> Span

Returns the Span associated with the ByteStream.

Source

pub fn with_span(self, span: Span) -> Self

Changes the Span associated with the ByteStream.

Source

pub fn type_(&self) -> ByteStreamType

Returns the ByteStreamType associated with the ByteStream.

Source

pub fn known_size(&self) -> Option<u64>

Returns the known size, in number of bytes, of the ByteStream.

Source

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.

Source

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.

Source

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.

Source

pub fn into_source(self) -> ByteStreamSource

Convert the ByteStream into its inner ByteStreamSource.

Source

pub fn into_stdio(self) -> Result<Stdio, Self>

Attempt to convert the ByteStream into a Stdio.

This will succeed if the ByteStreamSource of the ByteStream is either:

  • File
  • Child and the child has a stdout that is Some(ChildPipe::Pipe(..)).

All other cases return an Err with the original ByteStream in it.

Source

pub fn into_child(self) -> Result<ChildProcess, Self>

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.

Source

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.

Source

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.

Source

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.

Source

pub fn drain(self) -> Result<(), ShellError>

Consume and drop all bytes of the ByteStream.

Source

pub fn print(self, to_stderr: bool) -> Result<(), ShellError>

Print all bytes of the ByteStream to stdout or stderr.

Source

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

Source§

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

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

impl From<ByteStream> for PipelineData

Source§

fn from(stream: ByteStream) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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> 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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> IntoSpanned for T

Source§

fn into_spanned(self, span: Span) -> Spanned<T>

Wrap items together with a span into Spanned. Read more
Source§

impl<D> OwoColorize for D

Source§

fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>
where C: Color,

Set the foreground color generically Read more
Source§

fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>
where C: Color,

Set the background color generically. Read more
Source§

fn black(&self) -> FgColorDisplay<'_, Black, Self>

Change the foreground color to black
Source§

fn on_black(&self) -> BgColorDisplay<'_, Black, Self>

Change the background color to black
Source§

fn red(&self) -> FgColorDisplay<'_, Red, Self>

Change the foreground color to red
Source§

fn on_red(&self) -> BgColorDisplay<'_, Red, Self>

Change the background color to red
Source§

fn green(&self) -> FgColorDisplay<'_, Green, Self>

Change the foreground color to green
Source§

fn on_green(&self) -> BgColorDisplay<'_, Green, Self>

Change the background color to green
Source§

fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>

Change the foreground color to yellow
Source§

fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>

Change the background color to yellow
Source§

fn blue(&self) -> FgColorDisplay<'_, Blue, Self>

Change the foreground color to blue
Source§

fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>

Change the background color to blue
Source§

fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to magenta
Source§

fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to magenta
Source§

fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to purple
Source§

fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to purple
Source§

fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>

Change the foreground color to cyan
Source§

fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>

Change the background color to cyan
Source§

fn white(&self) -> FgColorDisplay<'_, White, Self>

Change the foreground color to white
Source§

fn on_white(&self) -> BgColorDisplay<'_, White, Self>

Change the background color to white
Source§

fn default_color(&self) -> FgColorDisplay<'_, Default, Self>

Change the foreground color to the terminal default
Source§

fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>

Change the background color to the terminal default
Source§

fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>

Change the foreground color to bright black
Source§

fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>

Change the background color to bright black
Source§

fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>

Change the foreground color to bright red
Source§

fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>

Change the background color to bright red
Source§

fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>

Change the foreground color to bright green
Source§

fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>

Change the background color to bright green
Source§

fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>

Change the foreground color to bright yellow
Source§

fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>

Change the background color to bright yellow
Source§

fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>

Change the foreground color to bright blue
Source§

fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>

Change the background color to bright blue
Source§

fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright magenta
Source§

fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright magenta
Source§

fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright purple
Source§

fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright purple
Source§

fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>

Change the foreground color to bright cyan
Source§

fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>

Change the background color to bright cyan
Source§

fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>

Change the foreground color to bright white
Source§

fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>

Change the background color to bright white
Source§

fn bold(&self) -> BoldDisplay<'_, Self>

Make the text bold
Source§

fn dimmed(&self) -> DimDisplay<'_, Self>

Make the text dim
Source§

fn italic(&self) -> ItalicDisplay<'_, Self>

Make the text italicized
Source§

fn underline(&self) -> UnderlineDisplay<'_, Self>

Make the text underlined
Make the text blink
Make the text blink (but fast!)
Source§

fn reversed(&self) -> ReversedDisplay<'_, Self>

Swap the foreground and background colors
Source§

fn hidden(&self) -> HiddenDisplay<'_, Self>

Hide the text
Source§

fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>

Cross out the text
Source§

fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either OwoColorize::fg or a color-specific method, such as OwoColorize::green, Read more
Source§

fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either OwoColorize::bg or a color-specific method, such as OwoColorize::on_yellow, Read more
Source§

fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the foreground color to a specific RGB value.
Source§

fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the background color to a specific RGB value.
Source§

fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>

Sets the foreground color to an RGB value.
Source§

fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>

Sets the background color to an RGB value.
Source§

fn style(&self, style: Style) -> Styled<&Self>

Apply a runtime-determined style
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize = _

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.