Skip to content

Commit 7a88490

Browse files
djccpu
authored andcommitted
Use as_str() method to reference GeneralDnsNameRef contents
The From impl feels a little unidiomatic because the GeneralDnsNameRef 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 d078ed3 commit 7a88490

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

src/end_entity.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ mod tests {
212212
EndEntityCert::try_from(der).expect("should parse end entity certificate correctly");
213213

214214
let mut names = cert.dns_names();
215-
assert_eq!(names.next().map(<&str>::from), Some(name));
216-
assert_eq!(names.next().map(<&str>::from), None);
215+
assert_eq!(names.next(), Some(name));
216+
assert_eq!(names.next(), None);
217217
}
218218
}

src/subject_name/dns_name.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ pub enum GeneralDnsNameRef<'name> {
2828
Wildcard(WildcardDnsNameRef<'name>),
2929
}
3030

31-
impl<'a> From<GeneralDnsNameRef<'a>> for &'a str {
32-
fn from(d: GeneralDnsNameRef<'a>) -> Self {
33-
match d {
34-
GeneralDnsNameRef::DnsName(name) => name.as_str(),
35-
GeneralDnsNameRef::Wildcard(name) => name.as_str(),
31+
impl<'a> GeneralDnsNameRef<'a> {
32+
/// Yields the DNS name as a `&str`.
33+
pub fn as_str(&self) -> &'a str {
34+
match self {
35+
Self::DnsName(name) => name.as_str(),
36+
Self::Wildcard(name) => name.as_str(),
3637
}
3738
}
3839
}

src/subject_name/verify.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,9 @@ pub(crate) fn list_cert_dns_names<'names>(
325325
// if the name could be converted to a DNS name, return it; otherwise,
326326
// keep going.
327327
match DnsNameRef::try_from_ascii(presented_id.as_slice_less_safe()) {
328-
Ok(dns_name) => Some(dns_name.into()),
328+
Ok(dns_name) => Some(dns_name.as_str()),
329329
Err(_) => match WildcardDnsNameRef::try_from_ascii(presented_id.as_slice_less_safe()) {
330-
Ok(wildcard_dns_name) => Some(wildcard_dns_name.into()),
330+
Ok(wildcard_dns_name) => Some(wildcard_dns_name.as_str()),
331331
Err(_) => None,
332332
},
333333
}

0 commit comments

Comments
 (0)