Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/kv/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl ToKey for str {
/// A key in a key-value.
// These impls must only be based on the as_str() representation of the key
// If a new field (such as an optional index) is added to the key they must not affect comparison
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Eq, Ord, Hash)]
pub struct Key<'k> {
key: MaybeStaticStr<'k>,
}
Expand Down Expand Up @@ -107,6 +107,18 @@ impl<'k> From<&'k str> for Key<'k> {
}
}

impl PartialEq for Key<'_> {
Comment thread
matteo-zeggiotti-ok marked this conversation as resolved.
Outdated
fn eq(&self, other: &Self) -> bool {
self.key.get() == other.key.get()
}
}

impl PartialOrd for Key<'_> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.key.get().cmp(other.key.get()))
}
}

#[cfg(feature = "std")]
mod std_support {
use super::*;
Expand Down Expand Up @@ -178,4 +190,24 @@ mod tests {
fn key_to_borrowed() {
assert_eq!("a key", Key::from_str("a key").to_borrowed_str().unwrap());
}

#[test]
fn key_static_eq() {
assert_eq!(Key::from_str_static("a key"), Key::from_str_static("a key"));
}

#[test]
fn key_borrowed_eq() {
let k1 = String::from("a key");
let k2 = String::from("a key");
assert_eq!(Key::from_str(&k1), Key::from_str(&k2));
}

#[test]
fn key_borrowed_and_static_eq() {
const KEY: &str = "a key";
let static_key = Key::from_str_static(KEY);
let borrowed_key = Key::from_str("a key");
assert_eq!(static_key, borrowed_key);
}
}