Skip to content

Commit 4c36826

Browse files
burtonageoThomas Bahn
authored and
Thomas Bahn
committed
Implement IntoAsciiString for Cow, where the inner types themselves implement IntoAsciiString
1 parent 55f8305 commit 4c36826

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

RELEASES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Unreleased
22
==========
33
* Implement the `std::ops::AddAssign` trait for `AsciiString`.
4+
* Implement the `IntoAsciiString` for `std::borrow::Cow`, where the inner types themselves
5+
implement `IntoAsciiString`.
46

57
Version 0.9.1 (2018-09-12)
68
==========================

src/ascii_string.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,41 @@ impl<'a> IntoAsciiString for &'a str {
784784
}
785785
}
786786

787+
impl<'a, B: ?Sized> IntoAsciiString for Cow<'a, B>
788+
where
789+
B: 'a + ToOwned,
790+
&'a B: IntoAsciiString,
791+
<B as ToOwned>::Owned: IntoAsciiString,
792+
{
793+
#[inline]
794+
unsafe fn into_ascii_string_unchecked(self) -> AsciiString {
795+
IntoAsciiString::into_ascii_string_unchecked(self.into_owned())
796+
}
797+
798+
fn into_ascii_string(self) -> Result<AsciiString, FromAsciiError<Self>> {
799+
match self {
800+
Cow::Owned(b) => {
801+
IntoAsciiString::into_ascii_string(b)
802+
.map_err(|FromAsciiError { error, owner }| {
803+
FromAsciiError {
804+
owner: Cow::Owned(owner),
805+
error: error,
806+
}
807+
})
808+
}
809+
Cow::Borrowed(b) => {
810+
IntoAsciiString::into_ascii_string(b)
811+
.map_err(|FromAsciiError { error, owner }| {
812+
FromAsciiError {
813+
owner: Cow::Borrowed(owner),
814+
error: error,
815+
}
816+
})
817+
}
818+
}
819+
}
820+
}
821+
787822
#[cfg(feature = "quickcheck")]
788823
impl Arbitrary for AsciiString {
789824
fn arbitrary<G: Gen>(g: &mut G) -> Self {

0 commit comments

Comments
 (0)