Skip to content

Commit

Permalink
remove as_borrowed
Browse files Browse the repository at this point in the history
  • Loading branch information
Dav1dde committed May 4, 2024
1 parent e30c4e7 commit 0950704
Showing 1 changed file with 8 additions and 34 deletions.
42 changes: 8 additions & 34 deletions metrics/src/cow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub struct Cow<'a, T: Cowable + ?Sized + 'a> {
_lifetime: PhantomData<&'a T>,
}

impl<'a, T> Cow<'a, T>
impl<T> Cow<'_, T>
where
T: Cowable + ?Sized,
{
Expand Down Expand Up @@ -178,35 +178,6 @@ where

T::owned_from_parts(cow.ptr, &cow.metadata)
}

/// Extracts the borrowed data.
///
/// Returns `None` if the data is either shared or owned.
///
/// # Examples
///
/// ```
/// use metrics::SharedString;
///
/// let s = SharedString::from_borrowed("foobar");
/// assert_eq!(s.as_borrowed(), Some("foobar"));
///
/// let s = SharedString::from_owned("foobar".to_owned());
/// assert_eq!(s.as_borrowed(), None);
///
/// let s = SharedString::from_shared("foobar".to_owned().into());
/// assert_eq!(s.as_borrowed(), None);
/// ```
pub fn as_borrowed(&self) -> Option<&'a T> {
match self.metadata.kind() {
Kind::Owned | Kind::Shared => None,
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.
Some(unsafe { &*T::borrowed_from_parts(self.ptr, &self.metadata) })
}
}
}
}

impl<'a, T> Cow<'a, T>
Expand Down Expand Up @@ -356,10 +327,13 @@ 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 {
if let Some(borrowed) = value.as_borrowed() {
Self::Borrowed(borrowed)
} else {
Self::Owned(value.into_owned())
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) })
}
}
}
}
Expand Down

0 comments on commit 0950704

Please sign in to comment.