From 654da2db8a2a051d57bae4c5d7e144af5821dafb Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 13 Sep 2020 23:13:07 +0800 Subject: [PATCH] Fix tests. --- codegen/src/test/module.rs | 4 ++-- doc/src/rust/packages/create.md | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/codegen/src/test/module.rs b/codegen/src/test/module.rs index 2bf48752..677cd040 100644 --- a/codegen/src/test/module.rs +++ b/codegen/src/test/module.rs @@ -109,7 +109,7 @@ mod module_tests { assert_eq!(item_mod.submodules().len(), 1); assert_eq!(&item_mod.submodules()[0].consts()[0].0, "MYSTIC_NUMBER"); assert_eq!( - item_mod.submodules()[0].consts()[0].1, + item_mod.submodules()[0].consts()[0].2, syn::parse2::(quote! { 42 }).unwrap() ); } @@ -170,7 +170,7 @@ mod module_tests { assert_eq!(item_mod.consts().len(), 1); assert_eq!(&item_mod.consts()[0].0, "MYSTIC_NUMBER"); assert_eq!( - item_mod.consts()[0].1, + item_mod.consts()[0].2, syn::parse2::(quote! { 42 }).unwrap() ); } diff --git a/doc/src/rust/packages/create.md b/doc/src/rust/packages/create.md index 4f55cc13..53ac17a0 100644 --- a/doc/src/rust/packages/create.md +++ b/doc/src/rust/packages/create.md @@ -66,6 +66,9 @@ In fact, this exactly is how Rhai's built-in packages, such as `BasicMathPackage `rhai::plugins::combine_with_exported_module!` adds all functions and constants from the [plugins][plugin module] definition into the package itself. +All sub-modules are _flattened_ (i.e. all functions and constants defined within sub-modules are registered +at the top level) and so there will not be any sub-modules added to the package. + ```rust // Import necessary types and traits. use rhai::{ @@ -84,6 +87,14 @@ mod my_module { pub fn get_num() -> i64 { 42 } + + // This is a sub-module, but if using combine_with_exported_module!, it will + // be flattened and all functions registered at the top level. + pub mod my_sub_module { + pub fn get_sub_num() -> i64 { + 0 + } + } } // Define the package 'MyPackage'. @@ -96,8 +107,17 @@ def_package!(rhai:MyPackage:"My own personal super package", module, { // Merge all registered functions and constants from the plugin module into the custom package. // + // Functions in the sub-module 'my_sub_module' are flattened and registered at the top level + // instead of in a sub-module. + // // The text string name in the middle parameter can be anything and is reserved for future use; // it is recommended to be an ID string that uniquely identifies the module. + // + // This call ends up registering three functions at the top level of the package: + // 1) greet + // 2) get_num + // 3) get_sub_num (flattened from sub-module 'my_sub_module') + // combine_with_exported_module!(module, "my-functions", my_module)); }); ```