about summary refs log tree commit diff
path: root/library/core/src/convert
diff options
context:
space:
mode:
authorEvgenii Zheltonozhskii <zheltonozhskiy@gmail.com>2025-07-21 12:49:45 +0300
committerEvgenii Zheltonozhskii <zheltonozhskiy@gmail.com>2025-07-21 12:49:45 +0300
commit3c76e3d0026ea9904358b1614bbd9c4f8b7cedf5 (patch)
tree5d3aeecf65661c610495cc977f2af80cbd9317c7 /library/core/src/convert
parent12865ffd0dfb4ea969e2f16eb0140238bb9dd382 (diff)
Constify Try, From, TryFrom
Diffstat (limited to 'library/core/src/convert')
-rw-r--r--library/core/src/convert/mod.rs40
-rw-r--r--library/core/src/convert/num.rs24
2 files changed, 43 insertions, 21 deletions
diff --git a/library/core/src/convert/mod.rs b/library/core/src/convert/mod.rs
index 38381dbdf23..220a24caf09 100644
--- a/library/core/src/convert/mod.rs
+++ b/library/core/src/convert/mod.rs
@@ -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_try", issue = "74935")]
 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")]
@@ -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_try", issue = "74935")]
 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")]
@@ -710,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_try", issue = "74935")]
+impl<T: PointeeSized, U: PointeeSized> const AsRef<U> for &T
 where
-    T: AsRef<U>,
+    T: ~const AsRef<U>,
 {
     #[inline]
     fn as_ref(&self) -> &U {
@@ -722,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_try", issue = "74935")]
+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 {
@@ -742,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_try", issue = "74935")]
+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 {
@@ -840,7 +847,8 @@ where
 ////////////////////////////////////////////////////////////////////////////////
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<T> AsRef<[T]> for [T] {
+#[rustc_const_unstable(feature = "const_try", issue = "74935")]
+impl<T> const AsRef<[T]> for [T] {
     #[inline(always)]
     fn as_ref(&self) -> &[T] {
         self
@@ -848,7 +856,8 @@ impl<T> AsRef<[T]> for [T] {
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<T> AsMut<[T]> for [T] {
+#[rustc_const_unstable(feature = "const_try", issue = "74935")]
+impl<T> const AsMut<[T]> for [T] {
     #[inline(always)]
     fn as_mut(&mut self) -> &mut [T] {
         self
@@ -856,7 +865,8 @@ impl<T> AsMut<[T]> for [T] {
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl AsRef<str> for str {
+#[rustc_const_unstable(feature = "const_try", issue = "74935")]
+impl const AsRef<str> for str {
     #[inline(always)]
     fn as_ref(&self) -> &str {
         self
@@ -864,7 +874,8 @@ impl AsRef<str> for str {
 }
 
 #[stable(feature = "as_mut_str_for_str", since = "1.51.0")]
-impl AsMut<str> for str {
+#[rustc_const_unstable(feature = "const_try", issue = "74935")]
+impl const AsMut<str> for str {
     #[inline(always)]
     fn as_mut(&mut self) -> &mut str {
         self
@@ -925,7 +936,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_try", issue = "74935")]
+impl const Clone for Infallible {
     fn clone(&self) -> Infallible {
         match *self {}
     }
@@ -953,7 +965,8 @@ impl Error for Infallible {
 }
 
 #[stable(feature = "convert_infallible", since = "1.34.0")]
-impl PartialEq for Infallible {
+#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
+impl const PartialEq for Infallible {
     fn eq(&self, _: &Infallible) -> bool {
         match *self {}
     }
@@ -977,7 +990,8 @@ impl Ord for Infallible {
 }
 
 #[stable(feature = "convert_infallible", since = "1.34.0")]
-impl From<!> for Infallible {
+#[rustc_const_unstable(feature = "const_try", issue = "74935")]
+impl const From<!> for Infallible {
     #[inline]
     fn from(x: !) -> Self {
         x
diff --git a/library/core/src/convert/num.rs b/library/core/src/convert/num.rs
index 50616732b77..affb4eb64d3 100644
--- a/library/core/src/convert/num.rs
+++ b/library/core/src/convert/num.rs
@@ -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_try", issue = "74935")]
+        impl const From<$Small> for $Large {
             // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
             // Rustdocs on functions do not.
             #[doc = $doc]
@@ -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_try", issue = "74935")]
+            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.
             ///
@@ -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_try", issue = "74935")]
+        impl const TryFrom<$source> for $target {
             type Error = TryFromIntError;
 
             /// Tries to create the target number type from a source
@@ -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_try", issue = "74935")]
+        impl const TryFrom<$source> for $target {
             type Error = TryFromIntError;
 
             /// Tries to create the target number type from a source
@@ -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_try", issue = "74935")]
+        impl const TryFrom<$source> for $target {
             type Error = TryFromIntError;
 
             /// Tries to create the target number type from a source
@@ -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_try", issue = "74935")]
+        impl const TryFrom<$source> for $target {
             type Error = TryFromIntError;
 
             /// Tries to create the target number type from a source
@@ -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_try", issue = "74935")]
+        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> ")]
@@ -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_try", issue = "74935")]
+        impl const TryFrom<NonZero<$source>> for NonZero<$target> {
             type Error = TryFromIntError;
 
             // Rustdocs on the impl block show a "[+] show undocumented items" toggle.