mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
jid: implement Borrow<Jid> on FullJid and BareJid
This allows to use them interchangably when looking up keys in hash sets and dicts.
This commit is contained in:
parent
2e9c9411a3
commit
c895cb1009
1 changed files with 56 additions and 2 deletions
|
@ -31,7 +31,7 @@
|
||||||
//! mixing left-to-write and right-to-left characters
|
//! mixing left-to-write and right-to-left characters
|
||||||
|
|
||||||
use core::num::NonZeroU16;
|
use core::num::NonZeroU16;
|
||||||
use std::borrow::Cow;
|
use std::borrow::{Borrow, Cow};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
@ -482,6 +482,18 @@ impl Deref for BareJid {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Borrow<Jid> for FullJid {
|
||||||
|
fn borrow(&self) -> &Jid {
|
||||||
|
&self.inner
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Borrow<Jid> for BareJid {
|
||||||
|
fn borrow(&self) -> &Jid {
|
||||||
|
&self.inner
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Debug for FullJid {
|
impl fmt::Debug for FullJid {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt.debug_tuple("FullJid").field(&self.inner).finish()
|
fmt.debug_tuple("FullJid").field(&self.inner).finish()
|
||||||
|
@ -826,7 +838,7 @@ impl From<BareJid> for Node {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
macro_rules! assert_size (
|
macro_rules! assert_size (
|
||||||
($t:ty, $sz:expr) => (
|
($t:ty, $sz:expr) => (
|
||||||
|
@ -1137,4 +1149,46 @@ mod tests {
|
||||||
other => panic!("unexpected result: {:?}", other),
|
other => panic!("unexpected result: {:?}", other),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn lookup_jid_by_full_jid() {
|
||||||
|
let mut map: HashSet<Jid> = HashSet::new();
|
||||||
|
let jid1 = Jid::new("foo@bar").unwrap();
|
||||||
|
let jid2 = Jid::new("foo@bar/baz").unwrap();
|
||||||
|
let jid3 = FullJid::new("foo@bar/baz").unwrap();
|
||||||
|
|
||||||
|
map.insert(jid1);
|
||||||
|
assert!(!map.contains(&jid2));
|
||||||
|
assert!(!map.contains(&jid3));
|
||||||
|
map.insert(jid2);
|
||||||
|
assert!(map.contains(&jid3));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn lookup_full_jid_by_jid() {
|
||||||
|
let mut map: HashSet<FullJid> = HashSet::new();
|
||||||
|
let jid1 = FullJid::new("foo@bar/baz").unwrap();
|
||||||
|
let jid2 = FullJid::new("foo@bar/fnord").unwrap();
|
||||||
|
let jid3 = Jid::new("foo@bar/fnord").unwrap();
|
||||||
|
|
||||||
|
map.insert(jid1);
|
||||||
|
assert!(!map.contains(&jid2));
|
||||||
|
assert!(!map.contains(&jid3));
|
||||||
|
map.insert(jid2);
|
||||||
|
assert!(map.contains(&jid3));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn lookup_bare_jid_by_jid() {
|
||||||
|
let mut map: HashSet<BareJid> = HashSet::new();
|
||||||
|
let jid1 = BareJid::new("foo@bar").unwrap();
|
||||||
|
let jid2 = BareJid::new("foo@baz").unwrap();
|
||||||
|
let jid3 = Jid::new("foo@baz").unwrap();
|
||||||
|
|
||||||
|
map.insert(jid1);
|
||||||
|
assert!(!map.contains(&jid2));
|
||||||
|
assert!(!map.contains(&jid3));
|
||||||
|
map.insert(jid2);
|
||||||
|
assert!(map.contains(&jid3));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue