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

change core::borrow::BorrowFrom to core::borrow::Borrow #530

Closed
mandel59 opened this issue Dec 17, 2014 · 8 comments
Closed

change core::borrow::BorrowFrom to core::borrow::Borrow #530

mandel59 opened this issue Dec 17, 2014 · 8 comments

Comments

@mandel59
Copy link

core::borrow::BorrowFrom is now defined as:

pub trait BorrowFrom<Sized? Owned> for Sized? {
    fn borrow_from(owned: &Owned) -> &Self;
}

The proposal core::borrow::Borrow simply swaps the type parameters Owned and Self. A: Borrow<T> means it is able to borrow a value typed as T from a value typed as A.

pub trait Borrow<Sized? T> for Sized? {
    fn borrow(&self) -> &T;
}

This changes BorrowFrom::borrow_from(&x) to x.borrow().

@mandel59
Copy link
Author

I've found that some methods in the standard library are already named borrow. It might be able to cover them. struct RefCell<T> has fn borrow<'a>(&'a self) -> Ref<'a, T> and trait Local<Borrowed> has fn borrow(unused_value: Option<Self>) -> Borrowed.

@mandel59
Copy link
Author

Here is a more generic definition:

pub trait Borrow<'a, Sized? T> for Sized? {
    fn borrow(&'a self) -> T;
}

Tweaking the definition of core::str::Str enable to implement as_slice for Borrow<'a, &'a str>.

pub trait Str<'a> for Sized? {
    fn as_slice(&'a self) -> &'a str;
}

pub impl<'a, T> Str<'a> for T where
    T: Borrow<'a, &'a str>
{
    fn as_slice(&'a self) -> &'a str {
        self.borrow()
    }
}

@Gankra
Copy link
Contributor

Gankra commented Jan 6, 2015

CC @aturon

@aturon
Copy link
Member

aturon commented Jan 6, 2015

I was about to prep a PR to do exactly this!

@aturon
Copy link
Member

aturon commented Jan 6, 2015

(Note that the original RFC had it as Borrow as well, but that required generalized where clauses which weren't available when we first rolled it out.)

@aturon
Copy link
Member

aturon commented Jan 7, 2015

Currently blocked on rust-lang/rust#20671

@SSheldon
Copy link

SSheldon commented Mar 5, 2015

Was this closed by rust-lang/rust#22210? @aturon

@aturon
Copy link
Member

aturon commented Mar 5, 2015

It was! Thanks.

@aturon aturon closed this as completed Mar 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants