Skip to content

dns: Avoid allocating in Name::is_localhost #1303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2021
Merged
Changes from all commits
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
8 changes: 5 additions & 3 deletions linkerd/dns/name/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ pub struct Name(webpki::DNSName);
pub struct InvalidName;

impl Name {
#[inline]
pub fn is_localhost(&self) -> bool {
use std::str::FromStr;
*self == Name::from_str("localhost.").unwrap()
self.as_ref().eq_ignore_ascii_case("localhost.")
}

#[inline]
pub fn without_trailing_dot(&self) -> &str {
self.as_ref().trim_end_matches('.')
}
Expand Down Expand Up @@ -52,7 +53,7 @@ impl<'a> TryFrom<&'a [u8]> for Name {
}
}

impl<'a> std::str::FromStr for Name {
impl std::str::FromStr for Name {
type Err = InvalidName;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::try_from(s.as_bytes())
Expand All @@ -72,6 +73,7 @@ impl<'t> From<&'t Name> for webpki::DNSNameRef<'t> {
}

impl AsRef<str> for Name {
#[inline]
fn as_ref(&self) -> &str {
<webpki::DNSName as AsRef<str>>::as_ref(&self.0)
}
Expand Down