Struct FunctionDefinition

Source
pub struct FunctionDefinition { /* private fields */ }
Expand description

Builder pattern for defining custom Engine’s functions

Implementations§

Source§

impl FunctionDefinition

Source

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);
Source

pub fn module_name<Name: Into<String>>(self, module_name: Name) -> Self

Specifies the module name for this function.

Source

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");
Source

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);
Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> FunctionDefinition

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for FunctionDefinition

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.