pub struct Function(/* private fields */);
Expand description
The type of a function in Rune.
Functions can be called using call expression syntax, such as <expr>()
.
There are multiple different kind of things which can be coerced into a function in Rune:
- Regular functions.
- Closures (which might or might not capture their environment).
- Built-in constructors for tuple types (tuple structs, tuple variants).
§Examples
// Captures the constructor for the `Some(<value>)` tuple variant.
let build_some = Some;
assert_eq!(build_some(42), Some(42));
fn build(value) {
Some(value)
}
// Captures the function previously defined.
let build_some = build;
assert_eq!(build_some(42), Some(42));
Implementations§
Source§impl Function
impl Function
Sourcepub fn new<F, A, K>(f: F) -> Selfwhere
F: Function<A, K>,
K: FunctionKind,
pub fn new<F, A, K>(f: F) -> Selfwhere
F: Function<A, K>,
K: FunctionKind,
Construct a Function from a Rust closure.
§Examples
use rune::{Hash, Vm};
use rune::runtime::Function;
use std::sync::Arc;
let mut sources = rune::sources! {
entry => {
pub fn main(function) {
function(41)
}
}
};
let unit = rune::prepare(&mut sources).build()?;
let mut vm = Vm::without_runtime(Arc::new(unit));
let function = Function::new(|value: u32| value + 1);
assert_eq!(function.type_hash(), Hash::EMPTY);
let value = vm.call(["main"], (function,))?;
let value: u32 = rune::from_value(value)?;
assert_eq!(value, 42);
Asynchronous functions:
use rune::{Hash, Vm};
use rune::runtime::Function;
use std::sync::Arc;
let mut sources = rune::sources! {
entry => {
pub async fn main(function) {
function(41).await
}
}
};
let unit = rune::prepare(&mut sources).build()?;
let mut vm = Vm::without_runtime(Arc::new(unit));
let function = Function::new(|value: u32| async move { value + 1 });
assert_eq!(function.type_hash(), Hash::EMPTY);
let value = vm.async_call(["main"], (function,)).await?;
let value: u32 = rune::from_value(value)?;
assert_eq!(value, 42);
Sourcepub fn function<F, A, K>(f: F) -> Selfwhere
F: Function<A, K>,
K: FunctionKind,
👎Deprecated: Use Function::new() instead
pub fn function<F, A, K>(f: F) -> Selfwhere
F: Function<A, K>,
K: FunctionKind,
See Function::new
.
Sourcepub fn async_function<F, A>(f: F) -> Selfwhere
F: Function<A, Async>,
👎Deprecated: Use Function::new() instead
pub fn async_function<F, A>(f: F) -> Selfwhere
F: Function<A, Async>,
See Function::function
.
Sourcepub async fn async_send_call<A, T>(&self, args: A) -> VmResult<T>
pub async fn async_send_call<A, T>(&self, args: A) -> VmResult<T>
Perform an asynchronous call over the function which also implements Send.
Sourcepub fn call<T>(&self, args: impl GuardedArgs) -> VmResult<T>where
T: FromValue,
pub fn call<T>(&self, args: impl GuardedArgs) -> VmResult<T>where
T: FromValue,
Perform a call over the function represented by this function pointer.
§Examples
use rune::{Hash, Vm};
use rune::runtime::Function;
use std::sync::Arc;
let mut sources = rune::sources! {
entry => {
fn add(a, b) {
a + b
}
pub fn main() { add }
}
};
let unit = rune::prepare(&mut sources).build()?;
let mut vm = Vm::without_runtime(Arc::new(unit));
let value = vm.call(["main"], ())?;
let value: Function = rune::from_value(value)?;
assert_eq!(value.call::<u32>((1, 2)).into_result()?, 3);
Sourcepub fn type_hash(&self) -> Hash
pub fn type_hash(&self) -> Hash
Type Hash of the underlying function.
§Examples
The type hash of a top-level function matches what you get out of Hash::type_hash.
use rune::{Hash, Vm};
use rune::runtime::Function;
use std::sync::Arc;
let mut sources = rune::sources! {
entry => {
fn pony() { }
pub fn main() { pony }
}
};
let unit = rune::prepare(&mut sources).build()?;
let mut vm = Vm::without_runtime(Arc::new(unit));
let pony = vm.call(["main"], ())?;
let pony: Function = rune::from_value(pony)?;
assert_eq!(pony.type_hash(), Hash::type_hash(["pony"]));
Sourcepub fn into_sync(self) -> Result<SyncFunction, RuntimeError>
pub fn into_sync(self) -> Result<SyncFunction, RuntimeError>
Try to convert into a SyncFunction. This might not be possible if this function is something which is not Sync, like a closure capturing context which is not thread-safe.
§Examples
use rune::{Hash, Vm};
use rune::runtime::Function;
use std::sync::Arc;
let mut sources = rune::sources! {
entry => {
fn pony() { }
pub fn main() { pony }
}
};
let unit = rune::prepare(&mut sources).build()?;
let mut vm = Vm::without_runtime(Arc::new(unit));
let pony = vm.call(["main"], ())?;
let pony: Function = rune::from_value(pony)?;
// This is fine, since `pony` is a free function.
let pony = pony.into_sync()?;
assert_eq!(pony.type_hash(), Hash::type_hash(["pony"]));
The following does not work, because we return a closure which tries to make use of a Generator which is not a constant value.
use rune::{Hash, Vm};
use rune::runtime::Function;
use std::sync::Arc;
let mut sources = rune::sources! {
entry => {
fn generator() {
yield 42;
}
pub fn main() {
let g = generator();
move || {
g.next()
}
}
}
};
let unit = rune::prepare(&mut sources).build()?;
let mut vm = Vm::without_runtime(Arc::new(unit));
let closure = vm.call(["main"], ())?;
let closure: Function = rune::from_value(closure)?;
// This is *not* fine since the returned closure has captured a
// generator which is not a constant value.
assert!(closure.into_sync().is_err());