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]

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

Runs the given string as Knight code, returning the result of its execution.

pub fn run<I>(&mut self, input: I) -> Result<Value> where
    I: IntoIterator<Item = char>, 
[src]

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

Trait Implementations

impl BufRead for Environment<'_, '_, '_>[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]

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]

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]

Read all bytes into buf until the delimiter byte or EOF is reached. Read more

fn split(self, byte: u8) -> Split<Self>1.0.0[src]

Returns an iterator over the contents of this reader split on the byte byte. Read more

fn lines(self) -> Lines<Self>1.0.0[src]

Returns an iterator over the lines of this reader. Read more

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

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

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

fn default() -> Self[src]

Returns the “default value” for a type. Read more

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.

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]

🔬 This is a nightly-only experimental API. (can_vector)

Determines if this Reader has an efficient read_vectored implementation. Read more

unsafe fn initializer(&self) -> Initializer[src]

🔬 This is a nightly-only experimental API. (read_initializer)

Determines if this Reader 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]

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]

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]

Read the exact number of bytes required to fill buf. Read more

fn by_ref(&mut self) -> &mut Self1.0.0[src]

Creates a “by reference” adaptor for this instance of Read. Read more

fn bytes(self) -> Bytes<Self>1.0.0[src]

Transforms this Read instance to an Iterator over its bytes. Read more

fn chain<R>(self, next: R) -> Chain<Self, R> where
    R: Read
1.0.0[src]

Creates an adaptor which will chain this stream with another. Read more

fn take(self, limit: u64) -> Take<Self>1.0.0[src]

Creates an adaptor which will read at most limit bytes from it. Read more

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]

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>1.36.0[src]

Like write, except that it writes from a slice of buffers. Read more

fn is_write_vectored(&self) -> bool[src]

🔬 This is a nightly-only experimental API. (can_vector)

Determines if this Writer has an efficient write_vectored implementation. Read more

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]

🔬 This is a nightly-only experimental API. (write_all_vectored)

Attempts to write multiple buffers into this writer. Read more

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>1.0.0[src]

Writes a formatted string into this writer, returning any error encountered. Read more

fn by_ref(&mut self) -> &mut Self1.0.0[src]

Creates a “by reference” adaptor for this instance of Write. 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> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

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

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

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

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

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

pub fn from(t: T) -> T[src]

Performs the conversion.

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

pub fn into(self) -> U[src]

Performs the conversion.

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.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

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.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.

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

pub fn vzip(self) -> V