Remove Engine::custom_types.
This commit is contained in:
parent
2b9b84a764
commit
1b6de25007
@ -34,6 +34,7 @@ Enhancements
|
|||||||
* `starts_with` and `ends_with` are added for strings.
|
* `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.
|
* 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.
|
* `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
|
Version 1.5.0
|
||||||
|
@ -273,7 +273,7 @@ impl Engine {
|
|||||||
/// ```
|
/// ```
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn register_type_with_name<T: Variant + Clone>(&mut self, name: &str) -> &mut Self {
|
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
|
self
|
||||||
}
|
}
|
||||||
/// Register a custom type for use with the [`Engine`], with a pretty-print name
|
/// Register a custom type for use with the [`Engine`], with a pretty-print name
|
||||||
@ -289,7 +289,8 @@ impl Engine {
|
|||||||
name: impl Into<Identifier>,
|
name: impl Into<Identifier>,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
// Add the pretty-print type name into the map
|
// 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
|
self
|
||||||
}
|
}
|
||||||
/// Register an type iterator for an iterable type with the [`Engine`].
|
/// Register an type iterator for an iterable type with the [`Engine`].
|
||||||
|
@ -97,7 +97,6 @@ impl Engine {
|
|||||||
#[cfg(feature = "no_module")]
|
#[cfg(feature = "no_module")]
|
||||||
return None;
|
return None;
|
||||||
})
|
})
|
||||||
.or_else(|| self.custom_types.get(name))
|
|
||||||
.unwrap_or_else(|| map_std_type_name(name, true))
|
.unwrap_or_else(|| map_std_type_name(name, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,8 +118,18 @@ impl Engine {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
self.custom_types
|
self.global_modules
|
||||||
.get(name)
|
.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 {
|
.unwrap_or_else(|| match name {
|
||||||
"INT" => return type_name::<crate::INT>(),
|
"INT" => return type_name::<crate::INT>(),
|
||||||
#[cfg(not(feature = "no_float"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
|
@ -7,7 +7,7 @@ use crate::func::native::{
|
|||||||
};
|
};
|
||||||
use crate::packages::{Package, StandardPackage};
|
use crate::packages::{Package, StandardPackage};
|
||||||
use crate::tokenizer::Token;
|
use crate::tokenizer::Token;
|
||||||
use crate::types::{dynamic::Union, CustomTypesCollection};
|
use crate::types::dynamic::Union;
|
||||||
use crate::{
|
use crate::{
|
||||||
Dynamic, Identifier, ImmutableString, Module, OptimizationLevel, Position, RhaiResult, Shared,
|
Dynamic, Identifier, ImmutableString, Module, OptimizationLevel, Position, RhaiResult, Shared,
|
||||||
StaticVec,
|
StaticVec,
|
||||||
@ -105,9 +105,6 @@ pub struct Engine {
|
|||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
pub(crate) module_resolver: Box<dyn crate::ModuleResolver>,
|
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.
|
/// An empty [`ImmutableString`] for cloning purposes.
|
||||||
pub(crate) empty_string: ImmutableString,
|
pub(crate) empty_string: ImmutableString,
|
||||||
|
|
||||||
@ -160,8 +157,7 @@ impl fmt::Debug for Engine {
|
|||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
f.field("global_sub_modules", &self.global_sub_modules);
|
f.field("global_sub_modules", &self.global_sub_modules);
|
||||||
|
|
||||||
f.field("type_names", &self.custom_types)
|
f.field("disabled_symbols", &self.disabled_symbols)
|
||||||
.field("disabled_symbols", &self.disabled_symbols)
|
|
||||||
.field("custom_keywords", &self.custom_keywords)
|
.field("custom_keywords", &self.custom_keywords)
|
||||||
.field("custom_syntax", &(!self.custom_syntax.is_empty()))
|
.field("custom_syntax", &(!self.custom_syntax.is_empty()))
|
||||||
.field("def_var_filter", &self.def_var_filter.is_some())
|
.field("def_var_filter", &self.def_var_filter.is_some())
|
||||||
@ -263,7 +259,6 @@ impl Engine {
|
|||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
module_resolver: Box::new(crate::module::resolvers::DummyModuleResolver::new()),
|
module_resolver: Box::new(crate::module::resolvers::DummyModuleResolver::new()),
|
||||||
|
|
||||||
custom_types: CustomTypesCollection::new(),
|
|
||||||
empty_string: ImmutableString::new(),
|
empty_string: ImmutableString::new(),
|
||||||
disabled_symbols: BTreeSet::new(),
|
disabled_symbols: BTreeSet::new(),
|
||||||
custom_keywords: BTreeMap::new(),
|
custom_keywords: BTreeMap::new(),
|
||||||
|
@ -421,6 +421,15 @@ impl Module {
|
|||||||
pub fn set_custom_type<T>(&mut self, name: &str) {
|
pub fn set_custom_type<T>(&mut self, name: &str) {
|
||||||
self.custom_types.add_type::<T>(name)
|
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.
|
/// Get the display name of a registered custom type.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn get_custom_type(&self, key: &str) -> Option<&str> {
|
pub fn get_custom_type(&self, key: &str) -> Option<&str> {
|
||||||
|
@ -27,13 +27,18 @@ impl CustomTypesCollection {
|
|||||||
}
|
}
|
||||||
/// Register a custom type.
|
/// Register a custom type.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn add(&mut self, key: impl Into<Identifier>, name: impl Into<Identifier>) {
|
pub fn add(&mut self, type_name: impl Into<Identifier>, name: impl Into<Identifier>) {
|
||||||
self.0.insert(key.into(), name.into());
|
self.add_raw(type_name, name.into());
|
||||||
}
|
}
|
||||||
/// Register a custom type.
|
/// Register a custom type.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn add_type<T>(&mut self, name: &str) {
|
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.
|
/// Find a custom type.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
Loading…
Reference in New Issue
Block a user