Skip to main content

NativeFunction

Struct NativeFunction 

Source
pub struct NativeFunction<Args, R = ()> { /* private fields */ }
Expand description

A typed hook that a type attribute, an enum attribute or another registry slot holds: a native function, called directly through the C ABI, or a packed Function registered in its place.

Args is the tuple of argument types and R the result type, as in Fn(Args) -> R: a hook that checks a pair is a NativeFunction<(&MyPairObj,)>, and one that measures it a NativeFunction<(&MyPairObj,), i64>. An argument is a borrowed object (&MyPairObj), a borrowed managed reference (&Array<T>) or &Any, and a native function receives its address, which C++ declares as const MyPairObj*, const Array<T>& or const Any&.

A native function carries no signature at run time, so the slot declares it. Reading a hook with from_any is unsafe, the caller vouching for the signature, and then calling it is safe. Building a hook from a Rust function with into() is safe; storing it, with TypeAttrDef::set_native_fn_attr or EnumDef::set_native_fn_attr, or converting it to Any for another registry with into_any, is unsafe, the caller vouching that the slot’s callers use exactly this signature.

§Examples

use tvm_ffi::reflection::{NativeFunction, TypeAttrColumn, TypeAttrDef};
use tvm_ffi::{rs_object, Object, ObjectArc, ObjectCore, Result};

#[rs_object(type_key = "my_ext.MyPair")]
pub struct MyPairObj {
    base_: Object,
    first: i64,
    second: i64,
}

fn sum(pair: &MyPairObj) -> Result<i64> {
    Ok(pair.first + pair.second)
}

let hook: NativeFunction<(&MyPairObj,), i64> = sum.into();
// SAFETY: `my_ext.sum` holds `i64(const MyPairObj*)` hooks.
unsafe { TypeAttrDef::<MyPairObj>::new().set_native_fn_attr("my_ext.sum", hook)? };

let stored = TypeAttrColumn::lookup("my_ext.sum").unwrap().get(MyPairObj::type_index()).unwrap();
// SAFETY: `my_ext.sum` holds `i64(const MyPairObj*)` hooks.
let sum: NativeFunction<(&MyPairObj,), i64> = unsafe { NativeFunction::from_any(stored)? };
let pair = ObjectArc::new(MyPairObj { base_: Object::new(), first: 1, second: 2 });
assert_eq!(sum.call((&*pair,))?, 3);

Implementations§

Source§

impl<Args: NativeArgs, R: TryFrom<Any>> NativeFunction<Args, R>

Source

pub fn call(&self, args: Args) -> Result<R>

Call the hook with args.

§Arguments
  • args - The arguments, as a tuple such as (&array,).
§Returns

The hook’s result.

§Errors

Returns the error the hook raises, or a TypeError when a packed function returns a value that does not convert to R.

§Panics

Panics if an object argument is not managed, such as an object built on the stack: the hook may retain it.

See also NativeFunction.

Source§

impl<Args, R> NativeFunction<Args, R>

Source

pub unsafe fn from_any(value: Any) -> Result<Self>

Read the hook that value holds: a non-null opaque pointer to a native function, or a packed Function.

§Arguments
  • value - The hook, as its registry slot holds it.
§Returns

The hook, typed with Args and R.

§Errors

Returns a TypeError if value is neither a non-null opaque pointer nor a Function.

§Safety

A native function in value must take Args and return R, as the code that registers it in the slot declares. A packed Function is called through checked conversions and has no such requirement.

See also NativeFunction.

Source

pub unsafe fn into_any(self) -> Any

The hook as a registry slot holds it: the opaque pointer of a native function, or the packed Function.

This is how a registry other than a type or enum attribute stores a hook; from_any reads it back.

§Safety

Every reader of the slot that stores the result calls it with exactly Args and reads R: a native caller calls the function pointer unchecked.

Trait Implementations§

Source§

impl<Args, R> Clone for NativeFunction<Args, R>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F, R: Into<Any>> From<F> for NativeFunction<(), R>
where F: for<'a> Fn() -> Result<R> + Copy + 'static,

Build a native hook from a function item or a closure that captures nothing, with the C ABI adapter that native callers call.

The function takes the arguments as call passes them and returns Result<R>; its error is the error the hook raises. A hook reports failure through its Result and must not panic: a panic aborts the process. A closure that captures a value does not compile.

See also NativeFunction.

Source§

fn from(_function: F) -> Self

Converts to this type from the input type.
Source§

impl<F, A0: NativeArg, R: Into<Any>> From<F> for NativeFunction<(A0,), R>
where F: for<'a> Fn(&'a A0::Target) -> Result<R> + Copy + 'static,

Build a native hook from a function item or a closure that captures nothing, with the C ABI adapter that native callers call.

The function takes the arguments as call passes them and returns Result<R>; its error is the error the hook raises. A hook reports failure through its Result and must not panic: a panic aborts the process. A closure that captures a value does not compile.

See also NativeFunction.

Source§

fn from(_function: F) -> Self

Converts to this type from the input type.
Source§

impl<F, A0: NativeArg, A1: NativeArg, R: Into<Any>> From<F> for NativeFunction<(A0, A1), R>
where F: for<'a> Fn(&'a A0::Target, &'a A1::Target) -> Result<R> + Copy + 'static,

Build a native hook from a function item or a closure that captures nothing, with the C ABI adapter that native callers call.

The function takes the arguments as call passes them and returns Result<R>; its error is the error the hook raises. A hook reports failure through its Result and must not panic: a panic aborts the process. A closure that captures a value does not compile.

See also NativeFunction.

