Struct knightrs::environment::Environment [−][src]
pub struct Environment<'i, 'o, 'c> { /* fields omitted */ }
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]#[must_use = "simply creating an environment doesn't do anything."]pub fn new() -> Self
[src]
#[must_use = "simply creating a builder does nothing."]pub fn builder() -> Builder<'i, 'o, 'c>
[src]
Creates a new Builder
.
This is simply a helper function, and is provided so that you don’t have to import Builder
.
Examples
let env = Environment::builder().disable_system().build(); // ... do stuff with `env`.
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, Error>
[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");
pub fn run_str<S: AsRef<str>>(&mut self, input: S) -> Result<Value, Error>
[src]
Runs the given string as Knight code, returning the result of its execution.
pub fn run<I>(&mut self, input: I) -> Result<Value, Error> where
I: IntoIterator<Item = char>,
[src]
I: IntoIterator<Item = char>,
Parses a Value
from the given iterator and then runs the value.
Trait Implementations
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]
Read bytes into data
from self
’s stdin
.
The stdin
can be customized at creation via Builder::stdin
.
pub fn read_vectored(
&mut self,
bufs: &mut [IoSliceMut<'_>]
) -> Result<usize, Error>
1.36.0[src]
&mut self,
bufs: &mut [IoSliceMut<'_>]
) -> Result<usize, Error>
pub fn is_read_vectored(&self) -> bool
[src]
pub unsafe fn initializer(&self) -> Initializer
[src]
pub fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
1.0.0[src]
pub fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0[src]
pub fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0[src]
pub fn by_ref(&mut self) -> &mut Self
1.0.0[src]
pub fn bytes(self) -> Bytes<Self>
1.0.0[src]
pub fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
1.0.0[src]
R: Read,
pub fn take(self, limit: u64) -> Take<Self>
1.0.0[src]
impl Write for Environment<'_, '_, '_>
[src]
impl Write for Environment<'_, '_, '_>
[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]
pub fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>
1.36.0[src]
pub fn is_write_vectored(&self) -> bool
[src]
pub fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0[src]
pub fn write_all_vectored(
&mut self,
bufs: &mut [IoSlice<'_>]
) -> Result<(), Error>
[src]
&mut self,
bufs: &mut [IoSlice<'_>]
) -> Result<(), Error>
pub fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>
1.0.0[src]
pub fn by_ref(&mut self) -> &mut Self
1.0.0[src]
Auto Trait Implementations
impl<'i, 'o, 'c> !RefUnwindSafe for Environment<'i, 'o, 'c>
impl<'i, 'o, 'c> !RefUnwindSafe for Environment<'i, 'o, 'c>
impl<'i, 'o, 'c> !Send 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> !Sync for Environment<'i, 'o, 'c>
impl<'i, 'o, 'c> Unpin 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>
impl<'i, 'o, 'c> !UnwindSafe for Environment<'i, 'o, 'c>