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

43
src/types/custom_types.rs Normal file
View File

@@ -0,0 +1,43 @@
//! Collection of custom types.
use crate::Identifier;
use std::{any::type_name, collections::BTreeMap, fmt};
/// _(internals)_ A custom type.
/// Exported under the `internals` feature only.
pub type CustomType = Identifier;
/// _(internals)_ A collection of custom types.
/// Exported under the `internals` feature only.
#[derive(Clone, Hash, Default)]
pub struct CustomTypesCollection(BTreeMap<Identifier, CustomType>);
impl fmt::Debug for CustomTypesCollection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("CustomTypesCollection ")?;
f.debug_map().entries(self.0.iter()).finish()
}
}
impl CustomTypesCollection {
/// Create a new [`CustomTypesCollection`].
#[inline(always)]
pub fn new() -> Self {
Self(BTreeMap::new())
}
/// 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());
}
/// Register a custom type.
#[inline(always)]
pub fn add_type<T>(&mut self, name: &str) {
self.0.insert(type_name::<T>().into(), name.into());
}
/// Find a custom type.
#[inline(always)]
pub fn get(&self, key: &str) -> Option<&str> {
self.0.get(key).map(CustomType::as_str)
}
}

View File

@@ -1,4 +1,5 @@
//! Helper module which defines the [`Any`] trait to to allow dynamic value handling.
//! Helper module which defines the [`Dynamic`] data type and the
//! [`Any`] trait to to allow custom type handling.
use crate::func::native::SendSync;
use crate::{reify, ExclusiveRange, FnPtr, ImmutableString, InclusiveRange, INT};

View File

@@ -1,5 +1,6 @@
//! Module defining Rhai data types.
pub mod custom_types;
pub mod dynamic;
pub mod error;
pub mod fn_ptr;
@@ -8,6 +9,7 @@ pub mod interner;
pub mod parse_error;
pub mod scope;
pub use custom_types::{CustomType, CustomTypesCollection};
pub use dynamic::Dynamic;
#[cfg(not(feature = "no_std"))]
pub use dynamic::Instant;