Skip to content

Instantly share code, notes, and snippets.

@valarauca
Forked from Lucretiel/percent-cow.rs
Created April 29, 2021 03:41
Show Gist options
  • Save valarauca/7d4275d7f23972d8f213d2d5d7e1bf1e to your computer and use it in GitHub Desktop.
Save valarauca/7d4275d7f23972d8f213d2d5d7e1bf1e to your computer and use it in GitHub Desktop.
impl<'i, B> FromPercentEncoded<'i> for Cow<'i, B>
where
B: ToOwned,
&'i B: FromPercentEncoded<'i>,
<&'i B as FromPercentEncoded<'i>>::StrError: MaybeCannotGrowError,
<&'i B as FromPercentEncoded<'i>>::BytesError: MaybeCannotGrowError,
B::Owned: FromPercentEncoded<'i>,
{
type BytesError = CowError<
<B::Owned as FromPercentEncoded<'i>>::BytesError,
<&'i B as FromPercentEncoded<'i>>::BytesError,
>;
type StrError = CowError<
<B::Owned as FromPercentEncoded<'i>>::StrError,
<&'i B as FromPercentEncoded<'i>>::StrError,
>;
fn append_str(&mut self, s: &'i str) -> Result<(), Self::StrError> {
let owned = match *self {
Cow::Owned(ref mut owned) => owned,
Cow::Borrowed(ref mut borrowed) => match borrowed.append_str(s) {
Ok(()) => return Ok(()),
Err(err) if err.cannot_grow() => self.to_mut(),
Err(err) => return Err(CowError::BorrowedError(err)),
},
};
owned.append_str(s).map_err(CowError::OwnedError)
}
fn append_bytes(&mut self, b: &'i [u8]) -> Result<(), Self::BytesError> {
let owned = match self {
Cow::Owned(owned) => owned,
Cow::Borrowed(borrowed) => match borrowed.append_bytes(b) {
Ok(()) => return Ok(()),
Err(err) if err.cannot_grow() => self.to_mut(),
Err(err) => return Err(CowError::BorrowedError(err)),
},
};
owned.append_bytes(b).map_err(CowError::OwnedError)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment