Skip to content

Commit

Permalink
Use as_str() method to reference GeneralDnsNameRef contents
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
djc authored and cpu committed Sep 20, 2023
1 parent abf970c commit f470c07
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/end_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ mod tests {
EndEntityCert::try_from(der).expect("should parse end entity certificate correctly");

let mut names = cert.dns_names();
assert_eq!(names.next().map(<&str>::from), Some(name));
assert_eq!(names.next().map(<&str>::from), None);
assert_eq!(names.next(), Some(name));
assert_eq!(names.next(), None);
}
}
11 changes: 6 additions & 5 deletions src/subject_name/dns_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ pub enum GeneralDnsNameRef<'name> {
Wildcard(WildcardDnsNameRef<'name>),
}

impl<'a> From<GeneralDnsNameRef<'a>> for &'a str {
fn from(d: GeneralDnsNameRef<'a>) -> Self {
match d {
GeneralDnsNameRef::DnsName(name) => name.as_str(),
GeneralDnsNameRef::Wildcard(name) => name.as_str(),
impl<'a> GeneralDnsNameRef<'a> {
/// Yields the DNS name as a `&str`.
pub fn as_str(&self) -> &'a str {
match self {
Self::DnsName(name) => name.as_str(),
Self::Wildcard(name) => name.as_str(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/subject_name/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ pub(crate) fn list_cert_dns_names<'names>(
// if the name could be converted to a DNS name, return it; otherwise,
// keep going.
match DnsNameRef::try_from_ascii(presented_id.as_slice_less_safe()) {
Ok(dns_name) => Some(dns_name.into()),
Ok(dns_name) => Some(dns_name.as_str()),
Err(_) => match WildcardDnsNameRef::try_from_ascii(presented_id.as_slice_less_safe()) {
Ok(wildcard_dns_name) => Some(wildcard_dns_name.into()),
Ok(wildcard_dns_name) => Some(wildcard_dns_name.as_str()),
Err(_) => None,
},
}
Expand Down

0 comments on commit f470c07

Please sign in to comment.