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

libs: generalize as_ref/by_ref adapters to use BorrowFrom #19188

Closed
aturon opened this issue Nov 21, 2014 · 6 comments
Closed

libs: generalize as_ref/by_ref adapters to use BorrowFrom #19188

aturon opened this issue Nov 21, 2014 · 6 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@aturon
Copy link
Member

aturon commented Nov 21, 2014

It is a pain to go from Option<String> to Option<&str>:

opt.as_ref().map(|s| s.as_slice())

This could be improved by integrating the new BorrowFrom infrastructure, generalizing the signature of as_ref from

fn as_ref(&self) -> Option<&T>

to

fn as_ref<U=T>(&self) -> Option<&U> where U: BorrowFrom<T>

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

@aturon
Copy link
Member Author

aturon commented Nov 21, 2014

cc @alexcrichton @nikomatsakis

@aturon
Copy link
Member Author

aturon commented Nov 21, 2014

cc @japaric, you may be interested in this as well.

@ftxqxd
Copy link
Contributor

ftxqxd commented Nov 28, 2014

Does this apply to the by_ref adapters on Iterator, Reader, and Writer, as the title suggests? I don’t really see how those methods are related.

@apasel422
Copy link
Contributor

This is now possible, though it requires the default_type_parameter_fallback feature gate:

#![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>();
}

@aidanhs
Copy link
Member

aidanhs commented Jul 26, 2016

On the topic of Option<String> to Option<&str>, one alternative possibility is given in http://stackoverflow.com/questions/31233938/converting-from-optionstring-to-optionstr

use std::ops::Deref;

trait OptionDeref<T: Deref> {
    fn as_deref(&self) -> Option<&T::Target>;
}

impl<T: Deref> OptionDeref<T> for Option<T> {
    fn as_deref(&self) -> Option<&T::Target> {
        self.as_ref().map(Deref::deref)
    }
}

@steveklabnik steveklabnik added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed A-libs labels Mar 24, 2017
@Mark-Simulacrum Mark-Simulacrum added C-feature-request Category: A feature request, i.e: not implemented / a PR. and removed I-papercut labels Jul 22, 2017
@aturon aturon closed this as completed Aug 1, 2017
@aturon
Copy link
Member Author

aturon commented Aug 1, 2017

Needs RFC (language change)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants