From 2c701038cd4a714b2eb0081066b1704256872270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Sch=C3=A4fer?= Date: Sat, 9 Mar 2024 09:00:36 +0100 Subject: [PATCH] Implement `as_str` on all Jid types This is useful for printing with quotes without copying any data. --- jid/CHANGELOG.md | 1 + jid/src/inner.rs | 5 +++++ jid/src/lib.rs | 17 +++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/jid/CHANGELOG.md b/jid/CHANGELOG.md index a316a397..6ad0ce31 100644 --- a/jid/CHANGELOG.md +++ b/jid/CHANGELOG.md @@ -20,6 +20,7 @@ Version xxx, release xxx: `impl From for BareJid` and `impl From for Jid`, both of which are (unlike `::from_parts`) copy-free. + - `as_str` methods have been added on all Jid types. Version 0.10.0, release 2023-08-17: * Breaking diff --git a/jid/src/inner.rs b/jid/src/inner.rs index 1ea83adc..781c940e 100644 --- a/jid/src/inner.rs +++ b/jid/src/inner.rs @@ -141,6 +141,11 @@ impl InnerJid { ResourceRef::from_str_unchecked(&self.normalized[slash + 1..]) }) } + + #[inline(always)] + pub(crate) fn as_str(&self) -> &str { + self.normalized.as_str() + } } impl FromStr for InnerJid { diff --git a/jid/src/lib.rs b/jid/src/lib.rs index c268af70..2c8de210 100644 --- a/jid/src/lib.rs +++ b/jid/src/lib.rs @@ -196,6 +196,13 @@ impl Jid { pub fn is_bare(&self) -> bool { !self.is_full() } + + /// Return a reference to the canonical string representation of the JID. + pub fn as_str(&self) -> &str { + match self { + Jid::Bare(BareJid { inner }) | Jid::Full(FullJid { inner }) => inner.as_str(), + } + } } impl TryFrom for FullJid { @@ -482,6 +489,11 @@ impl FullJid { self.inner.slash = None; BareJid { inner: self.inner } } + + /// Return a reference to the canonical string representation of the JID. + pub fn as_str(&self) -> &str { + self.inner.as_str() + } } impl FromStr for BareJid { @@ -608,6 +620,11 @@ impl BareJid { let resource = ResourcePart::new(resource)?; Ok(self.with_resource(&resource)) } + + /// Return a reference to the canonical string representation of the JID. + pub fn as_str(&self) -> &str { + self.inner.as_str() + } } #[cfg(feature = "minidom")]