Skip to content

Commit

Permalink
Implement conversion from cow to std cow (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dav1dde authored May 5, 2024
1 parent cd7de8c commit 3fc0b4f
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions metrics/src/cow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,20 @@ impl<'a> From<std::borrow::Cow<'a, str>> for Cow<'a, str> {
}
}

impl<'a, T: Cowable> From<Cow<'a, T>> for std::borrow::Cow<'a, T> {
#[inline]
fn from(value: Cow<'a, T>) -> Self {
match value.metadata.kind() {
Kind::Owned | Kind::Shared => Self::Owned(value.into_owned()),
Kind::Borrowed => {
// SAFETY: We know the contained data is borrowed from 'a, we're simply
// restoring the original immutable reference and returning a copy of it.
Self::Borrowed(unsafe { &*T::borrowed_from_parts(value.ptr, &value.metadata) })
}
}
}
}

impl From<String> for Cow<'_, str> {
#[inline]
fn from(s: String) -> Self {
Expand Down

0 comments on commit 3fc0b4f

Please sign in to comment.