Struct rhai::FuncRegistration

source ·
pub struct FuncRegistration { /* private fields */ }
Expand description

Type for fine-tuned module function registration.

Implementations§

source§

impl FuncRegistration

source

pub fn new(name: impl Into<Identifier>) -> Self

Create a new FuncRegistration.

§Defaults
  • Accessibility: The function namespace is FnNamespace::Internal.

  • Purity: The function is assumed to be pure unless it is a property setter or an index setter.

  • Volatility: The function is assumed to be non-volatile – i.e. it guarantees the same result for the same input(s).

  • Metadata: No metadata for the function is registered.

let mut module = Module::new();

fn inc(x: i64) -> i64 { x + 1 }

let f = FuncRegistration::new("inc")
    .in_global_namespace()
    .set_into_module(&mut module, inc);

let hash = f.hash;

assert!(module.contains_fn(hash));
source

pub fn new_getter(prop: impl AsRef<str>) -> Self

Create a new FuncRegistration for a property getter.

Not available under no_object.

§Defaults
  • Accessibility: The function namespace is FnNamespace::Global.

  • Purity: The function is assumed to be pure.

  • Volatility: The function is assumed to be non-volatile – i.e. it guarantees the same result for the same input(s).

  • Metadata: No metadata for the function is registered.

source

pub fn new_setter(prop: impl AsRef<str>) -> Self

Create a new FuncRegistration for a property setter.

Not available under no_object.

§Defaults
  • Accessibility: The function namespace is FnNamespace::Global.

  • Purity: The function is assumed to be no-pure.

  • Volatility: The function is assumed to be non-volatile – i.e. it guarantees the same result for the same input(s).

  • Metadata: No metadata for the function is registered.

source

pub fn new_index_getter() -> Self

Create a new FuncRegistration for an index getter.

Not available under both no_index and no_object.

§Defaults
  • Accessibility: The function namespace is FnNamespace::Global.

  • Purity: The function is assumed to be pure.

  • Volatility: The function is assumed to be non-volatile – i.e. it guarantees the same result for the same input(s).

  • Metadata: No metadata for the function is registered.

source

pub fn new_index_setter() -> Self

Create a new FuncRegistration for an index setter.

Not available under both no_index and no_object.

§Defaults
  • Accessibility: The function namespace is FnNamespace::Global.

  • Purity: The function is assumed to be no-pure.

  • Volatility: The function is assumed to be non-volatile – i.e. it guarantees the same result for the same input(s).

  • Metadata: No metadata for the function is registered.

source

pub const fn with_namespace(self, namespace: FnNamespace) -> Self

Set the namespace of the function.

source

pub const fn in_global_namespace(self) -> Self

Set the function to the global namespace.

source

pub const fn in_internal_namespace(self) -> Self

Set the function to the internal namespace.

source

pub const fn with_purity(self, pure: bool) -> Self

Set whether the function is pure. A pure function has no side effects.

source

pub const fn with_volatility(self, volatile: bool) -> Self

Set whether the function is volatile. A volatile function does not guarantee the same result for the same input(s).

source

pub fn with_params_info<S: AsRef<str>>( self, params: impl IntoIterator<Item = S> ) -> Self

(metadata) Set the function’s parameter names and/or types. Exported under the metadata feature only.

The input is a list of strings, each of which is either a parameter name or a parameter name/type pair.

The last string should be the type of the return value.

§Parameter Examples

"foo: &str" <- parameter name = foo, type = &str
"bar" <- parameter name = bar, type unknown
"_: i64" <- parameter name unknown, type = i64
"MyType" <- parameter name unknown, type = MyType

source

pub fn with_comments<S: AsRef<str>>( self, comments: impl IntoIterator<Item = S> ) -> Self

(metadata) Set the function’s doc-comments. Exported under the metadata feature only.

The input is a list of strings, each of which is either a block of single-line doc-comments or a single block doc-comment.

§Comments

Single-line doc-comments typically start with /// and should be merged, with line-breaks, into a single string without a final termination line-break.

Block doc-comments typically start with /** and end with */ and should be kept in a separate string.

Leading white-spaces should be stripped, and each string should always start with the corresponding doc-comment leader: /// or /**.

Each line in non-block doc-comments should start with ///.

source

pub fn register_into_engine<A: 'static, const N: usize, const X: bool, R, const F: bool, FUNC>( self, engine: &mut Engine, func: FUNC ) -> &FuncMetadata
where R: Variant + Clone, FUNC: RhaiNativeFunc<A, N, X, R, F> + SendSync + 'static,

Register the function into the specified Engine.

source

pub fn set_into_module<A: 'static, const N: usize, const X: bool, R, const F: bool, FUNC>( self, module: &mut Module, func: FUNC ) -> &FuncMetadata
where R: Variant + Clone, FUNC: RhaiNativeFunc<A, N, X, R, F> + SendSync + 'static,

Register the function into the specified Module.

source

pub fn set_into_module_raw( self, module: &mut Module, arg_types: impl AsRef<[TypeId]>, func: RhaiFunc ) -> &FuncMetadata

Register the function into the specified Module.

§WARNING - Low Level API

This function is very low level. It takes a list of TypeId’s indicating the actual types of the parameters.

§Panics

Panics if the type of the first parameter is Array, Map, String, ImmutableString, &str or INT and the function name indicates that it is an index getter or setter.

Indexers for arrays, object maps, strings and integers cannot be registered.

Trait Implementations§

source§

impl Clone for FuncRegistration

source§

fn clone(&self) -> FuncRegistration

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 FuncRegistration

source§

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

Formats the value using the given formatter. Read more
source§

impl Hash for FuncRegistration

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for FuncRegistration

source§

fn eq(&self, other: &FuncRegistration) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for FuncRegistration

source§

impl StructuralPartialEq for FuncRegistration

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> 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,

§

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>,

§

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>,

§

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.
source§

impl<T> Variant for T
where T: Any + Clone + SendSync,

source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert this Variant trait object to &dyn Any.
source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert this Variant trait object to &mut dyn Any.
source§

fn as_boxed_any(self: Box<T>) -> Box<dyn Any>

Convert this Variant trait object to Box<dyn Any>.
source§

fn type_name(&self) -> &'static str

Get the name of this type.
source§

fn clone_object(&self) -> Box<dyn Variant>

Clone this Variant trait object.