pub struct FunctionDefinition { /* private fields */ }
Expand description
Builder pattern for defining custom Engine’s functions
Implementations§
Source§impl FunctionDefinition
impl FunctionDefinition
Sourcepub fn new<Name: Into<String>, Dummy, Params, ReturnValue, Function, AbstractFunction: ToAbstractFunction<Params, ReturnValue, Function, Dummy>>(
function_name: Name,
function: AbstractFunction,
) -> Self
pub fn new<Name: Into<String>, Dummy, Params, ReturnValue, Function, AbstractFunction: ToAbstractFunction<Params, ReturnValue, Function, Dummy>>( function_name: Name, function: AbstractFunction, ) -> Self
Creates a new function with the name and function indicated as arguments.
This for example defines a function called ‘sum_two’ that sums two u8s:
moon_script::FunctionDefinition::new("sum_two", |num:u8, other:u8| num+other);
You can use it this way:
use moon_script::{ContextBuilder, Engine, FunctionDefinition};
let my_sum_function = FunctionDefinition::new("sum_two", |num:u8, other:u8| num+other);
let mut engine = Engine::new();
engine.add_function(my_sum_function);
let result = engine.parse("return sum_two(10,5);", ContextBuilder::new())
.unwrap().execute().map(|value|u8::try_from(value)).unwrap().unwrap();
assert_eq!(15, result);
Sourcepub fn module_name<Name: Into<String>>(self, module_name: Name) -> Self
pub fn module_name<Name: Into<String>>(self, module_name: Name) -> Self
Specifies the module name for this function.
Sourcepub fn associated_type_name<'input, Name: Into<MoonValueKind<'input>>>(
self,
associated_type_name: Name,
) -> Self
pub fn associated_type_name<'input, Name: Into<MoonValueKind<'input>>>( self, associated_type_name: Name, ) -> Self
Specifies the associated type for this function.
This is a function associated to the moon script primitive Integer:
moon_script::FunctionDefinition::new("sum_two", |num:u8, other:u8| num+other)
.associated_type_name(moon_script::MoonValueKind::Integer);
This is a function associated to a custom type that can be read as an u8:
moon_script::FunctionDefinition::new("sum_two", |num:u8, other:u8| num+other)
.associated_type_name("MyCustomTypeName");
Sourcepub fn associated_type_of<T>(self) -> Self
pub fn associated_type_of<T>(self) -> Self
Specifies the associated type for this function, but instead of receiving a name or a crate::MoonValueKind, it receives the value, this is preferred over Self::associated_type_name but it doesn’t allow you to create pseudo-types, requiring the use of real types.
use moon_script::{ContextBuilder, Engine, FunctionDefinition, InputVariable};
let mut engine = Engine::new();
engine.add_function(FunctionDefinition::new("add_two", |n:u8|n+2).associated_type_of::<u8>());
let context_with_variable = ContextBuilder::new().with_variable(InputVariable::new("five").associated_type_of::<u8>());
let ast = engine.parse("five.add_two()", context_with_variable).unwrap();
let result : u8 = ast.executor().push_variable("five", 5).execute().unwrap().try_into().unwrap();
assert_eq!(7, result);
Sourcepub const fn inline(self) -> Self
pub const fn inline(self) -> Self
Marks this function as constant, being able to inline it’s results when compiling the script if the arguments are also constant.
Sourcepub fn known_return_type_name<'input, Name: Into<MoonValueKind<'input>>>(
self,
return_type_name: Name,
) -> Self
pub fn known_return_type_name<'input, Name: Into<MoonValueKind<'input>>>( self, return_type_name: Name, ) -> Self
Specifies the type of the return value for this function, if let unmarked, associations cannot be used and therefore properties won’t work.
Sourcepub fn known_return_type_of<T>(self) -> Self
pub fn known_return_type_of<T>(self) -> Self
Specifies the type of the return value for this function, but instead of receiving a name or a crate::MoonValueKind, it receives the value, this is preferred over Self::known_return_type_name but it doesn’t allow you to create pseudo-types, requiring the use of real types.
Trait Implementations§
Source§impl Clone for FunctionDefinition
impl Clone for FunctionDefinition
Source§fn clone(&self) -> FunctionDefinition
fn clone(&self) -> FunctionDefinition
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more