Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement conversion from cow to std cow #478

Merged
merged 2 commits into from
May 5, 2024
Merged

Conversation

Dav1dde
Copy link
Contributor

@Dav1dde Dav1dde commented May 1, 2024

Closes: #474

@Dav1dde
Copy link
Contributor Author

Dav1dde commented May 1, 2024

Did some experiments with miri and it seems to be happy.

Copy link
Member

@tobz tobz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic is sound, but I think we just need to clean up the ergonomics a little.

Comment on lines 182 to 209
/// 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) })
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless there's a compelling reason to have this be a standalone method, we should just move the logic down into impl From<Cow<'a, T>> for std::borrow::Cow<'a, T>.

Copy link
Contributor Author

@Dav1dde Dav1dde May 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with that is, the From impl always requires a Clone (or taking ownership, but the way the SharedString is exposed it is usually through a reference), the as_borrowed() allows for a decision. In the future this can be supplemented with as_shared().

That being said, we can reduce the API surface and just impl the From (would be enough for my usecase) and see if more needs come up.

@Dav1dde
Copy link
Contributor Author

Dav1dde commented May 4, 2024

Removed the as_borrowed and only left the cow impl.

@Dav1dde Dav1dde changed the title Extract borrow from cow, implement conversion to std cow Implement conversion from cow to std cow May 4, 2024
@Dav1dde Dav1dde requested a review from tobz May 4, 2024 07:56
Copy link
Member

@tobz tobz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. 👍🏻

@tobz tobz merged commit 3fc0b4f into metrics-rs:main May 5, 2024
12 checks passed
@tobz tobz added C-core Component: core functionality such as traits, etc. E-intermediate Effort: intermediate. S-awaiting-release Status: awaiting a release to be considered fixed/implemented. T-ergonomics Type: ergonomics. labels May 5, 2024
@tobz
Copy link
Member

tobz commented May 27, 2024

Released in metrics@v0.23.0.

Thanks again for your contribution. 🙏🏻

@tobz tobz removed the S-awaiting-release Status: awaiting a release to be considered fixed/implemented. label May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-core Component: core functionality such as traits, etc. E-intermediate Effort: intermediate. T-ergonomics Type: ergonomics.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make it possible to extract &'static T from Cow<'static, T>.
2 participants