Ghci

Struct Ghci 

Source
pub struct Ghci { /* private fields */ }
Expand description

A ghci session handle

The session is stateful, so the order of interaction matters

Implementations§

Source§

impl Ghci

Source

pub fn new() -> Result<Self>

Create a new ghci session

It will use ghci on your PATH by default, but can be overridden to use any ghci by setting the GHCI_PATH environment variable pointing at the binary to use

§Errors

Returns IOError when it encounters IO errors as part of spawning the ghci subprocess

Source

pub fn eval(&mut self, input: &str) -> Result<EvalOutput>

Evaluate/run a statement

let mut ghci = Ghci::new()?;
let out = ghci.eval("putStrLn \"Hello world\"")?;
assert_eq!(&out.stdout, "Hello world\n");

Multi-line inputs are also supported. The evaluation output may contain both stdout and stderr:

let mut ghci = Ghci::new()?;
ghci.import(&["System.IO"]); // imports not supported as part of multi-line inputs

let out = ghci.eval(r#"
do
  hPutStrLn stdout "Output on stdout"
  hPutStrLn stderr "Output on stderr"
  hPutStrLn stdout "And a bit more on stdout"
"#)?;

assert_eq!(&out.stderr, "Output on stderr\n");
assert_eq!(&out.stdout, "Output on stdout\nAnd a bit more on stdout\n");
§Errors
  • Returns a Timeout if the evaluation timeout (set by Ghci::set_timeout) is reached before the evaluation completes.
  • Returns a IOError when encounters an IO error on the ghci subprocess stdin, stdout, or stderr.
  • Returns a PollError when waiting for output, if the ghci subprocess stdout or stderr is closed (upon a crash for example)
Source

pub fn set_timeout(&mut self, timeout: Option<Duration>)

Set a timeout for evaluations

let mut ghci = Ghci::new()?;
ghci.import(&["Control.Concurrent"])?;

let res = ghci.eval("threadDelay 50000");
assert!(matches!(res, Ok(_)));

ghci.set_timeout(Some(Duration::from_millis(20)));

let res = ghci.eval("threadDelay 50000");
assert!(matches!(res, Err(GhciError::Timeout)));

By default, no timeout is set.

Note: When a Timeout error is triggered, the ghci session must be closed with Ghci::close or Droped in order to properly stop the corresponding evaluation. If the evaluation is left to finish after a timeout occurs, the session is then left in a bad state that is not recoverable.

Source

pub fn import(&mut self, modules: &[&str]) -> Result<()>

Import multiple modules

let mut ghci = Ghci::new()?;
ghci.import(&["Data.Char", "Control.Applicative"])?;
§Errors

Same as Ghci::eval

Source

pub fn load(&mut self, paths: &[&Path]) -> Result<()>

Load multiple modules by file path

§Errors

Same as Ghci::eval

Source

pub fn close(self) -> Result<()>

Close the ghci session

Closing explicitly is not necessary as the Drop impl will take care of it. This function does however give the possibility to properly handle errors on close.

§Errors

If the underlying child process has already exited a IOError with InvalidInput error is returned

Trait Implementations§

Source§

impl Drop for Ghci

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for Ghci

§

impl RefUnwindSafe for Ghci

§

impl Send for Ghci

§

impl Sync for Ghci

§

impl Unpin for Ghci

§

impl UnwindSafe for Ghci

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.