Skip to content

Commit 51f23a4

Browse files
committed
Use as_str() method to reference DnsNameRef contents
The From impl feels a little unidiomatic because the DnsNameRef is not consumed. An AsRef impl would unnecessarily constrain the lifetime of the output value to `&self`, whereas it can live as long as `'a`.
1 parent 4e169bb commit 51f23a4

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

src/subject_name/dns_name.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub enum GeneralDnsNameRef<'name> {
3131
impl<'a> From<GeneralDnsNameRef<'a>> for &'a str {
3232
fn from(d: GeneralDnsNameRef<'a>) -> Self {
3333
match d {
34-
GeneralDnsNameRef::DnsName(name) => name.into(),
34+
GeneralDnsNameRef::DnsName(name) => name.as_str(),
3535
GeneralDnsNameRef::Wildcard(name) => name.into(),
3636
}
3737
}
@@ -82,15 +82,6 @@ impl AsRef<str> for DnsName {
8282
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
8383
pub struct DnsNameRef<'a>(pub(crate) &'a [u8]);
8484

85-
impl AsRef<str> for DnsNameRef<'_> {
86-
#[inline]
87-
fn as_ref(&self) -> &str {
88-
// The unwrap won't fail because DnsNameRef are guaranteed to be ASCII
89-
// and ASCII is a subset of UTF-8.
90-
core::str::from_utf8(self.0).unwrap()
91-
}
92-
}
93-
9485
impl<'a> DnsNameRef<'a> {
9586
/// Constructs a `DnsNameRef` from the given input if the input is a
9687
/// syntactically-valid DNS name.
@@ -115,10 +106,15 @@ impl<'a> DnsNameRef<'a> {
115106
/// Constructs a `DnsName` from this `DnsNameRef`
116107
#[cfg(feature = "alloc")]
117108
pub fn to_owned(&self) -> DnsName {
118-
// DnsNameRef is already guaranteed to be valid ASCII, which is a
119-
// subset of UTF-8.
120-
let s: &str = (*self).into();
121-
DnsName(s.to_ascii_lowercase())
109+
// DnsNameRef is already guaranteed to be valid ASCII, which is subset of UTF-8.
110+
DnsName(self.as_str().to_ascii_lowercase())
111+
}
112+
113+
/// Yields a reference to the DNS name as a `&str`.
114+
pub fn as_str(&self) -> &'a str {
115+
// The unwrap won't fail because `DnsNameRef` values are guaranteed to be ASCII and ASCII
116+
// is a subset of UTF-8.
117+
core::str::from_utf8(self.0).unwrap()
122118
}
123119
}
124120

@@ -137,14 +133,6 @@ impl core::fmt::Debug for DnsNameRef<'_> {
137133
}
138134
}
139135

140-
impl<'a> From<DnsNameRef<'a>> for &'a str {
141-
fn from(DnsNameRef(d): DnsNameRef<'a>) -> Self {
142-
// The unwrap won't fail because DnsNameRefs are guaranteed to be ASCII
143-
// and ASCII is a subset of UTF-8.
144-
core::str::from_utf8(d).unwrap()
145-
}
146-
}
147-
148136
/// A reference to a DNS Name presented by a server that may include a wildcard.
149137
///
150138
/// A `WildcardDnsNameRef` is guaranteed to be syntactically valid. The validity rules

src/subject_name/verify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(crate) fn verify_cert_dns_name(
2424
dns_name: DnsNameRef,
2525
) -> Result<(), Error> {
2626
let cert = cert.inner();
27-
let dns_name = untrusted::Input::from(dns_name.as_ref().as_bytes());
27+
let dns_name = untrusted::Input::from(dns_name.as_str().as_bytes());
2828
NameIterator::new(Some(cert.subject), cert.subject_alt_name)
2929
.find_map(|result| {
3030
let name = match result {

0 commit comments

Comments
 (0)