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

Introduce Option::<&T>::borrowed #1792

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions text/0000-option_borrowed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
- Feature Name: `option_borrowed`
- Start Date: 2016-11-14
- RFC PR: (leave this empty)
- Rust Issue: (leave this empty)

# Summary
[summary]: #summary

The standard library provides the `Option::<&T>::cloned` method for `T: Clone`
as the standard way to convert from `Option<&T>` to `Option<T>`. This RFC
proposes the addition of `Option::<&T>::borrowed` for `T: Borrow<U>` to convert
from `Option<&T>` to `Option<&U>`.

# Motivation
[motivation]: #motivation

How to convert from `Option<String>` to `Option<&str>` is sometimes asked on
Stack Overflow [1] [2]. This use case is also common in Servo, along other

Choose a reason for hiding this comment

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

nit: "along with other" rather than "along other."

cases that would also be covered by such a method.

[1] http://stackoverflow.com/q/31233938
[2] http://stackoverflow.com/q/34974732

# Detailed design
[design]: #detailed-design

This implementation will be added to the `core::option` module:

```rust
use core::borrow::Borrow;

impl<'a, T> Option<&'a T> {
fn borrowed<U>(self) -> Option<&'a U> where T: Borrow<U> {
self.map(T::borrow)
}
}
```

# Drawbacks
[drawbacks]: #drawbacks

None.

# Alternatives
[alternatives]: #alternatives

We could instead add a `dereferenced` method using the Deref trait, but that's
a mouthful and is less flexible than using `Borrow`.

# Unresolved questions
[unresolved]: #unresolved-questions

None.