From bf566dc25ab4a732722b8ea97f0e9c02f9119db5 Mon Sep 17 00:00:00 2001 From: Stan Bondi Date: Thu, 4 Jan 2024 11:45:20 +0400 Subject: [PATCH] fix(template-lib): minor cross-template call api improvement (#863) Description --- Add "shortcut" for cross-template calls that return the unit type i.e `call::<_, ()>(...)` Motivation and Context --- It is a little unwieldy to use `manager.call::<_, ()>(...)`. This PR adds `manager.invoke(...)` How Has This Been Tested? --- `call` has been tested in "composability" tests What process can a PR reviewer use to test or verify this change? --- Use `manager.invoke` Breaking Changes --- - [x] None - [ ] Requires data directory to be deleted - [ ] Other - Please specify --- dan_layer/template_lib/src/component/manager.rs | 11 +++++++++-- dan_layer/template_lib/src/template/manager.rs | 8 +++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/dan_layer/template_lib/src/component/manager.rs b/dan_layer/template_lib/src/component/manager.rs index 3510f1cf2..d1843d430 100644 --- a/dan_layer/template_lib/src/component/manager.rs +++ b/dan_layer/template_lib/src/component/manager.rs @@ -62,8 +62,9 @@ impl ComponentManager { Self::new(CallerContext::current_component_address()) } - /// Executes a method of the component. Used for template composability. - /// Component methods can be called from another component method or from template functions + /// Calls a method of another component and returns the result. + /// This is used to call external component methods and can be used in a component method or template function + /// context. pub fn call, R: DeserializeOwned>(&self, method: T, args: Vec) -> R { self.call_internal(CallMethodArg { component_address: self.address, @@ -83,6 +84,12 @@ impl ComponentManager { .expect("failed to decode component call result from engine") } + /// Calls a method of another component. The called method must return a unit type. + /// Equivalent to [`call::<_, ()>(method, args)`](ComponentManager::call). + pub fn invoke>(&self, method: T, args: Vec) { + self.call(method, args) + } + /// Get the component state pub fn get_state(&self) -> T { let result = call_engine::<_, InvokeResult>(EngineOp::ComponentInvoke, &ComponentInvokeArg { diff --git a/dan_layer/template_lib/src/template/manager.rs b/dan_layer/template_lib/src/template/manager.rs index 96cb6ce9f..3f2f40f24 100644 --- a/dan_layer/template_lib/src/template/manager.rs +++ b/dan_layer/template_lib/src/template/manager.rs @@ -40,7 +40,7 @@ impl TemplateManager { Self { template_address } } - /// Executes a function of the template. Used for template composability. + /// Executes a function in the template. /// Template functions can be called from another template function or from component methods. pub fn call, T: DeserializeOwned>(&self, function: F, args: Vec) -> T { self.call_internal(CallFunctionArg { @@ -60,4 +60,10 @@ impl TemplateManager { .decode() .expect("failed to decode template function call result from engine") } + + /// Calls a function in the template. The invoked function must return a unit type or a panic will occur. + /// Equivalent to `call::<_, ()>(function, args)`. + pub fn invoke>(&self, function: F, args: Vec) { + self.call(function, args) + } }