diff --git a/src/lib.rs b/src/lib.rs index a5ca511..668530d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,6 +26,26 @@ where Self::Owned(t) => std::borrow::Cow::Owned(t), } } + + pub fn to_mut(&mut self) -> &mut ::Owned { + match *self { + Self::Borrowed(t) => { + *self = Self::Owned(t.to_owned()); + match self { + Self::Owned(ref mut t) => t, + _ => unreachable!(), + } + } + Self::Ephemeral(t) => { + *self = Self::Owned(t.to_owned()); + match self { + Self::Owned(ref mut t) => t, + _ => unreachable!(), + } + } + Self::Owned(ref mut t) => t, + } + } } impl<'a, 'b, T> AsRef for TriCow<'a, 'b, T> @@ -95,19 +115,15 @@ mod tests { fn with_cow<'b>(cow: TriCow<'a, 'b, [u8]>) -> Self; } - struct A<'a>(TriCow<'a, 'static, [u8]>); + struct A<'a>(std::borrow::Cow<'a, [u8]>); struct B<'a>(A<'a>); - struct C<'a>(B<'a>, TriCow<'a, 'static, [u8]>); + struct C<'a>(B<'a>, std::borrow::Cow<'a, [u8]>); struct D<'a>(C<'a>); - struct E<'a>(D<'a>, TriCow<'a, 'static, [u8]>); + struct E<'a>(D<'a>, std::borrow::Cow<'a, [u8]>); impl<'a> WithCow<'a> for A<'a> { fn with_cow<'b>(cow: TriCow<'a, 'b, [u8]>) -> Self { - match cow { - TriCow::Borrowed(bytes) => Self(TriCow::Borrowed(bytes)), - TriCow::Ephemeral(bytes) => Self(TriCow::Owned(bytes.to_vec())), - TriCow::Owned(bytes) => Self(TriCow::Owned(bytes)), - } + Self(cow.into_std()) } } @@ -124,21 +140,21 @@ mod tests { let (first, second) = bytes.split_at(2); Self( B::with_cow(TriCow::Borrowed(first)), - TriCow::Borrowed(second), + std::borrow::Cow::Borrowed(second), ) } TriCow::Ephemeral(bytes) => { let (first, second) = bytes.split_at(2); Self( B::with_cow(TriCow::Ephemeral(first)), - TriCow::Owned(Vec::from(second)), + std::borrow::Cow::Owned(Vec::from(second)), ) } TriCow::Owned(bytes) => { let (first, second) = bytes.split_at(2); Self( B::with_cow(TriCow::Ephemeral(first)), - TriCow::Owned(Vec::from(second)), + std::borrow::Cow::Owned(Vec::from(second)), ) } } @@ -158,21 +174,21 @@ mod tests { let (first, second) = bytes.split_at(2); Self( D::with_cow(TriCow::Borrowed(first)), - TriCow::Borrowed(second), + std::borrow::Cow::Borrowed(second), ) } TriCow::Ephemeral(bytes) => { let (first, second) = bytes.split_at(2); Self( D::with_cow(TriCow::Ephemeral(first)), - TriCow::Owned(Vec::from(second)), + std::borrow::Cow::Owned(Vec::from(second)), ) } TriCow::Owned(bytes) => { let (first, second) = bytes.split_at(2); Self( D::with_cow(TriCow::Ephemeral(first)), - TriCow::Owned(Vec::from(second)), + std::borrow::Cow::Owned(Vec::from(second)), ) } }