rhai/packages/
mod.rs

1//! Module containing all built-in _packages_ available to Rhai, plus facilities to define custom packages.
2
3use crate::{Engine, Module, SharedModule};
4
5pub(crate) mod arithmetic;
6pub(crate) mod array_basic;
7pub(crate) mod bit_field;
8pub(crate) mod blob_basic;
9pub(crate) mod debugging;
10pub(crate) mod fn_basic;
11pub(crate) mod iter_basic;
12pub(crate) mod lang_core;
13pub(crate) mod logic;
14pub(crate) mod map_basic;
15pub(crate) mod math_basic;
16pub(crate) mod pkg_core;
17pub(crate) mod pkg_std;
18pub(crate) mod string_basic;
19pub(crate) mod string_more;
20pub(crate) mod time_basic;
21
22pub use arithmetic::ArithmeticPackage;
23#[cfg(not(feature = "no_index"))]
24pub use array_basic::BasicArrayPackage;
25pub use bit_field::BitFieldPackage;
26#[cfg(not(feature = "no_index"))]
27pub use blob_basic::BasicBlobPackage;
28#[cfg(feature = "debugging")]
29pub use debugging::DebuggingPackage;
30pub use fn_basic::BasicFnPackage;
31pub use iter_basic::BasicIteratorPackage;
32pub use lang_core::LanguageCorePackage;
33pub use logic::LogicPackage;
34#[cfg(not(feature = "no_object"))]
35pub use map_basic::BasicMapPackage;
36pub use math_basic::BasicMathPackage;
37pub use pkg_core::CorePackage;
38pub use pkg_std::StandardPackage;
39pub use string_basic::BasicStringPackage;
40pub use string_more::MoreStringPackage;
41#[cfg(not(feature = "no_time"))]
42pub use time_basic::BasicTimePackage;
43
44/// Trait that all packages must implement.
45pub trait Package {
46    /// Initialize the package.
47    /// Functions should be registered into `module` here.
48    #[cold]
49    fn init(module: &mut Module);
50
51    /// Initialize the package with an [`Engine`].
52    ///
53    /// Perform tasks such as registering custom operators/syntax.
54    #[cold]
55    #[inline]
56    #[allow(unused_variables)]
57    fn init_engine(engine: &mut Engine) {}
58
59    /// Register the package with an [`Engine`].
60    ///
61    /// # Example
62    ///
63    /// ```rust
64    /// # use rhai::Engine;
65    /// # use rhai::packages::{Package, CorePackage};
66    /// let mut engine = Engine::new_raw();
67    /// let package = CorePackage::new();
68    ///
69    /// package.register_into_engine(&mut engine);
70    /// ```
71    #[cold]
72    #[inline]
73    fn register_into_engine(&self, engine: &mut Engine) -> &Self {
74        Self::init_engine(engine);
75        engine.register_global_module(self.as_shared_module());
76        self
77    }
78
79    /// Register the package with an [`Engine`] under a static namespace.
80    ///
81    /// # Example
82    ///
83    /// ```rust
84    /// # use rhai::Engine;
85    /// # use rhai::packages::{Package, CorePackage};
86    /// let mut engine = Engine::new_raw();
87    /// let package = CorePackage::new();
88    ///
89    /// package.register_into_engine_as(&mut engine, "core");
90    /// ```
91    #[cfg(not(feature = "no_module"))]
92    #[cold]
93    #[inline]
94    fn register_into_engine_as(&self, engine: &mut Engine, name: &str) -> &Self {
95        Self::init_engine(engine);
96        engine.register_static_module(name, self.as_shared_module());
97        self
98    }
99
100    /// Get a reference to a shared module from this package.
101    #[must_use]
102    fn as_shared_module(&self) -> SharedModule;
103}
104
105/// Macro that makes it easy to define a _package_ (which is basically a shared [module][Module])
106/// and register functions into it.
107///
108/// Functions can be added to the package using [`Module::set_native_fn`].
109///
110/// # Example
111///
112/// Define a package named `MyPackage` with a single function named `my_add`:
113///
114/// ```
115/// use rhai::{Dynamic, EvalAltResult};
116/// use rhai::def_package;
117///
118/// fn add(x: i64, y: i64) -> Result<i64, Box<EvalAltResult>> { Ok(x + y) }
119///
120/// def_package! {
121///     /// My super-duper package.
122///     pub MyPackage(module) {
123///         // Load a native Rust function.
124///         module.set_native_fn("my_add", add);
125///     }
126/// }
127/// ```
128#[macro_export]
129macro_rules! def_package {
130    ($($(#[$outer:meta])* $mod:vis $package:ident($lib:ident)
131                $( : $($(#[$base_meta:meta])* $base_pkg:ty),+ )?
132                $block:block
133                $( |> | $engine:ident | $init_engine:block )?
134    )+) => { $(
135        $(#[$outer])*
136        $mod struct $package($crate::Shared<$crate::Module>);
137
138        impl $crate::packages::Package for $package {
139            #[inline(always)]
140            fn as_shared_module(&self) -> $crate::Shared<$crate::Module> {
141                self.0.clone()
142            }
143            fn init($lib: &mut $crate::Module) {
144                $($(
145                    $(#[$base_meta])* { <$base_pkg>::init($lib); }
146                )*)*
147
148                $block
149            }
150            fn init_engine(_engine: &mut $crate::Engine) {
151                $($(
152                    $(#[$base_meta])* { <$base_pkg>::init_engine(_engine); }
153                )*)*
154
155                $(
156                    let $engine = _engine;
157                    $init_engine
158                )*
159            }
160        }
161
162        impl Default for $package {
163            #[inline(always)]
164            fn default() -> Self {
165                Self::new()
166            }
167        }
168
169        impl $package {
170            #[doc=concat!("Create a new `", stringify!($package), "`")]
171            #[inline]
172            #[must_use]
173            pub fn new() -> Self {
174                let mut module = $crate::Module::new();
175                <Self as $crate::packages::Package>::init(&mut module);
176                module.build_index();
177                Self(module.into())
178            }
179        }
180    )* };
181    ($($(#[$outer:meta])* $root:ident :: $package:ident => | $lib:ident | $block:block)+) => { $(
182        $(#[$outer])*
183        /// # Deprecated
184        ///
185        /// This old syntax of `def_package!` is deprecated. Use the new syntax instead.
186        ///
187        /// This syntax will be removed in the next major version.
188        #[deprecated(since = "1.5.0", note = "this is an old syntax of `def_package!` and is deprecated; use the new syntax of `def_package!` instead")]
189        pub struct $package($root::Shared<$root::Module>);
190
191        impl $root::packages::Package for $package {
192            fn as_shared_module(&self) -> $root::Shared<$root::Module> {
193                self.0.clone()
194            }
195            fn init($lib: &mut $root::Module) {
196                $block
197            }
198        }
199
200        impl Default for $package {
201            #[inline(always)]
202            #[must_use]
203            fn default() -> Self {
204                Self::new()
205            }
206        }
207
208        impl $package {
209            #[inline]
210            #[must_use]
211            pub fn new() -> Self {
212                let mut module = $root::Module::new();
213                <Self as $root::packages::Package>::init(&mut module);
214                module.build_index();
215                Self(module.into())
216            }
217        }
218    )* };
219    ($root:ident : $package:ident : $comment:expr , $lib:ident , $block:stmt) => {
220        #[doc=$comment]
221        ///
222        /// # Deprecated
223        ///
224        /// This old syntax of `def_package!` is deprecated. Use the new syntax instead.
225        ///
226        /// This syntax will be removed in the next major version.
227        #[deprecated(since = "1.4.0", note = "this is an old syntax of `def_package!` and is deprecated; use the new syntax of `def_package!` instead")]
228        pub struct $package($root::Shared<$root::Module>);
229
230        impl $root::packages::Package for $package {
231            fn as_shared_module(&self) -> $root::Shared<$root::Module> {
232                self.0.clone()
233            }
234            fn init($lib: &mut $root::Module) {
235                $block
236            }
237        }
238
239        impl Default for $package {
240            #[inline(always)]
241            #[must_use]
242            fn default() -> Self {
243                Self::new()
244            }
245        }
246
247        impl $package {
248            #[inline]
249            #[must_use]
250            pub fn new() -> Self {
251                let mut module = $root::Module::new();
252                <Self as $root::packages::Package>::init(&mut module);
253                module.build_index();
254                Self(module.into())
255            }
256        }
257    };
258}