Remove Engine::custom_types.

This commit is contained in:
Stephen Chung 2022-03-29 08:18:20 +08:00
parent 2b9b84a764
commit 1b6de25007
6 changed files with 35 additions and 15 deletions

View File

@ -34,6 +34,7 @@ Enhancements
* `starts_with` and `ends_with` are added for strings.
* Variables in modules registered via `register_global_module` can now be accessed in the global namespace.
* `Dynamic::into_read_only` is added to convert a `Dynamic` value into constant.
* `Module` now holds a collection of custom types with an API.
Version 1.5.0

View File

@ -273,7 +273,7 @@ impl Engine {
/// ```
#[inline(always)]
pub fn register_type_with_name<T: Variant + Clone>(&mut self, name: &str) -> &mut Self {
self.custom_types.add_type::<T>(name);
self.global_namespace_mut().set_custom_type::<T>(name);
self
}
/// Register a custom type for use with the [`Engine`], with a pretty-print name
@ -289,7 +289,8 @@ impl Engine {
name: impl Into<Identifier>,
) -> &mut Self {
// Add the pretty-print type name into the map
self.custom_types.add(fully_qualified_type_path, name);
self.global_namespace_mut()
.set_custom_type_raw(fully_qualified_type_path, name);
self
}
/// Register an type iterator for an iterable type with the [`Engine`].

View File

@ -97,7 +97,6 @@ impl Engine {
#[cfg(feature = "no_module")]
return None;
})
.or_else(|| self.custom_types.get(name))
.unwrap_or_else(|| map_std_type_name(name, true))
}
@ -119,8 +118,18 @@ impl Engine {
};
}
self.custom_types
.get(name)
self.global_modules
.iter()
.find_map(|m| m.get_custom_type(name))
.or_else(|| {
#[cfg(not(feature = "no_module"))]
return self
.global_sub_modules
.iter()
.find_map(|(_, m)| m.get_custom_type(name));
#[cfg(feature = "no_module")]
return None;
})
.unwrap_or_else(|| match name {
"INT" => return type_name::<crate::INT>(),
#[cfg(not(feature = "no_float"))]

View File

@ -7,7 +7,7 @@ use crate::func::native::{
};
use crate::packages::{Package, StandardPackage};
use crate::tokenizer::Token;
use crate::types::{dynamic::Union, CustomTypesCollection};
use crate::types::dynamic::Union;
use crate::{
Dynamic, Identifier, ImmutableString, Module, OptimizationLevel, Position, RhaiResult, Shared,
StaticVec,
@ -105,9 +105,6 @@ pub struct Engine {
#[cfg(not(feature = "no_module"))]
pub(crate) module_resolver: Box<dyn crate::ModuleResolver>,
/// A map mapping type names to pretty-print names.
pub(crate) custom_types: CustomTypesCollection,
/// An empty [`ImmutableString`] for cloning purposes.
pub(crate) empty_string: ImmutableString,
@ -160,8 +157,7 @@ impl fmt::Debug for Engine {
#[cfg(not(feature = "no_module"))]
f.field("global_sub_modules", &self.global_sub_modules);
f.field("type_names", &self.custom_types)
.field("disabled_symbols", &self.disabled_symbols)
f.field("disabled_symbols", &self.disabled_symbols)
.field("custom_keywords", &self.custom_keywords)
.field("custom_syntax", &(!self.custom_syntax.is_empty()))
.field("def_var_filter", &self.def_var_filter.is_some())
@ -263,7 +259,6 @@ impl Engine {
#[cfg(not(feature = "no_module"))]
module_resolver: Box::new(crate::module::resolvers::DummyModuleResolver::new()),
custom_types: CustomTypesCollection::new(),
empty_string: ImmutableString::new(),
disabled_symbols: BTreeSet::new(),
custom_keywords: BTreeMap::new(),

View File

@ -421,6 +421,15 @@ impl Module {
pub fn set_custom_type<T>(&mut self, name: &str) {
self.custom_types.add_type::<T>(name)
}
/// Map a custom type to a friendly display name.
#[inline(always)]
pub fn set_custom_type_raw(
&mut self,
type_name: impl Into<Identifier>,
name: impl Into<Identifier>,
) {
self.custom_types.add(type_name, name)
}
/// Get the display name of a registered custom type.
#[inline(always)]
pub fn get_custom_type(&self, key: &str) -> Option<&str> {

View File

@ -27,13 +27,18 @@ impl CustomTypesCollection {
}
/// Register a custom type.
#[inline(always)]
pub fn add(&mut self, key: impl Into<Identifier>, name: impl Into<Identifier>) {
self.0.insert(key.into(), name.into());
pub fn add(&mut self, type_name: impl Into<Identifier>, name: impl Into<Identifier>) {
self.add_raw(type_name, name.into());
}
/// Register a custom type.
#[inline(always)]
pub fn add_type<T>(&mut self, name: &str) {
self.0.insert(type_name::<T>().into(), name.into());
self.add_raw(type_name::<T>(), name.into());
}
/// Register a custom type.
#[inline(always)]
pub fn add_raw(&mut self, type_name: impl Into<Identifier>, custom_type: CustomType) {
self.0.insert(type_name.into(), custom_type);
}
/// Find a custom type.
#[inline(always)]