Skip to content

Commit d078ed3

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

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

src/subject_name/dns_name.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<'a> From<GeneralDnsNameRef<'a>> for &'a str {
3232
fn from(d: GeneralDnsNameRef<'a>) -> Self {
3333
match d {
3434
GeneralDnsNameRef::DnsName(name) => name.as_str(),
35-
GeneralDnsNameRef::Wildcard(name) => name.into(),
35+
GeneralDnsNameRef::Wildcard(name) => name.as_str(),
3636
}
3737
}
3838
}
@@ -168,6 +168,13 @@ impl<'a> WildcardDnsNameRef<'a> {
168168
pub fn try_from_ascii_str(dns_name: &'a str) -> Result<Self, InvalidDnsNameError> {
169169
Self::try_from_ascii(dns_name.as_bytes())
170170
}
171+
172+
/// Yields a reference to the DNS name as a `&str`.
173+
pub fn as_str(&self) -> &'a str {
174+
// The unwrap won't fail because a `WildcardDnsNameRef` is guaranteed to be ASCII and
175+
// ASCII is a subset of UTF-8.
176+
core::str::from_utf8(self.0).unwrap()
177+
}
171178
}
172179

173180
impl core::fmt::Debug for WildcardDnsNameRef<'_> {
@@ -185,23 +192,6 @@ impl core::fmt::Debug for WildcardDnsNameRef<'_> {
185192
}
186193
}
187194

188-
impl<'a> From<WildcardDnsNameRef<'a>> for &'a str {
189-
fn from(WildcardDnsNameRef(d): WildcardDnsNameRef<'a>) -> Self {
190-
// The unwrap won't fail because WildcardDnsNameRef are guaranteed to be ASCII
191-
// and ASCII is a subset of UTF-8.
192-
core::str::from_utf8(d).unwrap()
193-
}
194-
}
195-
196-
impl AsRef<str> for WildcardDnsNameRef<'_> {
197-
#[inline]
198-
fn as_ref(&self) -> &str {
199-
// The unwrap won't fail because WildcardDnsNameRef are guaranteed to be ASCII
200-
// and ASCII is a subset of UTF-8.
201-
core::str::from_utf8(self.0).unwrap()
202-
}
203-
}
204-
205195
/// An error indicating that a `DnsNameRef` could not built because the input
206196
/// is not a syntactically-valid DNS Name.
207197
#[derive(Clone, Copy, Debug, Eq, PartialEq)]

0 commit comments

Comments
 (0)