Add DisplayLuaError trait for specialized error messages

This commit is contained in:
William Venner 2022-01-22 19:12:47 +00:00
parent 9b79d402e9
commit db19a553c7
1 changed files with 12 additions and 3 deletions

View File

@ -1,4 +1,4 @@
use std::num::NonZeroI32; use std::{num::NonZeroI32, borrow::Cow};
#[repr(transparent)] #[repr(transparent)]
pub struct ValuesReturned(pub i32); pub struct ValuesReturned(pub i32);
@ -44,12 +44,21 @@ impl From<Option<NonZeroI32>> for ValuesReturned {
} }
} }
impl<E: std::fmt::Debug> From<Result<i32, E>> for ValuesReturned { pub trait DisplayLuaError {
fn display_lua_error<'a>(&'a self) -> Cow<'a, str>;
}
impl<E: std::fmt::Debug> DisplayLuaError for E {
#[inline(always)]
fn display_lua_error<'a>(&'a self) -> Cow<'a, str> {
Cow::Owned(format!("{:?}", self))
}
}
impl<E: DisplayLuaError> From<Result<i32, E>> for ValuesReturned {
#[inline(always)] #[inline(always)]
fn from(res: Result<i32, E>) -> ValuesReturned { fn from(res: Result<i32, E>) -> ValuesReturned {
match res { match res {
Ok(vals) => ValuesReturned(vals), Ok(vals) => ValuesReturned(vals),
Err(err) => unsafe { super::state().error(&format!("{:?}", err)) } Err(err) => unsafe { super::state().error(err.display_lua_error().as_ref()) }
} }
} }
} }