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

Merged
merged 1 commit into from
Jul 21, 2025
Merged
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_try", issue = "74935")]
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_try", issue = "74935")]
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_try", issue = "74935")]
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_try", issue = "74935")]
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_try", issue = "74935")]
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_try", issue = "74935")]
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_try", issue = "74935")]
impl const TryFrom<u32> for char {
type Error = CharTryFromError;

#[inline]
Expand Down
40 changes: 27 additions & 13 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_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")]
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_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")]
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -840,31 +847,35 @@ 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
}
}

#[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
}
}

#[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
}
}

#[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
Expand Down Expand Up @@ -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 {}
}
Expand Down Expand Up @@ -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 {}
}
Expand All @@ -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
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_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]
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_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.
///
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_try", issue = "74935")]
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_try", issue = "74935")]
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_try", issue = "74935")]
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_try", issue = "74935")]
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_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> ")]
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_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.
Expand Down
36 changes: 24 additions & 12 deletions library/core/src/net/ip_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,8 @@ impl fmt::Debug for IpAddr {
}

#[stable(feature = "ip_from_ip", since = "1.16.0")]
impl From<Ipv4Addr> for IpAddr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<Ipv4Addr> for IpAddr {
/// Copies this address to a new `IpAddr::V4`.
///
/// # Examples
Expand All @@ -1111,7 +1112,8 @@ impl From<Ipv4Addr> for IpAddr {
}

#[stable(feature = "ip_from_ip", since = "1.16.0")]
impl From<Ipv6Addr> for IpAddr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<Ipv6Addr> for IpAddr {
/// Copies this address to a new `IpAddr::V6`.
///
/// # Examples
Expand Down Expand Up @@ -1221,7 +1223,8 @@ impl Ord for Ipv4Addr {
}

#[stable(feature = "ip_u32", since = "1.1.0")]
impl From<Ipv4Addr> for u32 {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<Ipv4Addr> for u32 {
/// Uses [`Ipv4Addr::to_bits`] to convert an IPv4 address to a host byte order `u32`.
#[inline]
fn from(ip: Ipv4Addr) -> u32 {
Expand All @@ -1230,7 +1233,8 @@ impl From<Ipv4Addr> for u32 {
}

#[stable(feature = "ip_u32", since = "1.1.0")]
impl From<u32> for Ipv4Addr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<u32> for Ipv4Addr {
/// Uses [`Ipv4Addr::from_bits`] to convert a host byte order `u32` into an IPv4 address.
#[inline]
fn from(ip: u32) -> Ipv4Addr {
Expand All @@ -1239,7 +1243,8 @@ impl From<u32> for Ipv4Addr {
}

#[stable(feature = "from_slice_v4", since = "1.9.0")]
impl From<[u8; 4]> for Ipv4Addr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<[u8; 4]> for Ipv4Addr {
/// Creates an `Ipv4Addr` from a four element byte array.
///
/// # Examples
Expand All @@ -1257,7 +1262,8 @@ impl From<[u8; 4]> for Ipv4Addr {
}

#[stable(feature = "ip_from_slice", since = "1.17.0")]
impl From<[u8; 4]> for IpAddr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<[u8; 4]> for IpAddr {
/// Creates an `IpAddr::V4` from a four element byte array.
///
/// # Examples
Expand Down Expand Up @@ -2211,15 +2217,17 @@ impl Ord for Ipv6Addr {
}

#[stable(feature = "i128", since = "1.26.0")]
impl From<Ipv6Addr> for u128 {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<Ipv6Addr> for u128 {
/// Uses [`Ipv6Addr::to_bits`] to convert an IPv6 address to a host byte order `u128`.
#[inline]
fn from(ip: Ipv6Addr) -> u128 {
ip.to_bits()
}
}
#[stable(feature = "i128", since = "1.26.0")]
impl From<u128> for Ipv6Addr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<u128> for Ipv6Addr {
/// Uses [`Ipv6Addr::from_bits`] to convert a host byte order `u128` to an IPv6 address.
#[inline]
fn from(ip: u128) -> Ipv6Addr {
Expand All @@ -2228,7 +2236,8 @@ impl From<u128> for Ipv6Addr {
}

#[stable(feature = "ipv6_from_octets", since = "1.9.0")]
impl From<[u8; 16]> for Ipv6Addr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<[u8; 16]> for Ipv6Addr {
/// Creates an `Ipv6Addr` from a sixteen element byte array.
///
/// # Examples
Expand All @@ -2255,7 +2264,8 @@ impl From<[u8; 16]> for Ipv6Addr {
}

#[stable(feature = "ipv6_from_segments", since = "1.16.0")]
impl From<[u16; 8]> for Ipv6Addr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<[u16; 8]> for Ipv6Addr {
/// Creates an `Ipv6Addr` from an eight element 16-bit array.
///
/// # Examples
Expand Down Expand Up @@ -2283,7 +2293,8 @@ impl From<[u16; 8]> for Ipv6Addr {
}

#[stable(feature = "ip_from_slice", since = "1.17.0")]
impl From<[u8; 16]> for IpAddr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<[u8; 16]> for IpAddr {
/// Creates an `IpAddr::V6` from a sixteen element byte array.
///
/// # Examples
Expand All @@ -2310,7 +2321,8 @@ impl From<[u8; 16]> for IpAddr {
}

#[stable(feature = "ip_from_slice", since = "1.17.0")]
impl From<[u16; 8]> for IpAddr {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl const From<[u16; 8]> for IpAddr {
/// Creates an `IpAddr::V6` from an eight element 16-bit array.
///
/// # Examples
Expand Down
Loading
Loading