From db19a553c716f4e8f311f7f40d331d2ff82f6624 Mon Sep 17 00:00:00 2001 From: William Venner Date: Sat, 22 Jan 2022 19:12:47 +0000 Subject: [PATCH] Add DisplayLuaError trait for specialized error messages --- gmod/src/lua/returns.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/gmod/src/lua/returns.rs b/gmod/src/lua/returns.rs index 8bfed8c..f211052 100644 --- a/gmod/src/lua/returns.rs +++ b/gmod/src/lua/returns.rs @@ -1,4 +1,4 @@ -use std::num::NonZeroI32; +use std::{num::NonZeroI32, borrow::Cow}; #[repr(transparent)] pub struct ValuesReturned(pub i32); @@ -44,12 +44,21 @@ impl From> for ValuesReturned { } } -impl From> for ValuesReturned { +pub trait DisplayLuaError { + fn display_lua_error<'a>(&'a self) -> Cow<'a, str>; +} +impl DisplayLuaError for E { + #[inline(always)] + fn display_lua_error<'a>(&'a self) -> Cow<'a, str> { + Cow::Owned(format!("{:?}", self)) + } +} +impl From> for ValuesReturned { #[inline(always)] fn from(res: Result) -> ValuesReturned { match res { Ok(vals) => ValuesReturned(vals), - Err(err) => unsafe { super::state().error(&format!("{:?}", err)) } + Err(err) => unsafe { super::state().error(err.display_lua_error().as_ref()) } } } } \ No newline at end of file