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

Add as_borrowed function #20

Merged
merged 3 commits into from
Mar 21, 2020
Merged

Conversation

Licenser
Copy link
Contributor

The function will consume the Cow and return the borrowed data with the associated lifetime. It panics if the data wasn't borrowed.

closes #19

@CAD97
Copy link

CAD97 commented Mar 20, 2020

This operation would probably be best called unwrap_borrowed as currently written.

If given a choice, I'd expect:

    fn as_borrowed(&self) -> Option<&'a T> {
        match self {
            Cow::Owned(_) => None,
            Cow::Borrowed(borrowed) => Some(borrowed),
        }
    }
    
    fn as_owned(&self) -> Option<&T::Owned> {
        match self {
            Cow::Owned(owned) => Some(owned),
            Cow::Borrowed(_) => None,
        }
    }
    
    fn as_owned_mut(&mut self) -> Option<&mut T::Owned> {
        match self {
            Cow::Owned(owned) => Some(owned),
            Cow::Borrowed(_) => None,
        }
    }

@Licenser
Copy link
Contributor Author

that's a much better name! I'll update it to unwrap_borrowed :)

@maciejhirsz maciejhirsz merged commit f01601a into maciejhirsz:master Mar 21, 2020
/// Panics: If the Beef is owned.
#[inline]
pub fn unwrap_borrowed(self) -> &'a T {
let cow = ManuallyDrop::new(self);
Copy link
Owner

Choose a reason for hiding this comment

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

Added tests in #21, this looked like a leak to me, Miri agreed :).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry about that!

@maciejhirsz
Copy link
Owner

Thanks for the PR! Released as 0.4.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

a way to 'extract' the borrowed value
3 participants