Struct Function

#[repr(transparent)]
pub struct Function<T: JsFunction = fn(JsValue, JsValue, JsValue, JsValue, JsValue, JsValue, JsValue, JsValue) -> JsValue> { /* private fields */ }

Function represents any generic Function in JS, by treating all arguments as JsValue.

It takes a generic parameter of phantom type fn (Arg1, ..., Argn) -> Ret which is used to type the JS function. For example, Function<fn () -> Number> represents a function taking no arguments that returns a number.

The 8 generic argument parameters (Arg1 through Arg8) are the argument types. Arguments not provided enable strict arity checking at compile time.

A void function is represented by fn (Arg) -> Undefined, and not the () unit type. This is because generics must be based on JS values in the JS generic type system.

The default without any parameters is as a void function - no arguments, Undefined return.

The default generic for Function is fn (JsValue, JsValue, ...) -> JsValue, representing any function, since all functions safely upcast into this function.

Arity Enforcement

It is not possible to use call4 or bind4 on a function that does not have at least 4 arguments — the compiler will reject this because only arguments that are not None support the trait bound for ErasableGeneric.

Examples

// A function taking no args, returning Number
let f: Function<Number> = get_some_fn();

// A function taking (String, Number) and returning Boolean
let f: Function<Boolean, String, Number> = get_some_fn();

### Upcasting

To pass a typed `Function` where a different generic Function is expected, `upcast()` may be used
to convert into any generic `Function` at zero cost with type-safety.

MDN documentation (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)

Implementations

impl Function

fn try_from(val: &JsValue) -> Option<&Function>

Returns the Function value of this JS value if it's an instance of a function.

If this JS value is not an instance of a function then this returns None.

impl<Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, Arg2: JsGeneric, Arg3: JsGeneric, Arg4: JsGeneric, Arg5: JsGeneric, Arg6: JsGeneric, Arg7: JsGeneric, Arg8: JsGeneric, F: JsFunction8<Ret = Ret, Arg1 = Arg1, Arg2 = Arg2, Arg3 = Arg3, Arg4 = Arg4, Arg5 = Arg5, Arg6 = Arg6, Arg7 = Arg7, Arg8 = Arg8>,

fn bind8(&self, context: &JsValue, arg1: &Arg1, arg2: &Arg2, arg3: &Arg3, arg4: &Arg4, arg5: &Arg5, arg6: &Arg6, arg7: &Arg7, arg8: &Arg8) -> Function<<F as JsFunction8>::Bind8>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Arg2: ErasableGenericBorrow<JsValue>,
    Arg3: ErasableGenericBorrow<JsValue>,
    Arg4: ErasableGenericBorrow<JsValue>,
    Arg5: ErasableGenericBorrow<JsValue>,
    Arg6: ErasableGenericBorrow<JsValue>,
    Arg7: ErasableGenericBorrow<JsValue>,
    Arg8: ErasableGenericBorrow<JsValue>,
    Function<<F as JsFunction8>::Bind8>: ErasableGenericOwn<Function<<fn(JsValue, JsValue, JsValue, JsValue, JsValue, JsValue, JsValue, JsValue) -> JsValue as JsFunction8>::Bind8>>,

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Note: See Function::bindn for arbitrary binding with function arity checking.

MDN documentation

impl<Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, Arg2: JsGeneric, Arg3: JsGeneric, Arg4: JsGeneric, Arg5: JsGeneric, Arg6: JsGeneric, Arg7: JsGeneric, Arg8: JsGeneric, F: JsFunction8<Ret = Ret, Arg1 = Arg1, Arg2 = Arg2, Arg3 = Arg3, Arg4 = Arg4, Arg5 = Arg5, Arg6 = Arg6, Arg7 = Arg7, Arg8 = Arg8>,

fn bind9(&self, context: &JsValue, arg1: &Arg1, arg2: &Arg2, arg3: &Arg3, arg4: &Arg4, arg5: &Arg5, arg6: &Arg6, arg7: &Arg7, arg8: &Arg8, arg9: &JsValue) -> Function<fn() -> Ret>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Arg2: ErasableGenericBorrow<JsValue>,
    Arg3: ErasableGenericBorrow<JsValue>,
    Arg4: ErasableGenericBorrow<JsValue>,
    Arg5: ErasableGenericBorrow<JsValue>,
    Arg6: ErasableGenericBorrow<JsValue>,
    Arg7: ErasableGenericBorrow<JsValue>,
    Arg8: ErasableGenericBorrow<JsValue>,
    Function<fn() -> Ret>: ErasableGenericOwn<Function<fn() -> JsValue>>,

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Note: See Function::bindn for arbitrary binding with function arity checking.

