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