Skip to content

Commit 830ed13

Browse files
committed
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>`
1 parent 74be072 commit 830ed13

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/liballoc/slice.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1830,13 +1830,34 @@ impl<T> Borrow<[T]> for Vec<T> {
18301830
}
18311831
}
18321832

1833+
#[unstable(feature = "borrow_ref_vec", issue = "45808")]
1834+
impl<'a, T> Borrow<[T]> for &'a Vec<T> {
1835+
fn borrow(&self) -> &[T] {
1836+
&self[..]
1837+
}
1838+
}
1839+
18331840
#[stable(feature = "rust1", since = "1.0.0")]
18341841
impl<T> BorrowMut<[T]> for Vec<T> {
18351842
fn borrow_mut(&mut self) -> &mut [T] {
18361843
&mut self[..]
18371844
}
18381845
}
18391846

1847+
#[unstable(feature = "borrow_ref_vec", issue = "45808")]
1848+
impl<'a, T> Borrow<[T]> for &'a mut Vec<T> {
1849+
fn borrow(&self) -> &[T] {
1850+
&self[..]
1851+
}
1852+
}
1853+
1854+
#[unstable(feature = "borrow_ref_vec", issue = "45808")]
1855+
impl<'a, T> BorrowMut<[T]> for &'a mut Vec<T> {
1856+
fn borrow_mut(&mut self) -> &mut [T] {
1857+
&mut self[..]
1858+
}
1859+
}
1860+
18401861
#[stable(feature = "rust1", since = "1.0.0")]
18411862
impl<T: Clone> ToOwned for [T] {
18421863
type Owned = Vec<T>;

src/liballoc/str.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use core::iter::FusedIterator;
4747
use std_unicode::str::{UnicodeStr, Utf16Encoder};
4848

4949
use vec_deque::VecDeque;
50-
use borrow::{Borrow, ToOwned};
50+
use borrow::{Borrow, BorrowMut, ToOwned};
5151
use string::String;
5252
use std_unicode;
5353
use vec::Vec;
@@ -182,6 +182,38 @@ impl Borrow<str> for String {
182182
}
183183
}
184184

185+
#[unstable(feature = "borrow_mut_string", issue = "45808")]
186+
impl<'a> BorrowMut<str> for String {
187+
#[inline]
188+
fn borrow_mut(&mut self) -> &mut str {
189+
&mut self[..]
190+
}
191+
}
192+
193+
#[unstable(feature = "borrow_ref_string", issue = "45808")]
194+
impl<'a> Borrow<str> for &'a String {
195+
#[inline]
196+
fn borrow(&self) -> &str {
197+
&self[..]
198+
}
199+
}
200+
201+
#[unstable(feature = "borrow_ref_string", issue = "45808")]
202+
impl<'a> Borrow<str> for &'a mut String {
203+
#[inline]
204+
fn borrow(&self) -> &str {
205+
&self[..]
206+
}
207+
}
208+
209+
#[unstable(feature = "borrow_ref_string", issue = "45808")]
210+
impl<'a> BorrowMut<str> for &'a mut String {
211+
#[inline]
212+
fn borrow_mut(&mut self) -> &mut str {
213+
&mut self[..]
214+
}
215+
}
216+
185217
#[stable(feature = "rust1", since = "1.0.0")]
186218
impl ToOwned for str {
187219
type Owned = String;

0 commit comments

Comments
 (0)