MDN documentation

impl<Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, Arg2: JsGeneric, Arg3: JsGeneric, Arg4: JsGeneric, Arg5: JsGeneric, Arg6: JsGeneric, Arg7: JsGeneric, Arg8: JsGeneric, F: JsFunction8<Ret = Ret, Arg1 = Arg1, Arg2 = Arg2, Arg3 = Arg3, Arg4 = Arg4, Arg5 = Arg5, Arg6 = Arg6, Arg7 = Arg7, Arg8 = Arg8>,

fn call8(&self, context: &JsValue, arg1: &Arg1, arg2: &Arg2, arg3: &Arg3, arg4: &Arg4, arg5: &Arg5, arg6: &Arg6, arg7: &Arg7, arg8: &Arg8) -> Result<Ret, JsValue>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Arg2: ErasableGenericBorrow<JsValue>,
    Arg3: ErasableGenericBorrow<JsValue>,
    Arg4: ErasableGenericBorrow<JsValue>,
    Arg5: ErasableGenericBorrow<JsValue>,
    Arg6: ErasableGenericBorrow<JsValue>,
    Arg7: ErasableGenericBorrow<JsValue>,
    Arg8: ErasableGenericBorrow<JsValue>,
    Ret: ErasableGenericOwn<JsValue>,

The call() method calls a function with a given this value and arguments provided individually.

Note: Use call() to get exact arity and also checked generic type casting.

MDN documentation

impl<Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, Arg2: JsGeneric, Arg3: JsGeneric, Arg4: JsGeneric, Arg5: JsGeneric, Arg6: JsGeneric, Arg7: JsGeneric, Arg8: JsGeneric, F: JsFunction8<Ret = Ret, Arg1 = Arg1, Arg2 = Arg2, Arg3 = Arg3, Arg4 = Arg4, Arg5 = Arg5, Arg6 = Arg6, Arg7 = Arg7, Arg8 = Arg8>,

fn call9(&self, context: &JsValue, arg1: &Arg1, arg2: &Arg2, arg3: &Arg3, arg4: &Arg4, arg5: &Arg5, arg6: &Arg6, arg7: &Arg7, arg8: &Arg8, arg9: &JsValue) -> Result<Ret, JsValue>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Arg2: ErasableGenericBorrow<JsValue>,
    Arg3: ErasableGenericBorrow<JsValue>,
    Arg4: ErasableGenericBorrow<JsValue>,
    Arg5: ErasableGenericBorrow<JsValue>,
    Arg6: ErasableGenericBorrow<JsValue>,
    Arg7: ErasableGenericBorrow<JsValue>,
    Arg8: ErasableGenericBorrow<JsValue>,
    Ret: ErasableGenericOwn<JsValue>,

The call() method calls a function with a given this value and arguments provided individually.

Note: Use call() to get exact arity and also checked generic type casting.

MDN documentation

impl<Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, Arg2: JsGeneric, Arg3: JsGeneric, Arg4: JsGeneric, Arg5: JsGeneric, Arg6: JsGeneric, Arg7: JsGeneric, F: JsFunction<Ret = Ret> + JsFunction7<Arg1 = Arg1, Arg2 = Arg2, Arg3 = Arg3, Arg4 = Arg4, Arg5 = Arg5, Arg6 = Arg6, Arg7 = Arg7>,

fn call7(&self, context: &JsValue, arg1: &Arg1, arg2: &Arg2, arg3: &Arg3, arg4: &Arg4, arg5: &Arg5, arg6: &Arg6, arg7: &Arg7) -> Result<Ret, JsValue>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Arg2: ErasableGenericBorrow<JsValue>,
    Arg3: ErasableGenericBorrow<JsValue>,
    Arg4: ErasableGenericBorrow<JsValue>,
    Arg5: ErasableGenericBorrow<JsValue>,
    Arg6: ErasableGenericBorrow<JsValue>,
    Arg7: ErasableGenericBorrow<JsValue>,
    Ret: ErasableGenericOwn<JsValue>,

The call() method calls a function with a given this value and arguments provided individually.

