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
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions src/liballoc/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down
34 changes: 33 additions & 1 deletion src/liballoc/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down