Struct rquickjs_core::prelude::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§

source§

impl<N, F, A, R> Func<(N, F, PhantomData<(A, R)>)>

source

pub fn new(name: N, func: F) -> Self

Trait Implementations§

source§

impl<F> AsRef<F> for Func<F>

source§

fn as_ref(&self) -> &F

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

impl<F> Deref for Func<F>

§

type Target = F

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<F, A, R> From<F> for Func<(F, PhantomData<(A, R)>)>

source§

fn from(func: F) -> Self

Converts to this type from the input type.
source§

impl<'js, F, A, R> IntoJs<'js> for Func<(F, PhantomData<(A, R)>)>where F: AsFunction<'js, A, R> + 'js,

source§

fn into_js(self, ctx: Ctx<'js>) -> Result<Value<'js>>

source§

impl<'js, N, F, A, R> IntoJs<'js> for Func<(N, F, PhantomData<(A, R)>)>where N: AsRef<str>, F: AsFunction<'js, A, R> + 'js,

source§

fn into_js(self, ctx: Ctx<'js>) -> Result<Value<'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<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<'js, T> AsProperty<'js, T> for Twhere T: IntoJs<'js>,

source§

fn config( self, ctx: Ctx<'js> ) -> Result<(i32, Value<'js>, Value<'js>, Value<'js>), Error>

Available on crate feature properties only.
Property configuration Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<'js, T> IntoInput<'js> for Twhere T: IntoJs<'js>,

source§

fn num_args(&self) -> usize

Get actual number of arguments
source§

fn into_input(self, input: &mut CallInput<'js>) -> Result<(), Error>

Put the value into inputs
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> ParallelSend for T