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

Commits on Nov 6, 2017

  1. 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>`
    erickt committed Nov 6, 2017
    Configuration menu
    Copy the full SHA
    830ed13 View commit details
    Browse the repository at this point in the history