Skip to main content

Virtual

Struct Virtual 

Source
pub struct Virtual { /* private fields */ }
Expand description

Provides virtual input/output/error streams: input can be provided using Virtual::write_input(), and output can be observed using Virtual::read_output() and Virtual::read_error().

Implementations§

Source§

impl Virtual

Source

pub fn new() -> Virtual

Creates a new, empty virtual stream provider.

Source

pub fn write_input(&mut self, input: &[u8])

Writes the provided buffer to the queue of buffers to be used when input is requested from this provider using Provider::input().

In particular, this method does NOT append data to a continuous buffer which is consumed by Provider::input(); rather, it enqueues a buffer which will be used for a SINGLE call to Provider::input(). The buffer is then discarded, regardless of how much of it was (or was not) read.

This enables precise control over the length of data returned from a call to Provider::input().

§Example
use io_providers::stream;

let mut streams = stream::Virtual::new();
streams.write_input("foo".as_bytes());
streams.write_input("bar".as_bytes());
// The first read on `streams.input()` will read from "foo"
// The second read on `streams.input()` will read from "bar"
Source

pub fn read_output<'a>(&'a self) -> &'a [u8]

Gets the data which has been written to the output stream.

§Example
use std::io::Write;
use io_providers::stream;
use io_providers::stream::Provider;

let mut streams = stream::Virtual::new();
writeln!(streams.output(), "test1");
write!(streams.output(), "test2");
assert_eq!("test1\ntest2", ::std::str::from_utf8(streams.read_output()).unwrap());
Source

pub fn read_error<'a>(&'a self) -> &'a [u8]

Gets the data which has been written to error stream.

§Example
use std::io::Write;
use io_providers::stream;
use io_providers::stream::Provider;

let mut streams = stream::Virtual::new();
writeln!(streams.error(), "test1");
write!(streams.error(), "test2");
assert_eq!("test1\ntest2", ::std::str::from_utf8(streams.read_error()).unwrap());

Trait Implementations§

Source§

impl Provider for Virtual

Source§

fn input(&mut self) -> &mut dyn Read

Gets the input stream.
Source§

fn output(&mut self) -> &mut dyn Write

Gets the output stream.
Source§

fn error(&mut self) -> &mut dyn Write

Gets the error stream.

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