Skip to content

Commit

Permalink
Remove generic from Table::equals and Value::equals
Browse files Browse the repository at this point in the history
  • Loading branch information
khvzak committed Oct 16, 2024
1 parent f9ae4bf commit 179c54f
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 36 deletions.
12 changes: 2 additions & 10 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,12 @@ impl Table {
/// # Ok(())
/// # }
/// ```
pub fn equals<T: AsRef<Self>>(&self, other: T) -> Result<bool> {
let other = other.as_ref();
pub fn equals(&self, other: &Self) -> Result<bool> {
if self == other {
return Ok(true);
}

// Compare using __eq metamethod if exists
// Compare using `__eq` metamethod if exists
// First, check the self for the metamethod.
// If self does not define it, then check the other table.
if let Some(mt) = self.metatable() {
Expand Down Expand Up @@ -811,13 +810,6 @@ impl fmt::Debug for Table {
}
}

impl AsRef<Table> for Table {
#[inline]
fn as_ref(&self) -> &Self {
self
}
}

impl<T> PartialEq<[T]> for Table
where
T: IntoLua + Clone,
Expand Down
18 changes: 5 additions & 13 deletions src/userdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ impl AnyUserData {
/// [`UserDataMetatable`]: crate::UserDataMetatable
#[inline]
pub fn metatable(&self) -> Result<UserDataMetatable> {
self.get_raw_metatable().map(UserDataMetatable)
self.raw_metatable().map(UserDataMetatable)
}

#[doc(hidden)]
Expand All @@ -911,7 +911,7 @@ impl AnyUserData {
self.metatable()
}

fn get_raw_metatable(&self) -> Result<Table> {
fn raw_metatable(&self) -> Result<Table> {
let lua = self.0.lua.lock();
let state = lua.state();
unsafe {
Expand Down Expand Up @@ -958,15 +958,14 @@ impl AnyUserData {
}
}

pub(crate) fn equals<T: AsRef<Self>>(&self, other: T) -> Result<bool> {
let other = other.as_ref();
pub(crate) fn equals(&self, other: &Self) -> Result<bool> {
// Uses lua_rawequal() under the hood
if self == other {
return Ok(true);
}

let mt = self.get_raw_metatable()?;
if mt != other.get_raw_metatable()? {
let mt = self.raw_metatable()?;
if mt != other.raw_metatable()? {
return Ok(false);
}

Expand Down Expand Up @@ -1010,13 +1009,6 @@ impl AnyUserData {
}
}

impl AsRef<AnyUserData> for AnyUserData {
#[inline]
fn as_ref(&self) -> &Self {
self
}
}

/// Handle to a `UserData` metatable.
#[derive(Clone, Debug)]
pub struct UserDataMetatable(pub(crate) Table);
Expand Down
15 changes: 4 additions & 11 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ impl Value {
/// Compares two values for equality.
///
/// Equality comparisons do not convert strings to numbers or vice versa.
/// Tables, Functions, Threads, and Userdata are compared by reference:
/// Tables, Functions, Threads, and UserData are compared by reference:
/// two objects are considered equal only if they are the same object.
///
/// If Tables or Userdata have `__eq` metamethod then mlua will try to invoke it.
/// If Tables or UserData have `__eq` metamethod then mlua will try to invoke it.
/// The first value is checked first. If that value does not define a metamethod
/// for `__eq`, then mlua will check the second value.
/// Then mlua calls the metamethod with the two values as arguments, if found.
pub fn equals<T: AsRef<Self>>(&self, other: T) -> Result<bool> {
match (self, other.as_ref()) {
pub fn equals(&self, other: &Self) -> Result<bool> {
match (self, other) {
(Value::Table(a), Value::Table(b)) => a.equals(b),
(Value::UserData(a), Value::UserData(b)) => a.equals(b),
(a, b) => Ok(a == b),
Expand Down Expand Up @@ -610,13 +610,6 @@ impl PartialEq for Value {
}
}

impl AsRef<Value> for Value {
#[inline]
fn as_ref(&self) -> &Self {
self
}
}

/// A wrapped [`Value`] with customized serialization behavior.
#[cfg(feature = "serialize")]
#[cfg_attr(docsrs, doc(cfg(feature = "serialize")))]
Expand Down
2 changes: 1 addition & 1 deletion tests/userdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ fn test_metamethods() -> Result<()> {

assert!(lua.load("userdata2 == userdata3").eval::<bool>()?);
assert!(userdata2 != userdata3); // because references are differ
assert!(userdata2.equals(userdata3)?);
assert!(userdata2.equals(&userdata3)?);

let userdata1: AnyUserData = globals.get("userdata1")?;
assert!(userdata1.metatable()?.contains(MetaMethod::Add)?);
Expand Down
2 changes: 1 addition & 1 deletion tests/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn test_value_eq() -> Result<()> {
assert!(string1 == string2);
assert!(string1.equals(&string2)?);
assert!(num1 == num2);
assert!(num1.equals(num2)?);
assert!(num1.equals(&num2)?);
assert!(num1 != num3);
assert!(func1 == func2);
assert!(func1 != func3);
Expand Down

0 comments on commit 179c54f

Please sign in to comment.