Add type alias support for plugin modules.

This commit is contained in:
Stephen Chung
2022-03-19 09:43:18 +08:00
parent 6546eae95f
commit fefa633cf0
12 changed files with 207 additions and 18 deletions

View File

@@ -10,7 +10,7 @@ use std::any::{type_name, TypeId};
use std::prelude::v1::*;
impl Engine {
/// Get the global namespace module (which is the last module in `global_modules`).
/// Get the global namespace module (which is the fist module in `global_modules`).
#[inline(always)]
#[allow(dead_code)]
pub(crate) fn global_namespace(&self) -> &Module {
@@ -273,7 +273,8 @@ impl Engine {
/// ```
#[inline(always)]
pub fn register_type_with_name<T: Variant + Clone>(&mut self, name: &str) -> &mut Self {
self.register_type_with_name_raw(type_name::<T>(), name)
self.custom_types.add_type::<T>(name);
self
}
/// Register a custom type for use with the [`Engine`], with a pretty-print name
/// for the `type_of` function. The type must implement [`Clone`].
@@ -288,8 +289,7 @@ impl Engine {
name: impl Into<Identifier>,
) -> &mut Self {
// Add the pretty-print type name into the map
self.type_names
.insert(fully_qualified_type_path.into(), name.into());
self.custom_types.add(fully_qualified_type_path, name);
self
}
/// Register an type iterator for an iterable type with the [`Engine`].

View File

@@ -85,9 +85,15 @@ impl Engine {
#[inline]
#[must_use]
pub fn map_type_name<'a>(&'a self, name: &'a str) -> &'a str {
self.type_names
.get(name)
.map(|s| s.as_str())
self.global_modules
.iter()
.find_map(|m| m.get_custom_type(name))
.or_else(|| {
self.global_sub_modules
.iter()
.find_map(|(_, m)| m.get_custom_type(name))
})
.or_else(|| self.custom_types.get(name))
.unwrap_or_else(|| map_std_type_name(name, true))
}
@@ -109,9 +115,8 @@ impl Engine {
};
}
self.type_names
self.custom_types
.get(name)
.map(|s| s.as_str())
.unwrap_or_else(|| match name {
"INT" => return type_name::<crate::INT>(),
#[cfg(not(feature = "no_float"))]