Skip to content

Commit

Permalink
Rollup merge of rust-lang#92382 - clarfonthey:const_convert, r=scottmcm
Browse files Browse the repository at this point in the history
Extend const_convert to rest of blanket core::convert impls

This adds constness to all the blanket impls in `core::convert` under the existing `const_convert` feature, tracked by rust-lang#88674.

Existing impls under that feature:

```rust
impl<T> const From<T> for T;
impl<T, U> const Into<U> for T where U: ~const From<T>;

impl<T> const ops::Try for Option<T>;
impl<T> const ops::FromResidual for Option<T>;

impl<T, E> const ops::Try for Result<T, E>;
impl<T, E, F> const ops::FromResidual<Result<convert::Infallible, E>> for Result<T, F> where F: ~const From<E>;
```

Additional impls:

```rust
impl<T: ?Sized, U: ?Sized> const AsRef<U> for &T where T: ~const AsRef<U>;
impl<T: ?Sized, U: ?Sized> const AsRef<U> for &mut T where T: ~const AsRef<U>;
impl<T: ?Sized, U: ?Sized> const AsMut<U> for &mut T where T: ~const AsMut<U>;

impl<T, U> const TryInto<U> for T where U: ~const TryFrom<T>;
impl<T, U> const TryFrom<U> for T where U: ~const Into<T>;
```
  • Loading branch information
matthiaskrgr authored Jan 15, 2022
2 parents 6471682 + b1b873f commit 1d9ab77
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions library/core/src/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,10 @@ pub trait TryFrom<T>: Sized {

// As lifts over &
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized, U: ?Sized> AsRef<U> for &T
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
impl<T: ?Sized, U: ?Sized> const AsRef<U> for &T
where
T: AsRef<U>,
T: ~const AsRef<U>,
{
fn as_ref(&self) -> &U {
<T as AsRef<U>>::as_ref(*self)
Expand All @@ -492,9 +493,10 @@ where

// As lifts over &mut
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized, U: ?Sized> AsRef<U> for &mut T
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
impl<T: ?Sized, U: ?Sized> const AsRef<U> for &mut T
where
T: AsRef<U>,
T: ~const AsRef<U>,
{
fn as_ref(&self) -> &U {
<T as AsRef<U>>::as_ref(*self)
Expand All @@ -511,9 +513,10 @@ where

// AsMut lifts over &mut
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized, U: ?Sized> AsMut<U> for &mut T
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
impl<T: ?Sized, U: ?Sized> const AsMut<U> for &mut T
where
T: AsMut<U>,
T: ~const AsMut<U>,
{
fn as_mut(&mut self) -> &mut U {
(*self).as_mut()
Expand Down Expand Up @@ -567,9 +570,10 @@ impl<T> const From<!> for T {

// TryFrom implies TryInto
#[stable(feature = "try_from", since = "1.34.0")]
impl<T, U> TryInto<U> for T
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
impl<T, U> const TryInto<U> for T
where
U: TryFrom<T>,
U: ~const TryFrom<T>,
{
type Error = U::Error;

Expand All @@ -581,9 +585,10 @@ where
// Infallible conversions are semantically equivalent to fallible conversions
// with an uninhabited error type.
#[stable(feature = "try_from", since = "1.34.0")]
impl<T, U> TryFrom<U> for T
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
impl<T, U> const TryFrom<U> for T
where
U: Into<T>,
U: ~const Into<T>,
{
type Error = Infallible;

Expand Down

0 comments on commit 1d9ab77

Please sign in to comment.