Note: Use call() to get exact arity and also checked generic type casting.

MDN documentation

impl<Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, Arg2: JsGeneric, Arg3: JsGeneric, Arg4: JsGeneric, Arg5: JsGeneric, Arg6: JsGeneric, Arg7: JsGeneric, F: JsFunction7<Ret = Ret, Arg1 = Arg1, Arg2 = Arg2, Arg3 = Arg3, Arg4 = Arg4, Arg5 = Arg5, Arg6 = Arg6, Arg7 = Arg7>,

fn bind7(&self, context: &JsValue, arg1: &Arg1, arg2: &Arg2, arg3: &Arg3, arg4: &Arg4, arg5: &Arg5, arg6: &Arg6, arg7: &Arg7) -> Function<<F as JsFunction7>::Bind7>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Arg2: ErasableGenericBorrow<JsValue>,
    Arg3: ErasableGenericBorrow<JsValue>,
    Arg4: ErasableGenericBorrow<JsValue>,
    Arg5: ErasableGenericBorrow<JsValue>,
    Arg6: ErasableGenericBorrow<JsValue>,
    Arg7: ErasableGenericBorrow<JsValue>,
    Function<<F as JsFunction7>::Bind7>: ErasableGenericOwn<Function<<fn(JsValue, JsValue, JsValue, JsValue, JsValue, JsValue, JsValue) -> JsValue as JsFunction7>::Bind7>>,

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Note: See Function::bindn for arbitrary binding with function arity checking.

MDN documentation

impl<Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, Arg2: JsGeneric, Arg3: JsGeneric, Arg4: JsGeneric, Arg5: JsGeneric, Arg6: JsGeneric, F: JsFunction<Ret = Ret> + JsFunction6<Arg1 = Arg1, Arg2 = Arg2, Arg3 = Arg3, Arg4 = Arg4, Arg5 = Arg5, Arg6 = Arg6>,

fn call6(&self, context: &JsValue, arg1: &Arg1, arg2: &Arg2, arg3: &Arg3, arg4: &Arg4, arg5: &Arg5, arg6: &Arg6) -> Result<Ret, JsValue>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Arg2: ErasableGenericBorrow<JsValue>,
    Arg3: ErasableGenericBorrow<JsValue>,
    Arg4: ErasableGenericBorrow<JsValue>,
    Arg5: ErasableGenericBorrow<JsValue>,
    Arg6: ErasableGenericBorrow<JsValue>,
    Ret: ErasableGenericOwn<JsValue>,

The call() method calls a function with a given this value and arguments provided individually.

Note: Use call() to get exact arity and also checked generic type casting.

MDN documentation

impl<Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, Arg2: JsGeneric, Arg3: JsGeneric, Arg4: JsGeneric, Arg5: JsGeneric, Arg6: JsGeneric, F: JsFunction6<Ret = Ret, Arg1 = Arg1, Arg2 = Arg2, Arg3 = Arg3, Arg4 = Arg4, Arg5 = Arg5, Arg6 = Arg6>,

fn bind6(&self, context: &JsValue, arg1: &Arg1, arg2: &Arg2, arg3: &Arg3, arg4: &Arg4, arg5: &Arg5, arg6: &Arg6) -> Function<<F as JsFunction6>::Bind6>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Arg2: ErasableGenericBorrow<JsValue>,
    Arg3: ErasableGenericBorrow<JsValue>,
    Arg4: ErasableGenericBorrow<JsValue>,
    Arg5: ErasableGenericBorrow<JsValue>,
    Arg6: ErasableGenericBorrow<JsValue>,
    Function<<F as JsFunction6>::Bind6>: ErasableGenericOwn<Function<<fn(JsValue, JsValue, JsValue, JsValue, JsValue, JsValue) -> JsValue as JsFunction6>::Bind6>>,

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Note: See Function::bindn for arbitrary binding with function arity checking.

MDN documentation

impl<Arg1, Arg2, Arg3, Arg4, Arg5, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, Arg2: JsGeneric, Arg3: JsGeneric, Arg4: JsGeneric, Arg5: JsGeneric, F: JsFunction<Ret = Ret> + JsFunction5<Arg1 = Arg1, Arg2 = Arg2, Arg3 = Arg3, Arg4 = Arg4, Arg5 = Arg5>,

