Skip to content

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

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions