Skip to main content

rhai/func/
plugin.rs

1//! Module defining macros for developing _plugins_.
2
3use super::FnCallArgs;
4#[cfg(feature = "no_std")]
5use std::prelude::v1::*;
6
7/// Result of a Rhai function.
8pub type RhaiResult = crate::RhaiResult;
9
10pub use rhai_codegen::*;
11
12// Removing these re-exports would be a breaking change,
13// despite there are other import paths that can be used.
14// https://github.com/rhaiscript/rhai/issues/1098
15pub use super::RhaiFunc;
16pub use crate::{
17    Dynamic, Engine, EvalAltResult, FnAccess, FnNamespace, FuncRegistration, ImmutableString,
18    Module, NativeCallContext, Position,
19};
20pub use std::{any::TypeId, mem};
21
22/// Trait implemented by a _plugin function_.
23///
24/// This trait should not be used directly.
25pub trait PluginFunc {
26    /// Call the plugin function with the arguments provided.
27    fn call(&self, context: Option<NativeCallContext>, args: &mut FnCallArgs) -> RhaiResult;
28
29    /// Is this plugin function a method?
30    #[must_use]
31    fn is_method_call(&self) -> bool;
32
33    /// Does this plugin function contain a [`NativeCallContext`] parameter?
34    #[must_use]
35    fn has_context(&self) -> bool;
36
37    /// Is this plugin function pure?
38    ///
39    /// Defaults to `true` such that any old implementation that has constant-checking code inside
40    /// the function itself will continue to work.
41    #[inline(always)]
42    #[must_use]
43    fn is_pure(&self) -> bool {
44        true
45    }
46
47    /// Is this plugin function volatile?
48    ///
49    /// A volatile function is not guaranteed to return the same result for the same input(s).
50    ///
51    /// Defaults to `true` such that any old implementation that has constant-checking code inside
52    /// the function itself will continue to work.
53    #[inline(always)]
54    #[must_use]
55    fn is_volatile(&self) -> bool {
56        true
57    }
58}