Skip to content

Commit 72824a4

Browse files
committed
Remove custom PartialEq for LuaString and use derived one
Lua can compare strings using `lua_rawequal` and it's more efficient than always compare bytes. Under the hood Lua compare pointers for interned strings and content for long ones. Close #694
1 parent c54b906 commit 72824a4

2 files changed

Lines changed: 24 additions & 20 deletions

File tree

src/string.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use {
2323
/// Handle to an internal Lua string.
2424
///
2525
/// Unlike Rust strings, Lua strings may not be valid UTF-8.
26-
#[derive(Clone)]
26+
#[derive(Clone, PartialEq)]
2727
pub struct LuaString(pub(crate) ValueRef);
2828

2929
impl LuaString {
@@ -186,12 +186,6 @@ where
186186
}
187187
}
188188

189-
impl PartialEq for LuaString {
190-
fn eq(&self, other: &LuaString) -> bool {
191-
self.as_bytes() == other.as_bytes()
192-
}
193-
}
194-
195189
impl Eq for LuaString {}
196190

197191
impl<T> PartialOrd<T> for LuaString

tests/string.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,37 @@ use mlua::{Lua, LuaString, Result};
55

66
#[test]
77
fn test_string_compare() {
8-
fn with_str<F: FnOnce(LuaString)>(s: &str, f: F) {
9-
f(Lua::new().create_string(s).unwrap());
8+
let lua = Lua::new();
9+
10+
fn with_str<F: FnOnce(LuaString)>(lua: &Lua, s: &str, f: F) {
11+
f(lua.create_string(s).unwrap());
1012
}
1113

1214
// Tests that all comparisons we want to have are usable
13-
with_str("teststring", |t| assert_eq!(t, "teststring")); // &str
14-
with_str("teststring", |t| assert_eq!(t, b"teststring")); // &[u8]
15-
with_str("teststring", |t| assert_eq!(t, b"teststring".to_vec())); // Vec<u8>
16-
with_str("teststring", |t| assert_eq!(t, "teststring".to_string())); // String
17-
with_str("teststring", |t| assert_eq!(t, t)); // mlua::String
18-
with_str("teststring", |t| assert_eq!(t, Cow::from(b"teststring".as_ref()))); // Cow (borrowed)
19-
with_str("bla", |t| assert_eq!(t, Cow::from(b"bla".to_vec()))); // Cow (owned)
15+
with_str(&lua, "teststring", |t| assert_eq!(t, "teststring")); // &str
16+
with_str(&lua, "teststring", |t| assert_eq!(t, b"teststring")); // &[u8]
17+
with_str(&lua, "teststring", |t| assert_eq!(t, b"teststring".to_vec())); // Vec<u8>
18+
with_str(&lua, "teststring", |t| assert_eq!(t, "teststring".to_string())); // String
19+
with_str(&lua, "teststring", |t| assert_eq!(t, t)); // mlua::String
20+
with_str(&lua, "teststring", |t| {
21+
assert_eq!(t, Cow::from(b"teststring".as_ref())) // Cow (borrowed)
22+
});
23+
with_str(&lua, "bla", |t| assert_eq!(t, Cow::from(b"bla".to_vec()))); // Cow (owned)
2024

2125
// Test ordering
22-
with_str("a", |a| {
26+
with_str(&lua, "a", |a| {
2327
assert!(!(a < a));
2428
assert!(!(a > a));
2529
});
26-
with_str("a", |a| assert!(a < "b"));
27-
with_str("a", |a| assert!(a < b"b"));
28-
with_str("a", |a| with_str("b", |b| assert!(a < b)));
30+
with_str(&lua, "a", |a| assert!(a < "b"));
31+
with_str(&lua, "a", |a| assert!(a < b"b"));
32+
with_str(&lua, "a", |a| with_str(&lua, "b", |b| assert!(a < b)));
33+
34+
// Long strings (not interned by Lua)
35+
let long_str = "abc".repeat(100);
36+
with_str(&lua, &long_str, |s1| {
37+
with_str(&lua, &long_str, |s2| assert_eq!(s1, s2))
38+
});
2939
}
3040

3141
#[test]

0 commit comments

Comments
 (0)