Struct rquickjs_core::Func [−][src]
#[repr(transparent)]pub struct Func<F>(pub F);
Expand description
The wrapper for function to convert is into JS
The Rust functions should be wrapped to convert it to JS using IntoJs
trait.
// Anonymous function
ctx.globals().set("sum", Func::from(|a: i32, b: i32| a + b))?;
assert_eq!(ctx.eval::<i32, _>("sum(3, 2)")?, 5);
assert_eq!(ctx.eval::<usize, _>("sum.length")?, 2);
assert!(ctx.eval::<Option<String>, _>("sum.name")?.is_none());
// Named function
ctx.globals().set("prod", Func::new("multiply", |a: i32, b: i32| a * b))?;
assert_eq!(ctx.eval::<i32, _>("prod(3, 2)")?, 6);
assert_eq!(ctx.eval::<usize, _>("prod.length")?, 2);
assert_eq!(ctx.eval::<String, _>("prod.name")?, "multiply");
Tuple Fields
0: F