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
impl Ghci
Sourcepub fn eval(&mut self, input: &str) -> Result<EvalOutput>
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
Timeoutif the evaluation timeout (set byGhci::set_timeout) is reached before the evaluation completes. - Returns a
IOErrorwhen encounters an IO error on theghcisubprocessstdin,stdout, orstderr. - Returns a
PollErrorwhen waiting for output, if theghcisubprocessstdoutorstderris closed (upon a crash for example)
Sourcepub fn set_timeout(&mut self, timeout: Option<Duration>)
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.
Sourcepub fn import(&mut self, modules: &[&str]) -> Result<()>
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
Sourcepub fn close(self) -> Result<()>
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