Skip to content

Commit

Permalink
Rollup merge of rust-lang#90270 - woppopo:const_borrow_trait, r=dtolnay
Browse files Browse the repository at this point in the history
Make `Borrow` and `BorrowMut` impls `const`

Tracking issue: rust-lang#91522
  • Loading branch information
matthiaskrgr committed Dec 11, 2021
2 parents d9df1a8 + 8f68bdc commit ae9dafc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
6 changes: 4 additions & 2 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,16 @@ impl<T, const N: usize> AsMut<[T]> for [T; N] {
}

#[stable(feature = "array_borrow", since = "1.4.0")]
impl<T, const N: usize> Borrow<[T]> for [T; N] {
#[rustc_const_unstable(feature = "const_borrow", issue = "91522")]
impl<T, const N: usize> const Borrow<[T]> for [T; N] {
fn borrow(&self) -> &[T] {
self
}
}

#[stable(feature = "array_borrow", since = "1.4.0")]
impl<T, const N: usize> BorrowMut<[T]> for [T; N] {
#[rustc_const_unstable(feature = "const_borrow", issue = "91522")]
impl<T, const N: usize> const BorrowMut<[T]> for [T; N] {
fn borrow_mut(&mut self) -> &mut [T] {
self
}
Expand Down
15 changes: 10 additions & 5 deletions library/core/src/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,36 +205,41 @@ pub trait BorrowMut<Borrowed: ?Sized>: Borrow<Borrowed> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> Borrow<T> for T {
#[rustc_const_unstable(feature = "const_borrow", issue = "91522")]
impl<T: ?Sized> const Borrow<T> for T {
#[rustc_diagnostic_item = "noop_method_borrow"]
fn borrow(&self) -> &T {
self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> BorrowMut<T> for T {
#[rustc_const_unstable(feature = "const_borrow", issue = "91522")]
impl<T: ?Sized> const BorrowMut<T> for T {
fn borrow_mut(&mut self) -> &mut T {
self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> Borrow<T> for &T {
#[rustc_const_unstable(feature = "const_borrow", issue = "91522")]
impl<T: ?Sized> const Borrow<T> for &T {
fn borrow(&self) -> &T {
&**self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> Borrow<T> for &mut T {
#[rustc_const_unstable(feature = "const_borrow", issue = "91522")]
impl<T: ?Sized> const Borrow<T> for &mut T {
fn borrow(&self) -> &T {
&**self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> BorrowMut<T> for &mut T {
#[rustc_const_unstable(feature = "const_borrow", issue = "91522")]
impl<T: ?Sized> const BorrowMut<T> for &mut T {
fn borrow_mut(&mut self) -> &mut T {
&mut **self
}
Expand Down

0 comments on commit ae9dafc

Please sign in to comment.