pub trait VirtualIo {
// Required methods
fn print<S: Into<String>>(&mut self, message: S) -> &mut Self;
fn println<S: Into<String>>(&mut self, message: S) -> &mut Self;
fn read_line(&mut self) -> String;
fn get_environment_var<S: Into<String>>(
&self,
variable: S,
) -> Result<String, VarError>;
fn get_environment_vars(&self) -> Vec<(String, String)>;
}
Expand description
Virtual IO is a rust library for easily implementing stdin and stdout in a
testable way. It replaces all println!
and print!
macro calls.
use virtual_io::{VirtualIo, Vio};
fn get_name() -> String {
get_name_base(&mut virtual_io::new())
}
fn get_name_base(vio: &mut impl VirtualIo) -> String {
vio.print("What is your name? ");
let name = vio.read_line();
vio.println(format!("Hello, {}!", name));
name
}
Required Methods§
Sourcefn print<S: Into<String>>(&mut self, message: S) -> &mut Self
fn print<S: Into<String>>(&mut self, message: S) -> &mut Self
Prints a message to the console. Is close to the print!
macro with
one difference: output is not buffered and is instead immediately
flushed.
Sourcefn println<S: Into<String>>(&mut self, message: S) -> &mut Self
fn println<S: Into<String>>(&mut self, message: S) -> &mut Self
Prints a message to the console with a new line at the end. Is
equivalent to the println!
macro.
Sourcefn read_line(&mut self) -> String
fn read_line(&mut self) -> String
Get user input from the console. The input ends when the user types a new line.
Sourcefn get_environment_var<S: Into<String>>(
&self,
variable: S,
) -> Result<String, VarError>
fn get_environment_var<S: Into<String>>( &self, variable: S, ) -> Result<String, VarError>
Get an environment variable. Is equivalent to std::env::var
.
§Errors
Returns an error if the environment variable is not present.
Sourcefn get_environment_vars(&self) -> Vec<(String, String)>
fn get_environment_vars(&self) -> Vec<(String, String)>
Get an environment variables as a vector. Each item in the vector is a (key, value) tuple.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.