fn call5(&self, context: &JsValue, arg1: &Arg1, arg2: &Arg2, arg3: &Arg3, arg4: &Arg4, arg5: &Arg5) -> Result<Ret, JsValue>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Arg2: ErasableGenericBorrow<JsValue>,
    Arg3: ErasableGenericBorrow<JsValue>,
    Arg4: ErasableGenericBorrow<JsValue>,
    Arg5: ErasableGenericBorrow<JsValue>,
    Ret: ErasableGenericOwn<JsValue>,

The call() method calls a function with a given this value and arguments provided individually.

Note: Use call() to get exact arity and also checked generic type casting.

MDN documentation

impl<Arg1, Arg2, Arg3, Arg4, Arg5, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, Arg2: JsGeneric, Arg3: JsGeneric, Arg4: JsGeneric, Arg5: JsGeneric, F: JsFunction5<Ret = Ret, Arg1 = Arg1, Arg2 = Arg2, Arg3 = Arg3, Arg4 = Arg4, Arg5 = Arg5>,

fn bind5(&self, context: &JsValue, arg1: &Arg1, arg2: &Arg2, arg3: &Arg3, arg4: &Arg4, arg5: &Arg5) -> Function<<F as JsFunction5>::Bind5>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Arg2: ErasableGenericBorrow<JsValue>,
    Arg3: ErasableGenericBorrow<JsValue>,
    Arg4: ErasableGenericBorrow<JsValue>,
    Arg5: ErasableGenericBorrow<JsValue>,
    Function<<F as JsFunction5>::Bind5>: ErasableGenericOwn<Function<<fn(JsValue, JsValue, JsValue, JsValue, JsValue) -> JsValue as JsFunction5>::Bind5>>,

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Note: See Function::bindn for arbitrary binding with function arity checking.

MDN documentation

impl<Arg1, Arg2, Arg3, Arg4, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, Arg2: JsGeneric, Arg3: JsGeneric, Arg4: JsGeneric, F: JsFunction<Ret = Ret> + JsFunction4<Arg1 = Arg1, Arg2 = Arg2, Arg3 = Arg3, Arg4 = Arg4>,

fn call4(&self, context: &JsValue, arg1: &Arg1, arg2: &Arg2, arg3: &Arg3, arg4: &Arg4) -> Result<Ret, JsValue>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Arg2: ErasableGenericBorrow<JsValue>,
    Arg3: ErasableGenericBorrow<JsValue>,
    Arg4: ErasableGenericBorrow<JsValue>,
    Ret: ErasableGenericOwn<JsValue>,

The call() method calls a function with a given this value and arguments provided individually.

Note: Use call() to get exact arity and also checked generic type casting.

MDN documentation

impl<Arg1, Arg2, Arg3, Arg4, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, Arg2: JsGeneric, Arg3: JsGeneric, Arg4: JsGeneric, F: JsFunction4<Ret = Ret, Arg1 = Arg1, Arg2 = Arg2, Arg3 = Arg3, Arg4 = Arg4>,

fn bind4(&self, context: &JsValue, arg1: &Arg1, arg2: &Arg2, arg3: &Arg3, arg4: &Arg4) -> Function<<F as JsFunction4>::Bind4>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Arg2: ErasableGenericBorrow<JsValue>,
    Arg3: ErasableGenericBorrow<JsValue>,
    Arg4: ErasableGenericBorrow<JsValue>,
    Function<<F as JsFunction4>::Bind4>: ErasableGenericOwn<Function<<fn(JsValue, JsValue, JsValue, JsValue) -> JsValue as JsFunction4>::Bind4>>,

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Note: See Function::bindn for arbitrary binding with function arity checking.

MDN documentation

impl<Arg1, Arg2, Arg3, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, Arg2: JsGeneric, Arg3: JsGeneric, F: JsFunction<Ret = Ret> + JsFunction3<Arg1 = Arg1, Arg2 = Arg2, Arg3 = Arg3>,

fn call3(&self, context: &JsValue, arg1: &Arg1, arg2: &Arg2, arg3: &Arg3) -> Result<Ret, JsValue>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Arg2: ErasableGenericBorrow<JsValue>,
    Arg3: ErasableGenericBorrow<JsValue>,
    Ret: ErasableGenericOwn<JsValue>,

The call() method calls a function with a given this value and arguments provided individually.

Note: Use call() to get exact arity and also checked generic type casting.

MDN documentation

