You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>`
0 commit comments