Struct evcxr::EvalContext
source · pub struct EvalContext { /* private fields */ }
Implementations§
source§impl EvalContext
impl EvalContext
sourcepub fn new() -> Result<(EvalContext, EvalContextOutputs), Error>
pub fn new() -> Result<(EvalContext, EvalContextOutputs), Error>
Examples found in repository?
examples/example_eval.rs (line 23)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
fn main() -> Result<(), Error> {
// You must call ```evcxr::runtime_hook()``` at the top of main, otherwise
// the library becomes a fork-bomb.
evcxr::runtime_hook();
let (mut context, outputs) = EvalContext::new()?;
context.eval("let mut s = String::new();")?;
context.eval(r#"s.push_str("Hello, ");"#)?;
context.eval(r#"s.push_str("World!");"#)?;
context.eval(r#"println!("{}", s);"#)?;
// For this trivial example, we just receive a single line of output from
// the code that was run. In a more complex case, we'd likely want to have
// separate threads waiting for output on both stdout and stderr.
if let Ok(line) = outputs.stdout.recv() {
println!("{line}");
}
Ok(())
}
pub fn with_subprocess_command(
subprocess_command: Command
) -> Result<(EvalContext, EvalContextOutputs), Error>
sourcepub fn state(&self) -> ContextState
pub fn state(&self) -> ContextState
Returns a new context state, suitable for passing to eval
after
optionally calling things like add_dep
.
sourcepub fn eval(&mut self, code: &str) -> Result<EvalOutputs, Error>
pub fn eval(&mut self, code: &str) -> Result<EvalOutputs, Error>
Evaluates the supplied Rust code.
Examples found in repository?
examples/example_eval.rs (line 24)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
fn main() -> Result<(), Error> {
// You must call ```evcxr::runtime_hook()``` at the top of main, otherwise
// the library becomes a fork-bomb.
evcxr::runtime_hook();
let (mut context, outputs) = EvalContext::new()?;
context.eval("let mut s = String::new();")?;
context.eval(r#"s.push_str("Hello, ");"#)?;
context.eval(r#"s.push_str("World!");"#)?;
context.eval(r#"println!("{}", s);"#)?;
// For this trivial example, we just receive a single line of output from
// the code that was run. In a more complex case, we'd likely want to have
// separate threads waiting for output on both stdout and stderr.
if let Ok(line) = outputs.stdout.recv() {
println!("{line}");
}
Ok(())
}