Skip to content

Commit decb5b9

Browse files
committed
Make MetaMethod::name() public
Tests for UserDataMetatable::pairs()
1 parent 1635903 commit decb5b9

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/userdata.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ impl fmt::Display for MetaMethod {
135135
}
136136

137137
impl MetaMethod {
138-
pub(crate) fn name(&self) -> &str {
138+
/// Returns Lua metamethod name, usually prefixed by two underscores.
139+
pub fn name(&self) -> &str {
139140
match self {
140141
MetaMethod::Add => "__add",
141142
MetaMethod::Sub => "__sub",
@@ -871,7 +872,7 @@ impl<'lua> UserDataMetatable<'lua> {
871872
/// The pairs are wrapped in a [`Result`], since they are lazily converted to `V` type.
872873
///
873874
/// [`Result`]: type.Result.html
874-
pub fn pairs<K: FromLua<'lua>, V: FromLua<'lua>>(self) -> UserDataMetatablePairs<'lua, V> {
875+
pub fn pairs<V: FromLua<'lua>>(self) -> UserDataMetatablePairs<'lua, V> {
875876
UserDataMetatablePairs(self.0.pairs())
876877
}
877878
}

tests/userdata.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,14 @@ fn test_metatable() -> Result<()> {
416416
Err(e) => panic!("expected MetaMethodRestricted, got {:?}", e),
417417
}
418418

419+
let mut methods = metatable
420+
.pairs()
421+
.into_iter()
422+
.map(|kv: Result<(_, Value)>| Ok(kv?.0))
423+
.collect::<Result<Vec<_>>>()?;
424+
methods.sort_by_cached_key(|k| k.name().to_owned());
425+
assert_eq!(methods, vec![MetaMethod::Index, "__type_name".into()]);
426+
419427
#[derive(Copy, Clone)]
420428
struct MyUserData2(i64);
421429

0 commit comments

Comments
 (0)