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

RFC: Implement CowUtils on Cow<str> itself to enable chaining #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

adamreichold
Copy link

Chaining multiple calls to CowUtils methods will currently yield results tied to temporary lifetimes because intermediate Cow<'s, str> will be borrowed as &'a str via &'a Cow<'s, str> thereby loosing the original lifetime 's.

By implementing CowUtils for Cow<str> we can chain the source lifetime through the calls at the cost of returning an owned Cow::Owned(String) instead of a borrow &'a str if a previous operation modified the source value, i.e. the calls in a chain will either all borrow or stop to completey at some point.

Another cost is that to handle short-lived references to owned values, the trait itself must be extended to require a Pattern impl for any lifetime, not just the given source lifetime. I don't think this is usually an issue, but it does require a HRTB and is really only necessary for chaining.

So in the end, I am not completely sure whether the design changes proposed here are really worth it, hence the RFC marker.

Chaining multiple calls to `CowUtils` methods will currently yield results tied
to temporary lifetimes because intermediate `Cow<'s, str>` will be borrowed as
`&'a str` via `&'a Cow<'s, str>` thereby loosing the original lifetime `'s`.

By implementing `CowUtils` for `Cow<str>` we can chain the source lifetime
through the calls at the cost of returning an owned `Cow::Owned(String)` instead
of a borrow `&'a str` if a previous operation modified the source value, i.e.
the calls in a chain will either all borrow or stop to completey at some point.

Another cost is that to handle short-lived references to owned values, the trait
itself must be extended to require a `Pattern` impl for any lifetime, not just
the given source lifetime. I don't think this is usually an issue, but it does
require a HRTB and is really only necessary for chaining.
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.

1 participant