pub struct Environment<'i, 'o, 'c> { /* private fields */ }
Expand description
The execution environment for Knight programs.
To aid in embedding Knight in other systems, the Environment
provides complete control over the stdin, stdout,
and output of the “system” (`
), in addition to keeping track of all relevant
variables. Because of this, the environment must always be passed when calling Value::run
.
This is in contrast with most other Knight implementations, which usually have a singular, global “environment”, and
§Examples
let mut env = Environment::new();
// Write to stdout.
writeln!(env, "Hello, world!");
// Read from stdin.
let mut str = String::new();
env.read_to_string(&mut str).expect("cant read from stdin!");
// execute command
println!("The stdout of `ls -al` is {}", env.system("ls -al").expect("`ls -al` failed"));
// create a variable
let var = env.get("foobar");
assert_eq!(var, env.get("foobar")); // both variables are the same.
Implementations§
Source§impl<'i, 'o, 'c> Environment<'i, 'o, 'c>
impl<'i, 'o, 'c> Environment<'i, 'o, 'c>
Sourcepub fn get<N: AsRef<str> + ToString + ?Sized>(&mut self, name: &N) -> Variable
pub fn get<N: AsRef<str> + ToString + ?Sized>(&mut self, name: &N) -> Variable
Retrieves the variable with the given name.
This method will always succeed; if this is the first time that name
has been seen by self
, a new (unassigned
) variable will be created.
§Examples
let mut env = Environment::new();
let var = env.get("plato");
assert_eq!(var, env.get("plato"));
Sourcepub fn system(&mut self, cmd: &str) -> Result<Text>
pub fn system(&mut self, cmd: &str) -> Result<Text>
Executes cmd
as a system command, returning the stdout of the child process.
This will internally call the value that was set for Builder::system
. See that function for more details
on, eg, the default value.
§Examples
let mut env = Environment::new();
assert_eq!(env.system("echo 'hello, knight!'").unwrap().as_str(), "hello, knight!\n");
Trait Implementations§
Source§impl BufRead for Environment<'_, '_, '_>
impl BufRead for Environment<'_, '_, '_>
Source§fn fill_buf(&mut self) -> Result<&[u8]>
fn fill_buf(&mut self) -> Result<&[u8]>
Read
methods, if empty. Read moreSource§fn consume(&mut self, amnt: usize)
fn consume(&mut self, amnt: usize)
amount
of additional bytes from the internal buffer as having been read.
Subsequent calls to read
only return bytes that have not been marked as read. Read moreSource§fn read_line(&mut self, buf: &mut String) -> Result<usize>
fn read_line(&mut self, buf: &mut String) -> Result<usize>
0xA
byte) is reached, and append
them to the provided String
buffer. Read moreSource§fn has_data_left(&mut self) -> Result<bool, Error>
fn has_data_left(&mut self) -> Result<bool, Error>
buf_read_has_data_left
)read
. Read more1.83.0 · Source§fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
byte
or EOF is reached. Read moreSource§impl Debug for Environment<'_, '_, '_>
impl Debug for Environment<'_, '_, '_>
Source§impl Default for Environment<'_, '_, '_>
impl Default for Environment<'_, '_, '_>
Source§impl Read for Environment<'_, '_, '_>
impl Read for Environment<'_, '_, '_>
Source§fn read(&mut self, data: &mut [u8]) -> Result<usize>
fn read(&mut self, data: &mut [u8]) -> Result<usize>
Read bytes into data
from self
’s stdin
.
The stdin
can be customized at creation via Builder::stdin
.
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl Write for Environment<'_, '_, '_>
impl Write for Environment<'_, '_, '_>
Source§fn write(&mut self, data: &[u8]) -> Result<usize>
fn write(&mut self, data: &[u8]) -> Result<usize>
Writes data
’s bytes into self
’s stdout
.
The stdin
can be customized at creation via Builder::stdin
.
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)