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>
impl<Args: NativeArgs, R: TryFrom<Any>> NativeFunction<Args, R>
Sourcepub fn call(&self, args: Args) -> Result<R>
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>
impl<Args, R> NativeFunction<Args, R>
Sourcepub unsafe fn from_any(value: Any) -> Result<Self>
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.
Sourcepub unsafe fn into_any(self) -> Any
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>
impl<Args, R> Clone for NativeFunction<Args, R>
Source§impl<F, R: Into<Any>> From<F> for NativeFunction<(), R>
Build a native hook from a function item or a closure that
captures nothing, with the C ABI adapter that native callers
call.
impl<F, R: Into<Any>> From<F> for NativeFunction<(), R>
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§impl<F, A0: NativeArg, R: Into<Any>> From<F> for NativeFunction<(A0,), R>
Build a native hook from a function item or a closure that
captures nothing, with the C ABI adapter that native callers
call.
impl<F, A0: NativeArg, R: Into<Any>> From<F> for NativeFunction<(A0,), R>
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§impl<F, A0: NativeArg, A1: NativeArg, R: Into<Any>> From<F> for NativeFunction<(A0, A1), R>
Build a native hook from a function item or a closure that
captures nothing, with the C ABI adapter that native callers
call.
impl<F, A0: NativeArg, A1: NativeArg, R: Into<Any>> From<F> for NativeFunction<(A0, A1), R>
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§impl<F, A0: NativeArg, A1: NativeArg, A2: NativeArg, R: Into<Any>> From<F> for NativeFunction<(A0, A1, A2), R>
Build a native hook from a function item or a closure that
captures nothing, with the C ABI adapter that native callers
call.
impl<F, A0: NativeArg, A1: NativeArg, A2: NativeArg, R: Into<Any>> From<F> for NativeFunction<(A0, A1, A2), R>
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§impl<F, A0: NativeArg, A1: NativeArg, A2: NativeArg, A3: NativeArg, R: Into<Any>> From<F> for NativeFunction<(A0, A1, A2, A3), R>
Build a native hook from a function item or a closure that
captures nothing, with the C ABI adapter that native callers
call.
impl<F, A0: NativeArg, A1: NativeArg, A2: NativeArg, A3: NativeArg, R: Into<Any>> From<F> for NativeFunction<(A0, A1, A2, A3), R>
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§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>
Build a native hook from a function item or a closure that
captures nothing, with the C ABI adapter that native callers
call.
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>
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§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>
Build a native hook from a function item or a closure that
captures nothing, with the C ABI adapter that native callers
call.
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>
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§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>
Build a native hook from a function item or a closure that
captures nothing, with the C ABI adapter that native callers
call.
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>
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§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>
Build a native hook from a function item or a closure that
captures nothing, with the C ABI adapter that native callers
call.
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>
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.