Struct rquickjs_core::function::Func
source · #[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_eq!(ctx.eval::<String, _>("sum.name")?, "");
// Call/apply works as expected
assert_eq!(ctx.eval::<i32, _>("sum.call(sum, 3, 2)")?, 5);
assert_eq!(ctx.eval::<i32, _>("sum.apply(sum, [3, 2])")?, 5);
// 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");
// Call/apply works as expected
assert_eq!(ctx.eval::<i32, _>("prod.call(prod, 3, 2)")?, 6);
assert_eq!(ctx.eval::<i32, _>("prod.apply(prod, [3, 2])")?, 6);
Tuple Fields§
§0: F
Implementations§
Trait Implementations§
source§impl<'js, F, A, R> IntoJs<'js> for Func<(F, PhantomData<(A, R)>)>where
F: AsFunction<'js, A, R> + 'js,
impl<'js, F, A, R> IntoJs<'js> for Func<(F, PhantomData<(A, R)>)>where F: AsFunction<'js, A, R> + 'js,
Auto Trait Implementations§
impl<F> RefUnwindSafe for Func<F>where F: RefUnwindSafe,
impl<F> Send for Func<F>where F: Send,
impl<F> Sync for Func<F>where F: Sync,
impl<F> Unpin for Func<F>where F: Unpin,
impl<F> UnwindSafe for Func<F>where F: UnwindSafe,
Blanket Implementations§
source§impl<'js, T> AsProperty<'js, T> for Twhere
T: IntoJs<'js>,
impl<'js, T> AsProperty<'js, T> for Twhere T: IntoJs<'js>,
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