-
Notifications
You must be signed in to change notification settings - Fork 13k
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
libs: generalize as_ref
/by_ref
adapters to use BorrowFrom
#19188
Comments
cc @japaric, you may be interested in this as well. |
Does this apply to the |
This is now possible, though it requires the #![feature(default_type_parameter_fallback)]
use Option::*;
use std::borrow::Borrow;
pub enum Option<T> {
None,
Some(T),
}
impl<T> Option<T> {
pub fn as_ref<U: ?Sized = T>(&self) -> Option<&U> where T: Borrow<U> {
match *self {
None => None,
Some(ref t) => Some(t.borrow()),
}
}
}
#[test]
fn test() {
let opt = Some("foo".to_string());
let _r1 = opt.as_ref(); // falls back to `Option<&String>`
let _r2 = opt.as_ref::<str>();
} |
On the topic of
|
Needs RFC (language change) |
It is a pain to go from
Option<String>
toOption<&str>
:This could be improved by integrating the new
BorrowFrom
infrastructure, generalizing the signature ofas_ref
fromto
However, to avoid a regression in type inference for types that can be borrowed in multiple ways, we need default type parameters on methods (as above). See rust-lang/rfcs#213
The text was updated successfully, but these errors were encountered: