Skip to content

Commit 8e2ceea

Browse files
author
Thomas Bahn
authored
Merge branch 'master' into cstring_conv
2 parents 296c3a8 + 4c36826 commit 8e2ceea

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
@@ -3,6 +3,8 @@ Unreleased
33
* Implement the `std::ops::AddAssign` trait for `AsciiString`.
44
* Implement the `IntoAsciiString` trait for `std::ffi::CStr` and `std::ffi::CString` types,
55
and implemented the `AsAsciiStr` trait for `std::ffi::CStr` type.
6+
* Implement the `IntoAsciiString` for `std::borrow::Cow`, where the inner types themselves
7+
implement `IntoAsciiString`.
68

79
Version 0.9.1 (2018-09-12)
810
==========================

src/ascii_string.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,41 @@ impl<'a> IntoAsciiString for &'a CStr {
854854
}
855855
}
856856

857+
impl<'a, B: ?Sized> IntoAsciiString for Cow<'a, B>
858+
where
859+
B: 'a + ToOwned,
860+
&'a B: IntoAsciiString,
861+
<B as ToOwned>::Owned: IntoAsciiString,
862+
{
863+
#[inline]
864+
unsafe fn into_ascii_string_unchecked(self) -> AsciiString {
865+
IntoAsciiString::into_ascii_string_unchecked(self.into_owned())
866+
}
867+
868+
fn into_ascii_string(self) -> Result<AsciiString, FromAsciiError<Self>> {
869+
match self {
870+
Cow::Owned(b) => {
871+
IntoAsciiString::into_ascii_string(b)
872+
.map_err(|FromAsciiError { error, owner }| {
873+
FromAsciiError {
874+
owner: Cow::Owned(owner),
875+
error: error,
876+
}
877+
})
878+
}
879+
Cow::Borrowed(b) => {
880+
IntoAsciiString::into_ascii_string(b)
881+
.map_err(|FromAsciiError { error, owner }| {
882+
FromAsciiError {
883+
owner: Cow::Borrowed(owner),
884+
error: error,
885+
}
886+
})
887+
}
888+
}
889+
}
890+
}
891+
857892
#[cfg(feature = "quickcheck")]
858893
impl Arbitrary for AsciiString {
859894
fn arbitrary<G: Gen>(g: &mut G) -> Self {

0 commit comments

Comments
 (0)