Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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