Skip to content

Commit 08faa34

Browse files
committed
Add more IntoAsciiString conversions
1 parent efaa11d commit 08faa34

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/ascii_string.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,20 @@ impl Into<Vec<u8>> for AsciiString {
448448
}
449449
}
450450

451+
impl<'a> From<&'a AsciiStr> for AsciiString {
452+
#[inline]
453+
fn from(s: &'a AsciiStr) -> Self {
454+
s.to_ascii_string()
455+
}
456+
}
457+
458+
impl<'a> From<&'a [AsciiChar]> for AsciiString {
459+
#[inline]
460+
fn from(s: &'a [AsciiChar]) -> AsciiString {
461+
s.into_iter().map(|c| *c).collect()
462+
}
463+
}
464+
451465
impl Into<String> for AsciiString {
452466
#[inline]
453467
fn into(self) -> String {
@@ -719,18 +733,29 @@ pub trait IntoAsciiString: Sized {
719733
fn into_ascii_string(self) -> Result<AsciiString, FromAsciiError<Self>>;
720734
}
721735

722-
impl IntoAsciiString for AsciiString {
736+
impl IntoAsciiString for Vec<AsciiChar> {
723737
#[inline]
724738
unsafe fn into_ascii_string_unchecked(self) -> AsciiString {
725-
self
739+
AsciiString::from(self)
740+
}
741+
#[inline]
742+
fn into_ascii_string(self) -> Result<AsciiString, FromAsciiError<Self>> {
743+
Ok(AsciiString::from(self))
744+
}
745+
}
746+
747+
impl<'a> IntoAsciiString for &'a [AsciiChar] {
748+
#[inline]
749+
unsafe fn into_ascii_string_unchecked(self) -> AsciiString {
750+
AsciiString::from(self)
726751
}
727752
#[inline]
728-
fn into_ascii_string(self) -> Result<Self, FromAsciiError<Self>> {
729-
Ok(self)
753+
fn into_ascii_string(self) -> Result<AsciiString, FromAsciiError<Self>> {
754+
Ok(AsciiString::from(self))
730755
}
731756
}
732757

733-
impl IntoAsciiString for Vec<AsciiChar> {
758+
impl<'a> IntoAsciiString for &'a AsciiStr {
734759
#[inline]
735760
unsafe fn into_ascii_string_unchecked(self) -> AsciiString {
736761
AsciiString::from(self)
@@ -771,6 +796,7 @@ macro_rules! impl_into_ascii_string {
771796
};
772797
}
773798

799+
impl_into_ascii_string!{AsciiString}
774800
impl_into_ascii_string!{Vec<u8>}
775801
impl_into_ascii_string!{'a, &'a [u8]}
776802
impl_into_ascii_string!{String}

0 commit comments

Comments
 (0)