Source§

fn from(_function: F) -> Self

Converts to this type from the input type.
Source§

impl<F, A0: NativeArg, A1: NativeArg, A2: NativeArg, R: Into<Any>> From<F> for NativeFunction<(A0, A1, A2), R>
where F: for<'a> Fn(&'a A0::Target, &'a A1::Target, &'a A2::Target) -> Result<R> + Copy + 'static,

Build a native hook from a function item or a closure that captures nothing, with the C ABI adapter that native callers call.

The function takes the arguments as call passes them and returns Result<R>; its error is the error the hook raises. A hook reports failure through its Result and must not panic: a panic aborts the process. A closure that captures a value does not compile.

See also NativeFunction.

Source§

fn from(_function: F) -> Self

Converts to this type from the input type.
Source§

impl<F, A0: NativeArg, A1: NativeArg, A2: NativeArg, A3: NativeArg, R: Into<Any>> From<F> for NativeFunction<(A0, A1, A2, A3), R>
where F: for<'a> Fn(&'a A0::Target, &'a A1::Target, &'a A2::Target, &'a A3::Target) -> Result<R> + Copy + 'static,

Build a native hook from a function item or a closure that captures nothing, with the C ABI adapter that native callers call.

The function takes the arguments as call passes them and returns Result<R>; its error is the error the hook raises. A hook reports failure through its Result and must not panic: a panic aborts the process. A closure that captures a value does not compile.

See also NativeFunction.

Source§

fn from(_function: F) -> Self

Converts to this type from the input type.
Source§

impl<F, A0: NativeArg, A1: NativeArg, A2: NativeArg, A3: NativeArg, A4: NativeArg, R: Into<Any>> From<F> for NativeFunction<(A0, A1, A2, A3, A4), R>
where F: for<'a> Fn(&'a A0::Target, &'a A1::Target, &'a A2::Target, &'a A3::Target, &'a A4::Target) -> Result<R> + Copy + 'static,

Build a native hook from a function item or a closure that captures nothing, with the C ABI adapter that native callers call.

The function takes the arguments as call passes them and returns Result<R>; its error is the error the hook raises. A hook reports failure through its Result and must not panic: a panic aborts the process. A closure that captures a value does not compile.

See also NativeFunction.

Source§

fn from(_function: F) -> Self

Converts to this type from the input type.
Source§

impl<F, A0: NativeArg, A1: NativeArg, A2: NativeArg, A3: NativeArg, A4: NativeArg, A5: NativeArg, R: Into<Any>> From<F> for NativeFunction<(A0, A1, A2, A3, A4, A5), R>
where F: for<'a> Fn(&'a A0::Target, &'a A1::Target, &'a A2::Target, &'a A3::Target, &'a A4::Target, &'a A5::Target) -> Result<R> + Copy + 'static,

Build a native hook from a function item or a closure that captures nothing, with the C ABI adapter that native callers call.

The function takes the arguments as call passes them and returns Result<R>; its error is the error the hook raises. A hook reports failure through its Result and must not panic: a panic aborts the process. A closure that captures a value does not compile.

See also NativeFunction.

Source§

fn from(_function: F) -> Self

Converts to this type from the input type.
Source§

impl<F, A0: NativeArg, A1: NativeArg, A2: NativeArg, A3: NativeArg, A4: NativeArg, A5: NativeArg, A6: NativeArg, R: Into<Any>> From<F> for NativeFunction<(A0, A1, A2, A3, A4, A5, A6), R>
where F: for<'a> Fn(&'a A0::Target, &'a A1::Target, &'a A2::Target, &'a A3::Target, &'a A4::Target, &'a A5::Target, &'a A6::Target) -> Result<R> + Copy + 'static,

Build a native hook from a function item or a closure that captures nothing, with the C ABI adapter that native callers call.

The function takes the arguments as call passes them and returns Result<R>; its error is the error the hook raises. A hook reports failure through its Result and must not panic: a panic aborts the process. A closure that captures a value does not compile.

See also NativeFunction.

Source§

fn from(_function: F) -> Self

Converts to this type from the input type.
Source§

impl<F, A0: NativeArg, A1: NativeArg, A2: NativeArg, A3: NativeArg, A4: NativeArg, A5: NativeArg, A6: NativeArg, A7: NativeArg, R: Into<Any>> From<F> for NativeFunction<(A0, A1, A2, A3, A4, A5, A6, A7), R>
where F: for<'a> Fn(&'a A0::Target, &'a A1::Target, &'a A2::Target, &'a A3::Target, &'a A4::Target, &'a A5::Target, &'a A6::Target, &'a A7::Target) -> Result<R> + Copy + 'static,

Build a native hook from a function item or a closure that captures nothing, with the C ABI adapter that native callers call.

The function takes the arguments as call passes them and returns Result<R>; its error is the error the hook raises. A hook reports failure through its Result and must not panic: a panic aborts the process. A closure that captures a value does not compile.

See also NativeFunction.

Source§

fn from(_function: F) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<Args, R = ()> !Send for NativeFunction<Args, R>

§

impl<Args, R = ()> !Sync for NativeFunction<Args, R>

§

impl<Args, R> Freeze for NativeFunction<Args, R>

§

impl<Args, R> RefUnwindSafe for NativeFunction<Args, R>

§

impl<Args, R> Unpin for NativeFunction<Args, R>

§

impl<Args, R> UnsafeUnpin for NativeFunction<Args, R>

§

impl<Args, R> UnwindSafe for NativeFunction<Args, R>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where 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<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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> TypeSubsumes<T> for T
where T: ?Sized,