Skip to content

Instantly share code, notes, and snippets.

@Lucretiel
Created April 29, 2021 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Lucretiel/335821c39f45d864971a13d217d83713 to your computer and use it in GitHub Desktop.
Save Lucretiel/335821c39f45d864971a13d217d83713 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