#[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

Implementations

Trait Implementations

Converts this type into a shared reference of the (usually inferred) input type.

The resulting type after dereferencing.

Dereferences the value.

Converts to this type from the input type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Converts to this type from the input type.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.