Skip to content

Constify Try, From, TryFrom and relevant traits #143768

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ impl Error for TryFromSliceError {
}

#[stable(feature = "try_from_slice_error", since = "1.36.0")]
impl From<Infallible> for TryFromSliceError {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const From<Infallible> for TryFromSliceError {
fn from(x: Infallible) -> TryFromSliceError {
match x {}
}
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/ascii/ascii_char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,8 @@ macro_rules! into_int_impl {
($($ty:ty)*) => {
$(
#[unstable(feature = "ascii_char", issue = "110998")]
impl From<AsciiChar> for $ty {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const From<AsciiChar> for $ty {
#[inline]
fn from(chr: AsciiChar) -> $ty {
chr as u8 as $ty
Expand Down
15 changes: 10 additions & 5 deletions library/core/src/char/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ pub(super) const unsafe fn from_u32_unchecked(i: u32) -> char {
}

#[stable(feature = "char_convert", since = "1.13.0")]
impl From<char> for u32 {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const From<char> for u32 {
/// Converts a [`char`] into a [`u32`].
///
/// # Examples
Expand All @@ -53,7 +54,8 @@ impl From<char> for u32 {
}

#[stable(feature = "more_char_conversions", since = "1.51.0")]
impl From<char> for u64 {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const From<char> for u64 {
/// Converts a [`char`] into a [`u64`].
///
/// # Examples
Expand All @@ -72,7 +74,8 @@ impl From<char> for u64 {
}

#[stable(feature = "more_char_conversions", since = "1.51.0")]
impl From<char> for u128 {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const From<char> for u128 {
/// Converts a [`char`] into a [`u128`].
///
/// # Examples
Expand Down Expand Up @@ -157,7 +160,8 @@ impl TryFrom<char> for u16 {
/// for a superset of Windows-1252 that fills the remaining blanks with corresponding
/// C0 and C1 control codes.
#[stable(feature = "char_convert", since = "1.13.0")]
impl From<u8> for char {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const From<u8> for char {
/// Converts a [`u8`] into a [`char`].
///
/// # Examples
Expand Down Expand Up @@ -247,7 +251,8 @@ const fn char_try_from_u32(i: u32) -> Result<char, CharTryFromError> {
}

#[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<u32> for char {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const TryFrom<u32> for char {
type Error = CharTryFromError;

#[inline]
Expand Down
66 changes: 46 additions & 20 deletions library/core/src/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ pub const fn identity<T>(x: T) -> T {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "AsRef"]
#[const_trait]
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
pub trait AsRef<T: PointeeSized>: PointeeSized {
/// Converts this type into a shared reference of the (usually inferred) input type.
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -367,6 +369,8 @@ pub trait AsRef<T: PointeeSized>: PointeeSized {
/// `&mut Vec<u8>`, for example, is the better choice (callers need to pass the correct type then).
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "AsMut"]
#[const_trait]
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
pub trait AsMut<T: PointeeSized>: PointeeSized {
/// Converts this type into a mutable reference of the (usually inferred) input type.
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -445,6 +449,8 @@ pub trait AsMut<T: PointeeSized>: PointeeSized {
#[rustc_diagnostic_item = "Into"]
#[stable(feature = "rust1", since = "1.0.0")]
#[doc(search_unbox)]
#[const_trait]
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
pub trait Into<T>: Sized {
/// Converts this type into the (usually inferred) input type.
#[must_use]
Expand Down Expand Up @@ -580,6 +586,8 @@ pub trait Into<T>: Sized {
note = "to coerce a `{T}` into a `{Self}`, use `&*` as a prefix",
))]
#[doc(search_unbox)]
#[const_trait]
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
pub trait From<T>: Sized {
/// Converts to this type from the input type.
#[rustc_diagnostic_item = "from_fn"]
Expand Down Expand Up @@ -607,6 +615,8 @@ pub trait From<T>: Sized {
/// [`Into`], see there for details.
#[rustc_diagnostic_item = "TryInto"]
#[stable(feature = "try_from", since = "1.34.0")]
#[const_trait]
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
pub trait TryInto<T>: Sized {
/// The type returned in the event of a conversion error.
#[stable(feature = "try_from", since = "1.34.0")]
Expand Down Expand Up @@ -685,6 +695,8 @@ pub trait TryInto<T>: Sized {
/// [`try_from`]: TryFrom::try_from
#[rustc_diagnostic_item = "TryFrom"]
#[stable(feature = "try_from", since = "1.34.0")]
#[const_trait]
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
pub trait TryFrom<T>: Sized {
/// The type returned in the event of a conversion error.
#[stable(feature = "try_from", since = "1.34.0")]
Expand All @@ -702,9 +714,10 @@ pub trait TryFrom<T>: Sized {

// As lifts over &
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: PointeeSized, U: PointeeSized> AsRef<U> for &T
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl<T: PointeeSized, U: PointeeSized> const AsRef<U> for &T
where
T: AsRef<U>,
T: ~const AsRef<U>,
{
#[inline]
fn as_ref(&self) -> &U {
Expand All @@ -714,9 +727,10 @@ where

// As lifts over &mut
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: PointeeSized, U: PointeeSized> AsRef<U> for &mut T
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl<T: PointeeSized, U: PointeeSized> const AsRef<U> for &mut T
where
T: AsRef<U>,
T: ~const AsRef<U>,
{
#[inline]
fn as_ref(&self) -> &U {
Expand All @@ -734,9 +748,10 @@ where

// AsMut lifts over &mut
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: PointeeSized, U: PointeeSized> AsMut<U> for &mut T
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl<T: PointeeSized, U: PointeeSized> const AsMut<U> for &mut T
where
T: AsMut<U>,
T: ~const AsMut<U>,
{
#[inline]
fn as_mut(&mut self) -> &mut U {
Expand All @@ -754,9 +769,10 @@ where

// From implies Into
#[stable(feature = "rust1", since = "1.0.0")]
impl<T, U> Into<U> for T
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl<T, U> const Into<U> for T
where
U: From<T>,
U: ~const From<T>,
{
/// Calls `U::from(self)`.
///
Expand All @@ -771,7 +787,8 @@ where

// From (and thus Into) is reflexive
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> From<T> for T {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl<T> const From<T> for T {
/// Returns the argument unchanged.
#[inline(always)]
fn from(t: T) -> T {
Expand All @@ -795,9 +812,10 @@ impl<T> 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_trait_impl", issue = "67792")]
impl<T, U> const TryInto<U> for T
where
U: TryFrom<T>,
U: ~const TryFrom<T>,
{
type Error = U::Error;

Expand All @@ -810,9 +828,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_trait_impl", issue = "67792")]
impl<T, U> const TryFrom<U> for T
where
U: Into<T>,
U: ~const Into<T>,
{
type Error = Infallible;

Expand All @@ -827,31 +846,35 @@ where
////////////////////////////////////////////////////////////////////////////////

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> AsRef<[T]> for [T] {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl<T> const AsRef<[T]> for [T] {
#[inline(always)]
fn as_ref(&self) -> &[T] {
self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> AsMut<[T]> for [T] {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl<T> const AsMut<[T]> for [T] {
#[inline(always)]
fn as_mut(&mut self) -> &mut [T] {
self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<str> for str {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const AsRef<str> for str {
#[inline(always)]
fn as_ref(&self) -> &str {
self
}
}

#[stable(feature = "as_mut_str_for_str", since = "1.51.0")]
impl AsMut<str> for str {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const AsMut<str> for str {
#[inline(always)]
fn as_mut(&mut self) -> &mut str {
self
Expand Down Expand Up @@ -912,7 +935,8 @@ impl AsMut<str> for str {
pub enum Infallible {}

#[stable(feature = "convert_infallible", since = "1.34.0")]
impl Clone for Infallible {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const Clone for Infallible {
fn clone(&self) -> Infallible {
match *self {}
}
Expand Down Expand Up @@ -940,7 +964,8 @@ impl Error for Infallible {
}

#[stable(feature = "convert_infallible", since = "1.34.0")]
impl PartialEq for Infallible {
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
impl const PartialEq for Infallible {
fn eq(&self, _: &Infallible) -> bool {
match *self {}
}
Expand All @@ -964,7 +989,8 @@ impl Ord for Infallible {
}

#[stable(feature = "convert_infallible", since = "1.34.0")]
impl From<!> for Infallible {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const From<!> for Infallible {
#[inline]
fn from(x: !) -> Self {
x
Expand Down
24 changes: 16 additions & 8 deletions library/core/src/convert/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ macro_rules! impl_from {
};
($Small:ty => $Large:ty, #[$attr:meta], $doc:expr $(,)?) => {
#[$attr]
impl From<$Small> for $Large {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const From<$Small> for $Large {
// Rustdocs on the impl block show a "[+] show undocumented items" toggle.
// Rustdocs on functions do not.
#[doc = $doc]
Expand Down Expand Up @@ -200,7 +201,8 @@ macro_rules! impl_float_from_bool {
)?
) => {
#[stable(feature = "float_from_bool", since = "1.68.0")]
impl From<bool> for $float {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const From<bool> for $float {
#[doc = concat!("Converts a [`bool`] to [`", stringify!($float),"`] losslessly.")]
/// The resulting value is positive `0.0` for `false` and `1.0` for `true` values.
///
Expand Down Expand Up @@ -250,7 +252,8 @@ impl_float_from_bool!(
macro_rules! impl_try_from_unbounded {
($source:ty => $($target:ty),+) => {$(
#[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<$source> for $target {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const TryFrom<$source> for $target {
type Error = TryFromIntError;

/// Tries to create the target number type from a source
Expand All @@ -268,7 +271,8 @@ macro_rules! impl_try_from_unbounded {
macro_rules! impl_try_from_lower_bounded {
($source:ty => $($target:ty),+) => {$(
#[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<$source> for $target {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const TryFrom<$source> for $target {
type Error = TryFromIntError;

/// Tries to create the target number type from a source
Expand All @@ -290,7 +294,8 @@ macro_rules! impl_try_from_lower_bounded {
macro_rules! impl_try_from_upper_bounded {
($source:ty => $($target:ty),+) => {$(
#[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<$source> for $target {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const TryFrom<$source> for $target {
type Error = TryFromIntError;

/// Tries to create the target number type from a source
Expand All @@ -312,7 +317,8 @@ macro_rules! impl_try_from_upper_bounded {
macro_rules! impl_try_from_both_bounded {
($source:ty => $($target:ty),+) => {$(
#[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<$source> for $target {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const TryFrom<$source> for $target {
type Error = TryFromIntError;

/// Tries to create the target number type from a source
Expand Down Expand Up @@ -450,7 +456,8 @@ use crate::num::NonZero;
macro_rules! impl_nonzero_int_from_nonzero_int {
($Small:ty => $Large:ty) => {
#[stable(feature = "nz_int_conv", since = "1.41.0")]
impl From<NonZero<$Small>> for NonZero<$Large> {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const From<NonZero<$Small>> for NonZero<$Large> {
// Rustdocs on the impl block show a "[+] show undocumented items" toggle.
// Rustdocs on functions do not.
#[doc = concat!("Converts <code>[NonZero]\\<[", stringify!($Small), "]></code> ")]
Expand Down Expand Up @@ -540,7 +547,8 @@ impl_nonzero_int_try_from_int!(isize);
macro_rules! impl_nonzero_int_try_from_nonzero_int {
($source:ty => $($target:ty),+) => {$(
#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
impl TryFrom<NonZero<$source>> for NonZero<$target> {
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
impl const TryFrom<NonZero<$source>> for NonZero<$target> {
type Error = TryFromIntError;

// Rustdocs on the impl block show a "[+] show undocumented items" toggle.
Expand Down
Loading
Loading