Skip to content

Commit 219a115

Browse files
committed
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 0754c79 commit 219a115

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

src/end_entity.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ mod tests {
217217
let mut names = cert
218218
.dns_names()
219219
.expect("should get all DNS names correctly for end entity cert");
220-
assert_eq!(names.next().map(<&str>::from), Some(name));
221-
assert_eq!(names.next().map(<&str>::from), None);
220+
assert_eq!(names.next().map(|name| name.as_str()), Some(name));
221+
assert_eq!(names.next().map(|name| name.as_str()), None);
222222
}
223223
}

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
}

tests/integration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ fn expect_cert_dns_names(data: &[u8], expected_names: &[&str]) {
351351
assert_eq!(actual_names.len(), expected_names.len());
352352

353353
let actual_names: std::collections::HashSet<&str> =
354-
actual_names.drain(..).map(|name| name.into()).collect();
354+
actual_names.drain(..).map(|name| name.as_str()).collect();
355355

356356
assert_eq!(actual_names, expected_names);
357357
}

0 commit comments

Comments
 (0)