From 6f4240ed5a78abb493dd2a480bf2202dddc1b33e Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 30 Mar 2022 10:33:43 +0800 Subject: [PATCH] Add examples to Module custom type API. --- src/module/mod.rs | 56 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/src/module/mod.rs b/src/module/mod.rs index 34c2887b..976a554b 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -417,20 +417,68 @@ impl Module { } /// Map a custom type to a friendly display name. + /// + /// # Example + /// + /// ``` + /// # use rhai::Module; + /// #[derive(Clone)] + /// struct TestStruct {} + /// + /// let name = std::any::type_name::of::()); + /// + /// let mut module = Module::new(); + /// + /// module.set_custom_type("MyType"); + /// + /// assert_eq!(module.get_custom_type(name, Some("MyType")); + /// ``` #[inline(always)] - pub fn set_custom_type(&mut self, name: &str) { - self.custom_types.add_type::(name) + pub fn set_custom_type(&mut self, name: &str) -> &mut Self { + self.custom_types.add_type::(name); + self } /// Map a custom type to a friendly display name. + /// + /// ``` + /// # use rhai::Module; + /// #[derive(Clone)] + /// struct TestStruct {} + /// + /// let name = std::any::type_name::of::()); + /// + /// let mut module = Module::new(); + /// + /// module.set_custom_type_raw(name, "MyType"); + /// + /// assert_eq!(module.get_custom_type(name, Some("MyType")); + /// ``` #[inline(always)] pub fn set_custom_type_raw( &mut self, type_name: impl Into, name: impl Into, - ) { - self.custom_types.add(type_name, name) + ) -> &mut Self { + self.custom_types.add(type_name, name); + self } /// Get the display name of a registered custom type. + /// + /// # Example + /// + /// ``` + /// # use rhai::Module; + /// #[derive(Clone)] + /// struct TestStruct {} + /// + /// let name = std::any::type_name::of::()); + /// + /// let mut module = Module::new(); + /// + /// module.set_custom_type("MyType"); + /// + /// assert_eq!(module.get_custom_type(name, Some("MyType")); + /// ``` #[inline(always)] pub fn get_custom_type(&self, key: &str) -> Option<&str> { self.custom_types.get(key)