maciejhirsz / beef

Faster, more compact implementation of std::borrow::Cow
https://crates.io/crates/beef
Apache License 2.0
338 stars 17 forks source link

Add as_borrowed function #20

Closed Licenser closed 4 years ago

Licenser commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

maciejhirsz commented 4 years ago

Thanks for the PR! Released as 0.4.1.