Refactor a bit conversion int->number

This commit is contained in:
Alex Orlenko 2021-11-04 13:15:26 +00:00
parent ad70ba54a5
commit d0641d812f
No known key found for this signature in database
GPG Key ID: 4C150C250863B96D
1 changed files with 9 additions and 12 deletions

View File

@ -349,18 +349,15 @@ macro_rules! lua_convert_int {
($x:ty) => {
impl<'lua> ToLua<'lua> for $x {
fn to_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {
if let Some(i) = cast(self) {
Ok(Value::Integer(i))
} else {
// TODO: Remove conversion to Number in v0.7
cast(self)
.ok_or_else(|| Error::ToLuaConversionError {
from: stringify!($x),
to: "number",
message: Some("out of range".to_owned()),
})
.map(Value::Number)
}
cast(self)
.map(Value::Integer)
.or_else(|| cast(self).map(Value::Number))
// This is impossible error because conversion to Number never fails
.ok_or_else(|| Error::ToLuaConversionError {
from: stringify!($x),
to: "number",
message: Some("out of range".to_owned()),
})
}
}