-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve formatting / printing of userdata and tables with __type and …
…/ or __tostring metamethods
- Loading branch information
1 parent
0efc2c5
commit 1fb1d3e
Showing
7 changed files
with
121 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,37 @@ | ||
use mlua::prelude::*; | ||
|
||
pub fn get_table_type_metavalue<'a>(tab: &'a LuaTable<'a>) -> Option<String> { | ||
let s = tab | ||
.get_metatable()? | ||
.get::<_, LuaString>(LuaMetaMethod::Type.name()) | ||
.ok()?; | ||
let s = s.to_str().ok()?; | ||
Some(s.to_string()) | ||
} | ||
|
||
pub fn get_userdata_type_metavalue<'a>(tab: &'a LuaAnyUserData<'a>) -> Option<String> { | ||
let s = tab | ||
.get_metatable() | ||
.ok()? | ||
.get::<LuaString>(LuaMetaMethod::Type.name()) | ||
.ok()?; | ||
let s = s.to_str().ok()?; | ||
Some(s.to_string()) | ||
} | ||
|
||
pub fn call_table_tostring_metamethod<'a>(tab: &'a LuaTable<'a>) -> Option<String> { | ||
let f = match tab.get_metatable() { | ||
None => None, | ||
Some(meta) => match meta.get::<_, LuaFunction>(LuaMetaMethod::ToString.name()) { | ||
Ok(method) => Some(method), | ||
Err(_) => None, | ||
}, | ||
}?; | ||
match f.call::<_, String>(()) { | ||
Ok(res) => Some(res), | ||
Err(_) => None, | ||
} | ||
tab.get_metatable()? | ||
.get::<_, LuaFunction>(LuaMetaMethod::ToString.name()) | ||
.ok()? | ||
.call(tab) | ||
.ok() | ||
} | ||
|
||
pub fn call_userdata_tostring_metamethod<'a>(tab: &'a LuaAnyUserData<'a>) -> Option<String> { | ||
let f = match tab.get_metatable() { | ||
Err(_) => None, | ||
Ok(meta) => match meta.get::<LuaFunction>(LuaMetaMethod::ToString.name()) { | ||
Ok(method) => Some(method), | ||
Err(_) => None, | ||
}, | ||
}?; | ||
match f.call::<_, String>(()) { | ||
Ok(res) => Some(res), | ||
Err(_) => None, | ||
} | ||
tab.get_metatable() | ||
.ok()? | ||
.get::<LuaFunction>(LuaMetaMethod::ToString.name()) | ||
.ok()? | ||
.call(tab) | ||
.ok() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters