Skip to content

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

Closed
@aturon

Description

@aturon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions