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

alloc: Add Borrow impls for &String, &mut String, &Vec, and &mut Vec #45808

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
alloc: Add Borrow impls for &String, &mut String, &Vec, and &mut Vec
This patch addresses a small papercut, where the auto-ref/deref
does not work well with generic types. One small example of this
is:

```rust
use std::borrow::Borrow;

fn foo<T: Borrow<[usize]>>(x: T) {
    println!("{:?}", x.borrow());
}

fn main() {
    let x = vec![1, 2, 3];
    foo(&x);
}
```

Without this patch, rust will error with:

```
error[E0277]: the trait bound `&std::vec::Vec<{integer}>: std::borrow::Borrow<[usize]>` is not satisfied
 --> foo.rs:9:5
  |
9 |     foo(&x);
  |     ^^^ the trait `std::borrow::Borrow<[usize]>` is not implemented for `&std::vec::Vec<{integer}>`
  |
  = help: the following implementations were found:
            <std::vec::Vec<T> as std::borrow::Borrow<[T]>>
  = note: required by `foo`

error: aborting due to previous error
```

This forces users to use `x.as_slice()` to get the code to compile.
This patch then implements the following to cut down on unnecessary
line noise:

* `Borrow<str>` for `&String`, `&mut String`
* `BorrowMut<str>` for `String` and `&mut String`
* `Borrow<[T]>` for `&Vec<T>` and `&mut Vec<T>`
* `BorrowMut<[T]>` for `&mut Vec<T>`
  • Loading branch information
erickt committed Nov 6, 2017

Verified

This commit was signed with the committer’s verified signature.
neo1973 Markus Härer
commit 830ed13802488cd0de1b46501a6c080dffdb08f3
21 changes: 21 additions & 0 deletions src/liballoc/slice.rs
Original file line number Diff line number Diff line change
@@ -1830,13 +1830,34 @@ impl<T> Borrow<[T]> for Vec<T> {
}
}

#[unstable(feature = "borrow_ref_vec", issue = "45808")]
impl<'a, T> Borrow<[T]> for &'a Vec<T> {
fn borrow(&self) -> &[T] {
&self[..]
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> BorrowMut<[T]> for Vec<T> {
fn borrow_mut(&mut self) -> &mut [T] {
&mut self[..]
}
}

#[unstable(feature = "borrow_ref_vec", issue = "45808")]
impl<'a, T> Borrow<[T]> for &'a mut Vec<T> {
fn borrow(&self) -> &[T] {
&self[..]
}
}

#[unstable(feature = "borrow_ref_vec", issue = "45808")]
impl<'a, T> BorrowMut<[T]> for &'a mut Vec<T> {
fn borrow_mut(&mut self) -> &mut [T] {
&mut self[..]
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Clone> ToOwned for [T] {
type Owned = Vec<T>;
34 changes: 33 additions & 1 deletion src/liballoc/str.rs
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ use core::iter::FusedIterator;
use std_unicode::str::{UnicodeStr, Utf16Encoder};

use vec_deque::VecDeque;
use borrow::{Borrow, ToOwned};
use borrow::{Borrow, BorrowMut, ToOwned};
use string::String;
use std_unicode;
use vec::Vec;
@@ -182,6 +182,38 @@ impl Borrow<str> for String {
}
}

#[unstable(feature = "borrow_mut_string", issue = "45808")]
impl<'a> BorrowMut<str> for String {
#[inline]
fn borrow_mut(&mut self) -> &mut str {
&mut self[..]
}
}

#[unstable(feature = "borrow_ref_string", issue = "45808")]
impl<'a> Borrow<str> for &'a String {
#[inline]
fn borrow(&self) -> &str {
&self[..]
}
}

#[unstable(feature = "borrow_ref_string", issue = "45808")]
impl<'a> Borrow<str> for &'a mut String {
#[inline]
fn borrow(&self) -> &str {
&self[..]
}
}

#[unstable(feature = "borrow_ref_string", issue = "45808")]
impl<'a> BorrowMut<str> for &'a mut String {
#[inline]
fn borrow_mut(&mut self) -> &mut str {
&mut self[..]
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl ToOwned for str {
type Owned = String;