Struct knightrs::environment::Environment [−][src]
pub struct Environment<'i, 'o, 'c> { /* fields omitted */ }
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
impl<'i, 'o, 'c> Environment<'i, 'o, 'c>
[src]
impl<'i, 'o, 'c> Environment<'i, 'o, 'c>
[src]pub fn get<N: AsRef<str> + ToString + ?Sized>(&mut self, name: &N) -> Variable
[src]
pub fn get<N: AsRef<str> + ToString + ?Sized>(&mut self, name: &N) -> Variable
[src]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"));
pub fn system(&mut self, cmd: &str) -> Result<Text>
[src]
pub fn system(&mut self, cmd: &str) -> Result<Text>
[src]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
impl BufRead for Environment<'_, '_, '_>
[src]
impl BufRead for Environment<'_, '_, '_>
[src]fn fill_buf(&mut self) -> Result<&[u8]>
[src]
fn fill_buf(&mut self) -> Result<&[u8]>
[src]Returns the contents of the internal buffer, filling it with more data from the inner reader if it is empty. Read more
fn consume(&mut self, amnt: usize)
[src]
fn consume(&mut self, amnt: usize)
[src]Tells this buffer that amt
bytes have been consumed from the buffer,
so they should no longer be returned in calls to read
. Read more
fn read_line(&mut self, buf: &mut String) -> Result<usize>
[src]
fn read_line(&mut self, buf: &mut String) -> Result<usize>
[src]Read all bytes until a newline (the 0xA
byte) is reached, and append
them to the provided buffer. Read more
fn read_until(
&mut self,
byte: u8,
buf: &mut Vec<u8, Global>
) -> Result<usize, Error>
1.0.0[src]
fn read_until(
&mut self,
byte: u8,
buf: &mut Vec<u8, Global>
) -> Result<usize, Error>
1.0.0[src]Read all bytes into buf
until the delimiter byte
or EOF is reached. Read more
impl Debug for Environment<'_, '_, '_>
[src]
impl Debug for Environment<'_, '_, '_>
[src]impl Default for Environment<'_, '_, '_>
[src]
impl Default for Environment<'_, '_, '_>
[src]impl Read for Environment<'_, '_, '_>
[src]
impl Read for Environment<'_, '_, '_>
[src]fn read(&mut self, data: &mut [u8]) -> Result<usize>
[src]
fn read(&mut self, data: &mut [u8]) -> Result<usize>
[src]Read bytes into data
from self
’s stdin
.
The stdin
can be customized at creation via Builder::stdin
.
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
1.36.0[src]
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
1.36.0[src]Like read
, except that it reads into a slice of buffers. Read more
fn is_read_vectored(&self) -> bool
[src]
fn is_read_vectored(&self) -> bool
[src]can_vector
)Determines if this Read
er has an efficient read_vectored
implementation. Read more
unsafe fn initializer(&self) -> Initializer
[src]
unsafe fn initializer(&self) -> Initializer
[src]read_initializer
)Determines if this Read
er can work with buffers of uninitialized
memory. Read more
fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
1.0.0[src]
fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
1.0.0[src]Read all bytes until EOF in this source, placing them into buf
. Read more
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0[src]
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0[src]Read all bytes until EOF in this source, appending them to buf
. Read more
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0[src]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0[src]Read the exact number of bytes required to fill buf
. Read more
fn by_ref(&mut self) -> &mut Self
1.0.0[src]
fn by_ref(&mut self) -> &mut Self
1.0.0[src]Creates a “by reference” adaptor for this instance of Read
. Read more
impl Write for Environment<'_, '_, '_>
[src]
impl Write for Environment<'_, '_, '_>
[src]fn write(&mut self, data: &[u8]) -> Result<usize>
[src]
fn write(&mut self, data: &[u8]) -> Result<usize>
[src]Writes data
’s bytes into self
’s stdout
.
The stdin
can be customized at creation via Builder::stdin
.
fn flush(&mut self) -> Result<()>
[src]
fn flush(&mut self) -> Result<()>
[src]Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
fn is_write_vectored(&self) -> bool
[src]
fn is_write_vectored(&self) -> bool
[src]can_vector
)Determines if this Write
r has an efficient write_vectored
implementation. Read more
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0[src]
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0[src]Attempts to write an entire buffer into this writer. Read more
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
[src]
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
[src]write_all_vectored
)Attempts to write multiple buffers into this writer. Read more
Auto Trait Implementations
impl<'i, 'o, 'c> !RefUnwindSafe for Environment<'i, 'o, 'c>
impl<'i, 'o, 'c> !Send for Environment<'i, 'o, 'c>
impl<'i, 'o, 'c> !Sync for Environment<'i, 'o, 'c>
impl<'i, 'o, 'c> Unpin for Environment<'i, 'o, 'c>
impl<'i, 'o, 'c> !UnwindSafe for Environment<'i, 'o, 'c>
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,