pub struct ExternMap { /* private fields */ }Expand description
HashMap-backed Externs impl for host-side Rust closures.
use flow_ir_core::{eval_expr_with_externs, EvalError, Expr, ExternMap};
use serde_json::{json, Value};
let mut externs = ExternMap::new();
externs.register("math.sqrt", |args: &[Value]| {
let x = args[0].as_f64().ok_or_else(|| EvalError::ExternError {
ref_: "math.sqrt".into(),
msg: "expected number".into(),
})?;
Ok(json!(x.sqrt()))
});
let expr: Expr = serde_json::from_value(json!({
"op": "call_extern", "ref": "math.sqrt",
"args": [{ "op": "lit", "value": 9.0 }],
})).unwrap();
let out = eval_expr_with_externs(&expr, &json!({}), &externs).unwrap();
assert_eq!(out, json!(3.0));Implementations§
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for ExternMap
impl !UnwindSafe for ExternMap
impl Freeze for ExternMap
impl Send for ExternMap
impl Sync for ExternMap
impl Unpin for ExternMap
impl UnsafeUnpin for ExternMap
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more