Skip to content

Commit 3fc0b4f

Browse files
authored
Implement conversion from cow to std cow (#478)
1 parent cd7de8c commit 3fc0b4f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

metrics/src/cow.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,20 @@ impl<'a> From<std::borrow::Cow<'a, str>> for Cow<'a, str> {
324324
}
325325
}
326326

327+
impl<'a, T: Cowable> From<Cow<'a, T>> for std::borrow::Cow<'a, T> {
328+
#[inline]
329+
fn from(value: Cow<'a, T>) -> Self {
330+
match value.metadata.kind() {
331+
Kind::Owned | Kind::Shared => Self::Owned(value.into_owned()),
332+
Kind::Borrowed => {
333+
// SAFETY: We know the contained data is borrowed from 'a, we're simply
334+
// restoring the original immutable reference and returning a copy of it.
335+
Self::Borrowed(unsafe { &*T::borrowed_from_parts(value.ptr, &value.metadata) })
336+
}
337+
}
338+
}
339+
}
340+
327341
impl From<String> for Cow<'_, str> {
328342
#[inline]
329343
fn from(s: String) -> Self {

0 commit comments

Comments
 (0)