Make MetaMethod::name() public

Tests for UserDataMetatable::pairs()
This commit is contained in:
Alex Orlenko 2021-02-20 22:42:09 +00:00
parent 1635903d3f
commit decb5b9e37
2 changed files with 11 additions and 2 deletions

View File

@ -135,7 +135,8 @@ impl fmt::Display for MetaMethod {
} }
impl MetaMethod { impl MetaMethod {
pub(crate) fn name(&self) -> &str { /// Returns Lua metamethod name, usually prefixed by two underscores.
pub fn name(&self) -> &str {
match self { match self {
MetaMethod::Add => "__add", MetaMethod::Add => "__add",
MetaMethod::Sub => "__sub", MetaMethod::Sub => "__sub",
@ -871,7 +872,7 @@ impl<'lua> UserDataMetatable<'lua> {
/// The pairs are wrapped in a [`Result`], since they are lazily converted to `V` type. /// The pairs are wrapped in a [`Result`], since they are lazily converted to `V` type.
/// ///
/// [`Result`]: type.Result.html /// [`Result`]: type.Result.html
pub fn pairs<K: FromLua<'lua>, V: FromLua<'lua>>(self) -> UserDataMetatablePairs<'lua, V> { pub fn pairs<V: FromLua<'lua>>(self) -> UserDataMetatablePairs<'lua, V> {
UserDataMetatablePairs(self.0.pairs()) UserDataMetatablePairs(self.0.pairs())
} }
} }

View File

@ -416,6 +416,14 @@ fn test_metatable() -> Result<()> {
Err(e) => panic!("expected MetaMethodRestricted, got {:?}", e), Err(e) => panic!("expected MetaMethodRestricted, got {:?}", e),
} }
let mut methods = metatable
.pairs()
.into_iter()
.map(|kv: Result<(_, Value)>| Ok(kv?.0))
.collect::<Result<Vec<_>>>()?;
methods.sort_by_cached_key(|k| k.name().to_owned());
assert_eq!(methods, vec![MetaMethod::Index, "__type_name".into()]);
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
struct MyUserData2(i64); struct MyUserData2(i64);