rhai/src/packages/fn_basic.rs

48 lines
1.2 KiB
Rust
Raw Normal View History

2020-08-14 12:58:34 +02:00
use crate::plugin::*;
2021-03-29 07:07:10 +02:00
use crate::{def_package, FnPtr, ImmutableString, NativeCallContext};
2021-04-17 09:15:54 +02:00
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
2020-11-23 12:11:32 +01:00
2021-12-20 04:42:39 +01:00
def_package! {
/// Package of basic function pointer utilities.
2021-12-20 04:42:39 +01:00
crate::BasicFnPackage => |lib| {
lib.standard = true;
2021-12-20 04:42:39 +01:00
combine_with_exported_module!(lib, "FnPtr", fn_ptr_functions);
}
}
2020-08-20 16:11:41 +02:00
#[export_module]
mod fn_ptr_functions {
/// Return the name of the function.
///
/// # Example
///
/// ```rhai
/// fn double(x) { x * 2 }
///
/// let f = Fn("double");
///
/// print(f.name); // prints "double"
/// ```
#[rhai_fn(name = "name", get = "name", pure)]
2021-08-13 07:42:39 +02:00
pub fn name(fn_ptr: &mut FnPtr) -> ImmutableString {
fn_ptr.fn_name_raw().into()
2020-08-20 16:11:41 +02:00
}
2020-10-18 16:10:08 +02:00
/// Return `true` if the function is an anonymous function.
///
/// # Example
///
/// ```rhai
/// let f = |x| x * 2;
///
/// print(f.is_anonymous); // prints true
/// ```
2020-10-18 16:10:08 +02:00
#[cfg(not(feature = "no_function"))]
2021-10-20 10:22:12 +02:00
#[rhai_fn(name = "is_anonymous", get = "is_anonymous", pure)]
pub fn is_anonymous(fn_ptr: &mut FnPtr) -> bool {
fn_ptr.is_anonymous()
}
}