diff options
author | Alex Orlenko <zxteam@protonmail.com> | 2022-04-08 16:43:09 +0100 |
---|---|---|
committer | Alex Orlenko <zxteam@protonmail.com> | 2022-04-08 20:02:18 +0100 |
commit | 28a063c1e568050a5e2e078076272d943c467444 (patch) | |
tree | ed51da9e5eeaf687ca6e764c13ab78def46d97bd /src | |
parent | 55fac90a748cc8c1b77166dc13da422f6b3906ea (diff) | |
download | mlua-28a063c1e568050a5e2e078076272d943c467444.zip |
Implement Hash for Lua String
Diffstat (limited to 'src')
-rw-r--r-- | src/string.rs | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/src/string.rs b/src/string.rs index e91416b..87234f0 100644 --- a/src/string.rs +++ b/src/string.rs @@ -1,4 +1,5 @@ -use std::borrow::Cow; +use std::borrow::{Borrow, Cow}; +use std::hash::{Hash, Hasher}; use std::string::String as StdString; use std::{slice, str}; @@ -119,6 +120,12 @@ impl<'lua> AsRef<[u8]> for String<'lua> { } } +impl<'lua> Borrow<[u8]> for String<'lua> { + fn borrow(&self) -> &[u8] { + self.as_bytes() + } +} + // Lua strings are basically &[u8] slices, so implement PartialEq for anything resembling that. // // This makes our `String` comparable with `Vec<u8>`, `[u8]`, `&str`, `String` and `mlua::String` @@ -136,6 +143,14 @@ where } } +impl<'lua> Eq for String<'lua> {} + +impl<'lua> Hash for String<'lua> { + fn hash<H: Hasher>(&self, state: &mut H) { + self.as_bytes().hash(state); + } +} + #[cfg(feature = "serialize")] impl<'lua> Serialize for String<'lua> { fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error> |