impl<Arg1, Arg2, Arg3, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, Arg2: JsGeneric, Arg3: JsGeneric, F: JsFunction3<Ret = Ret, Arg1 = Arg1, Arg2 = Arg2, Arg3 = Arg3>,

fn bind3(&self, context: &JsValue, arg1: &Arg1, arg2: &Arg2, arg3: &Arg3) -> Function<<F as JsFunction3>::Bind3>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Arg2: ErasableGenericBorrow<JsValue>,
    Arg3: ErasableGenericBorrow<JsValue>,
    Function<<F as JsFunction3>::Bind3>: ErasableGenericOwn<Function<<fn(JsValue, JsValue, JsValue) -> JsValue as JsFunction3>::Bind3>>,

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Note: See Function::bindn for arbitrary binding with function arity checking.

MDN documentation

impl<Arg1, Arg2, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, Arg2: JsGeneric, F: JsFunction<Ret = Ret> + JsFunction1<Arg1 = Arg1> + JsFunction2<Arg2 = Arg2>,

fn call2(&self, context: &JsValue, arg1: &Arg1, arg2: &Arg2) -> Result<Ret, JsValue>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Arg2: ErasableGenericBorrow<JsValue>,
    Ret: ErasableGenericOwn<JsValue>,

The call() method calls a function with a given this value and arguments provided individually.

Note: Use call() to get exact arity and also checked generic type casting.

MDN documentation

impl<Arg1, Arg2, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, Arg2: JsGeneric, F: JsFunction2<Ret = Ret, Arg1 = Arg1, Arg2 = Arg2>,

fn bind2(&self, context: &JsValue, arg1: &Arg1, arg2: &Arg2) -> Function<<F as JsFunction2>::Bind2>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Arg2: ErasableGenericBorrow<JsValue>,
    Function<<F as JsFunction2>::Bind2>: ErasableGenericOwn<Function<<fn(JsValue, JsValue) -> JsValue as JsFunction2>::Bind2>>,

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Note: See Function::bindn for arbitrary binding with function arity checking.

MDN documentation

impl<Arg1, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, F: JsFunction<Ret = Ret> + JsFunction1<Arg1 = Arg1>,

fn call1(&self, context: &JsValue, arg1: &Arg1) -> Result<Ret, JsValue>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Ret: ErasableGenericOwn<JsValue>,

The call() method calls a function with a given this value and arguments provided individually.

Note: Use call() to get exact arity and also checked generic type casting.

MDN documentation

impl<Arg1, F, Ret> Function<F> where Ret: JsGeneric, Arg1: JsGeneric, F: JsFunction1<Ret = Ret, Arg1 = Arg1>,

fn bind1(&self, context: &JsValue, arg1: &Arg1) -> Function<<F as JsFunction1>::Bind1>
where
    Arg1: ErasableGenericBorrow<JsValue>,
    Function<<F as JsFunction1>::Bind1>: ErasableGenericOwn<Function<<fn(JsValue) -> JsValue as JsFunction1>::Bind1>>,

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Note: See Function::bindn for arbitrary binding with function arity checking.

MDN documentation

impl<F, Ret> Function<F> where Ret: JsGeneric, F: JsFunction<Ret = Ret>,

fn call0(&self, context: &JsValue) -> Result<Ret, JsValue>
where
    Ret: ErasableGenericOwn<JsValue>,

The call() method calls a function with a given this value and arguments provided individually.

Note: Use call() to get exact arity and also checked generic type casting.

MDN documentation

impl<F: JsFunction> Function<F>

fn closure_ref<'a, C>(closure: &'a ScopedClosure<'_, C>) -> &'a Self
where
    F: FunctionIntoClosure,
    C: WasmClosure + ?Sized,
    <F as FunctionIntoClosure>::ClosureTypeMut: UpcastFrom<<C as WasmClosure>::AsMut>,

Convert a borrowed ScopedClosure into a typed JavaScript Function reference.

The conversion is a direct type-safe conversion and upcast of a closure into its corresponding typed JavaScript Function, based on covariance and contravariance Upcast trait hierarchy.

For transferring ownership to JS, use Function::from_closure.

