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]

#[must_use = "simply creating an environment doesn't do anything."]
pub fn new() -> Self
[src]

Creates an empty Environment.

Examples

let env = Environment::new();

// ...do stuff with `env`.

#[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]

Parses a Value from the given iterator and then runs the value.

Trait Implementations

impl Debug for Environment<'_, '_, '_>[src]

impl Default 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.

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.

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> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,