From d9d44a96838088cb2047017ac5b38135e838a15f Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 9 Dec 2021 12:49:12 +0800 Subject: [PATCH] Add Engine::register_type_with_name_raw. --- CHANGELOG.md | 1 + src/api/register.rs | 22 +++++++++++++++++++--- src/engine.rs | 2 +- src/module/resolvers/mod.rs | 2 +- src/packages/mod.rs | 12 ++++++------ src/types/immutable_string.rs | 3 +-- 6 files changed, 29 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 556b22fb..2b921acc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ New features Enhancements ------------ +* Added `Engine::register_type_with_name_raw` to register a custom type based on a fully-qualified type path. * Added `into_array` and `into_typed_array` for `Dynamic`. * Added `FnPtr::call` and `FnPtr::call_within_context` to simplify calling a function pointer. * A function's hashes are included in its JSON metadata to assist in debugging. Each function's `hashBase` field in the JSON object should map directly to the pre-calculated hash in the function call. diff --git a/src/api/register.rs b/src/api/register.rs index 304731b9..32c929d7 100644 --- a/src/api/register.rs +++ b/src/api/register.rs @@ -4,6 +4,7 @@ use crate::func::{FnCallArgs, RegisterNativeFunction, SendSync}; use crate::types::dynamic::Variant; use crate::{ Engine, EvalAltResult, FnAccess, FnNamespace, Identifier, Module, NativeCallContext, Shared, + SmartString, }; use std::any::{type_name, TypeId}; #[cfg(feature = "no_std")] @@ -146,11 +147,12 @@ impl Engine { /// /// # WARNING - Low Level API /// - /// This function is very low level. It takes a list of [`TypeId`][std::any::TypeId]'s indicating the actual types of the parameters. + /// This function is very low level. It takes a list of [`TypeId`][std::any::TypeId]'s + /// indicating the actual types of the parameters. /// /// ## Arguments /// - /// Arguments are simply passed in as a mutable array of [`&mut Dynamic`][crate::Dynamic], + /// Arguments are simply passed in as a mutable array of [`&mut Dynamic`][crate::Dynamic]. /// The arguments are guaranteed to be of the correct types matching the [`TypeId`][std::any::TypeId]'s. /// /// To access a primary argument value (i.e. cloning is cheap), use: `args[n].as_xxx().unwrap()` @@ -263,9 +265,23 @@ impl Engine { /// ``` #[inline(always)] pub fn register_type_with_name(&mut self, name: &str) -> &mut Self { + self.register_type_with_name_raw(type_name::(), name) + } + /// Register a custom type for use with the [`Engine`], with a pretty-print name + /// for the `type_of` function. The type must implement [`Clone`]. + /// + /// # WARNING - Low Level API + /// + /// This function is low level. + #[inline(always)] + pub fn register_type_with_name_raw( + &mut self, + fully_qualified_type_path: impl Into, + name: impl Into, + ) -> &mut Self { // Add the pretty-print type name into the map self.type_names - .insert(type_name::().into(), Box::new(name.into())); + .insert(fully_qualified_type_path.into(), name.into()); self } /// Register an type iterator for an iterable type with the [`Engine`]. diff --git a/src/engine.rs b/src/engine.rs index 47dcade6..b77050f5 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -936,7 +936,7 @@ pub struct Engine { pub(crate) module_resolver: Option>, /// A map mapping type names to pretty-print names. - pub(crate) type_names: BTreeMap>, + pub(crate) type_names: BTreeMap, /// An empty [`ImmutableString`] for cloning purposes. pub(crate) empty_string: ImmutableString, diff --git a/src/module/resolvers/mod.rs b/src/module/resolvers/mod.rs index 087d8f7c..47911c34 100644 --- a/src/module/resolvers/mod.rs +++ b/src/module/resolvers/mod.rs @@ -31,7 +31,7 @@ pub trait ModuleResolver: SendSync { /// Returns [`None`] (default) if such resolution is not supported /// (e.g. if the module is Rust-based). /// - /// ## Low-Level API + /// # WARNING - Low Level API /// /// Override the default implementation of this method if the module resolver /// serves modules based on compiled Rhai scripts. diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 63bfc5d4..a18a11cf 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -37,10 +37,11 @@ pub use time_basic::BasicTimePackage; /// Trait that all packages must implement. pub trait Package { - /// Register all the functions in a package into a store. - fn init(lib: &mut Module); + /// Initialize the package. + /// Functions should be registered into `module` here. + fn init(module: &mut Module); - /// Retrieve the generic package library from this package. + /// Get a reference to a shared module from this package. #[must_use] fn as_shared_module(&self) -> Shared; } @@ -60,10 +61,10 @@ pub trait Package { /// /// fn add(x: i64, y: i64) -> Result> { Ok(x + y) } /// -/// def_package!(rhai:MyPackage:"My super-duper package", lib, +/// def_package!(rhai:MyPackage:"My super-duper package", module, /// { /// // Load a binary function with all value parameters. -/// lib.set_native_fn("my_add", add); +/// module.set_native_fn("my_add", add); /// }); /// ``` #[macro_export] @@ -76,7 +77,6 @@ macro_rules! def_package { fn as_shared_module(&self) -> $root::Shared<$root::Module> { self.0.clone() } - fn init($lib: &mut $root::Module) { $block } diff --git a/src/types/immutable_string.rs b/src/types/immutable_string.rs index 822223df..8c8dc3eb 100644 --- a/src/types/immutable_string.rs +++ b/src/types/immutable_string.rs @@ -16,8 +16,7 @@ use std::{ /// The system immutable string type. /// -/// An [`ImmutableString`] wraps an [`Rc`][std::rc::Rc]`<`[`SmartString`][smartstring::SmartString]`>` -/// (or [`Arc`][std::sync::Arc]`<`[`SmartString`][smartstring::SmartString]`>` under the `sync` feature) +/// An [`ImmutableString`] wraps an `Rc` (or `Arc` under the `sync` feature) /// so that it can be simply shared and not cloned. /// /// # Example