fn from_closure<C>(closure: ScopedClosure<'static, C>) -> Self
where
    F: FunctionIntoClosure,
    C: WasmClosure + ?Sized,
    <F as FunctionIntoClosure>::ClosureTypeMut: UpcastFrom<<C as WasmClosure>::AsMut>,

Convert a Rust closure into a typed JavaScript Function.

This function releases ownership of the closure to JS, and provides an owned function handle for the same closure.

The conversion is a direct type-safe conversion and upcast of a closure into its corresponding typed JavaScript Function, based on covariance and contravariance Upcast trait hierarchy.

This method is only supported for static closures which do not have borrowed lifetime data, and thus can be released into JS.

For borrowed closures, which cannot cede ownership to JS, instead use Function::closure_ref.

impl<T> Function<T> where T: JsFunction,

fn name(&self) -> JsString

A Function object's read-only name property indicates the function's name as specified when it was created or "anonymous" for functions created anonymously.

MDN documentation

impl<T> Function<T> where T: JsFunction,

fn bind(&self, context: &JsValue) -> Function<T>
where
    Function<T>: ErasableGenericOwn<Function<fn() -> JsValue>>,

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

MDN documentation

impl<T> Function<T> where T: JsFunction,

fn bind0(&self, context: &JsValue) -> Function<T>
where
    Function<T>: ErasableGenericOwn<Function<fn() -> JsValue>>,

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Note: See Function::bindn for arbitrary binding with function arity checking.

MDN documentation

impl<T> Function<T> where T: JsFunction,

fn to_string(&self) -> JsString

The toString() method returns a string representing the source code of the function.

MDN documentation

impl<T> Function<T> where T: JsFunction,

fn apply(&self, context: &JsValue, args: &Array) -> Result<<T as JsFunction>::Ret, JsValue>
where
    <T as JsFunction>::Ret: ErasableGenericOwn<<fn() -> JsValue as JsFunction>::Ret>,

The apply() method calls a function with a given this value, and arguments provided as an array (or an array-like object).

MDN documentation

impl<T> Function<T> where T: JsFunction,

fn length(&self) -> u32

The length property indicates the number of arguments expected by the function.

MDN documentation

impl<T: JsFunction> Function<T>

fn len(&self) -> usize

Get the static arity of this function type.

fn is_empty(&self) -> bool

Returns true if this is a zero-argument function.

impl<T: JsFunction> Function<T>

fn call<Args: JsArgs<T>>(&self, context: &JsValue, args: Args) -> Result<T::Ret, JsValue>

The call() method calls a function with a given this value and arguments provided as a tuple.

This method accepts a tuple of references matching the function's argument types.

Example

// 0-arg function
let f: Function<fn() -> Number> = get_fn();
let result = f.call(&JsValue::NULL, ())?;

// 1-arg function (note trailing comma for 1-tuple)
let f: Function<fn(JsString) -> Number> = get_fn();
let result = f.call(&JsValue::NULL, (&name,))?;

// 2-arg function
let f: Function<fn(JsString, Boolean) -> Number> = get_fn();
let result = f.call(&JsValue::NULL, (&name, &flag))?;

MDN documentation

fn bindn<Args: JsArgs<T>>(&self, context: &JsValue, args: Args) -> Args::BindOutput

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

This method accepts a tuple of references to bind.

Example

let f: Function<fn(JsString, Boolean) -> Number> = get_fn();

// Bind no args - same signature
let bound: Function<fn(JsString, Boolean) -> Number> = f.bind(&ctx, ());

// Bind one arg (use 1-tuple of references)
let bound: Function<fn(Boolean) -> Number> = f.bind(&ctx, (&my_string,));

// Bind two args - becomes 0-arg function
let bound: Function<fn() -> Number> = f.bind(&ctx, (&my_string, &my_bool));

MDN documentation

Trait Implementations

impl From<JsValue> for Function

fn from(obj: JsValue) -> Self

impl<T: Clone + JsFunction> Clone for Function<T>

fn clone(&self) -> Self

impl<T: Debug + JsFunction> Debug for Function<T>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl<T: Eq + JsFunction> Eq for Function<T>

impl<T: JsFunction> AsRef<Function<T>> for Function<T>

fn as_ref(&self) -> &Function<T>

impl<T: JsFunction> AsRef<JsValue> for Function<T>

fn as_ref(&self) -> &JsValue

impl<T: JsFunction> AsRef<Object> for Function<T>

fn as_ref(&self) -> &Object

impl<T: JsFunction> Deref for Function<T>

type Target = Object;
fn deref(&self) -> &Object

impl<T: JsFunction> ErasableGeneric for Function<T>

type Repr = JsValue;

impl<T: JsFunction> FromWasmAbi for Function<T>

type Abi = <JsValue as FromWasmAbi>::Abi;
unsafe fn from_abi(js: Self::Abi) -> Self

impl<T: JsFunction> IntoJsGeneric for Function<T> where Function<T>: JsGeneric,

type JsCanon = Function<T>;
fn to_js(self) -> Function<T>

impl<T: JsFunction> IntoWasmAbi for Function<T>

type Abi = <JsValue as IntoWasmAbi>::Abi;
fn into_abi(self) -> Self::Abi

impl<T: JsFunction> JsCast for Function<T>

fn instanceof(val: &JsValue) -> bool
fn is_type_of(val: &JsValue) -> bool
fn unchecked_from_js(val: JsValue) -> Self
fn unchecked_from_js_ref(val: &JsValue) -> &Self

impl<T: JsFunction> LongRefFromWasmAbi for Function<T>

type Abi = <JsValue as LongRefFromWasmAbi>::Abi;
type Anchor = Function<T>;
unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor

impl<T: JsFunction> OptionFromWasmAbi for Function<T>

fn is_none(abi: &Self::Abi) -> bool

impl<T: JsFunction> OptionIntoWasmAbi for Function<T>

fn none() -> Self::Abi

impl<T: JsFunction> Promising for Function<T>

type Resolution = Function<T>;

impl<T: JsFunction> RefFromWasmAbi for Function<T>

type Abi = <JsValue as RefFromWasmAbi>::Abi;
type Anchor = ManuallyDrop<Function<T>>;
unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor

impl<T: JsFunction> WasmDescribe for Function<T>

fn describe()

impl<T: JsFunction, U> UpcastFrom<Function<T>> for Function<U> where U: UpcastFrom<T> + JsFunction,

impl<T: PartialEq + JsFunction> PartialEq for Function<T>

fn eq(&self, other: &Self) -> bool

impl<T: PartialEq + JsFunction> StructuralPartialEq for Function<T>

Auto Trait Implementations

impl<T> Freeze for Function<T> where PhantomData<(T,)>: Freeze,

impl<T> RefUnwindSafe for Function<T> where PhantomData<(T,)>: RefUnwindSafe,

impl<T> Send for Function<T> where PhantomData<(T,)>: Send,

impl<T> Sync for Function<T> where PhantomData<(T,)>: Sync,

impl<T> Unpin for Function<T> where PhantomData<(T,)>: Unpin,

impl<T> UnsafeUnpin for Function<T> where PhantomData<(T,)>: UnsafeUnpin,

impl<T> UnwindSafe for Function<T> where PhantomData<(T,)>: UnwindSafe,

Blanket Implementations

impl<P, T> Receiver for Function<T> where P: Deref<Target = T> + ?Sized, T: ?Sized,

impl<S, T> Upcast<T> for Function<T> where T: UpcastFrom<S> + ?Sized, S: ?Sized,

impl<T> Any for Function<T> where T: 'static + ?Sized,

impl<T> Borrow<T> for Function<T> where T: ?Sized,

impl<T> BorrowMut<T> for Function<T> where T: ?Sized,

impl<T> CloneToUninit for Function<T> where T: Clone,

impl<T> From<T> for Function<T>

impl<T> JsGeneric for Function<T> where T: ErasableGeneric<Repr = JsValue> + UpcastFrom<T> + Upcast<JsValue> + JsCast + 'static,

impl<T> ReturnWasmAbi for Function<T> where T: IntoWasmAbi,

impl<T> ToOwned for Function<T> where T: Clone,

impl<T> TryFromJsValue for Function<T> where T: JsCast,

impl<T> VectorFromWasmAbi for Function<T> where T: ErasableGeneric<Repr = JsValue> + WasmDescribe,

impl<T> VectorIntoWasmAbi for Function<T> where T: ErasableGeneric<Repr = JsValue> + WasmDescribe,

impl<T> VectorRefIntoWasmAbi for Function<T> where &'a T: for<'a> Into<JsValue>,

impl<T, U> Into<U> for Function<T> where U: From<T>,

impl<T, U> TryFrom<U> for Function<T> where U: Into<T>,

impl<T, U> TryInto<U> for Function<T> where U: TryFrom<T>,