Skip to content

Commit 4ff1174

Browse files
committed
From and TryFrom
1 parent cdac44e commit 4ff1174

File tree

15 files changed

+146
-66
lines changed

15 files changed

+146
-66
lines changed

library/core/src/array/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ impl Error for TryFromSliceError {
198198
}
199199

200200
#[stable(feature = "try_from_slice_error", since = "1.36.0")]
201-
impl From<Infallible> for TryFromSliceError {
201+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
202+
impl const From<Infallible> for TryFromSliceError {
202203
fn from(x: Infallible) -> TryFromSliceError {
203204
match x {}
204205
}

library/core/src/ascii/ascii_char.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,8 @@ macro_rules! into_int_impl {
546546
($($ty:ty)*) => {
547547
$(
548548
#[unstable(feature = "ascii_char", issue = "110998")]
549-
impl From<AsciiChar> for $ty {
549+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
550+
impl const From<AsciiChar> for $ty {
550551
#[inline]
551552
fn from(chr: AsciiChar) -> $ty {
552553
chr as u8 as $ty

library/core/src/char/convert.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ pub(super) const unsafe fn from_u32_unchecked(i: u32) -> char {
3636
}
3737

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

5556
#[stable(feature = "more_char_conversions", since = "1.51.0")]
56-
impl From<char> for u64 {
57+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
58+
impl const From<char> for u64 {
5759
/// Converts a [`char`] into a [`u64`].
5860
///
5961
/// # Examples
@@ -72,7 +74,8 @@ impl From<char> for u64 {
7274
}
7375

7476
#[stable(feature = "more_char_conversions", since = "1.51.0")]
75-
impl From<char> for u128 {
77+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
78+
impl const From<char> for u128 {
7679
/// Converts a [`char`] into a [`u128`].
7780
///
7881
/// # Examples
@@ -157,7 +160,8 @@ impl TryFrom<char> for u16 {
157160
/// for a superset of Windows-1252 that fills the remaining blanks with corresponding
158161
/// C0 and C1 control codes.
159162
#[stable(feature = "char_convert", since = "1.13.0")]
160-
impl From<u8> for char {
163+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
164+
impl const From<u8> for char {
161165
/// Converts a [`u8`] into a [`char`].
162166
///
163167
/// # Examples
@@ -247,7 +251,8 @@ const fn char_try_from_u32(i: u32) -> Result<char, CharTryFromError> {
247251
}
248252

249253
#[stable(feature = "try_from", since = "1.34.0")]
250-
impl TryFrom<u32> for char {
254+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
255+
impl const TryFrom<u32> for char {
251256
type Error = CharTryFromError;
252257

253258
#[inline]

library/core/src/convert/mod.rs

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ pub const fn identity<T>(x: T) -> T {
216216
/// ```
217217
#[stable(feature = "rust1", since = "1.0.0")]
218218
#[rustc_diagnostic_item = "AsRef"]
219+
#[const_trait]
220+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
219221
pub trait AsRef<T: PointeeSized>: PointeeSized {
220222
/// Converts this type into a shared reference of the (usually inferred) input type.
221223
#[stable(feature = "rust1", since = "1.0.0")]
@@ -367,6 +369,8 @@ pub trait AsRef<T: PointeeSized>: PointeeSized {
367369
/// `&mut Vec<u8>`, for example, is the better choice (callers need to pass the correct type then).
368370
#[stable(feature = "rust1", since = "1.0.0")]
369371
#[rustc_diagnostic_item = "AsMut"]
372+
#[const_trait]
373+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
370374
pub trait AsMut<T: PointeeSized>: PointeeSized {
371375
/// Converts this type into a mutable reference of the (usually inferred) input type.
372376
#[stable(feature = "rust1", since = "1.0.0")]
@@ -445,6 +449,8 @@ pub trait AsMut<T: PointeeSized>: PointeeSized {
445449
#[rustc_diagnostic_item = "Into"]
446450
#[stable(feature = "rust1", since = "1.0.0")]
447451
#[doc(search_unbox)]
452+
#[const_trait]
453+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
448454
pub trait Into<T>: Sized {
449455
/// Converts this type into the (usually inferred) input type.
450456
#[must_use]
@@ -580,6 +586,8 @@ pub trait Into<T>: Sized {
580586
note = "to coerce a `{T}` into a `{Self}`, use `&*` as a prefix",
581587
))]
582588
#[doc(search_unbox)]
589+
#[const_trait]
590+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
583591
pub trait From<T>: Sized {
584592
/// Converts to this type from the input type.
585593
#[rustc_diagnostic_item = "from_fn"]
@@ -607,6 +615,8 @@ pub trait From<T>: Sized {
607615
/// [`Into`], see there for details.
608616
#[rustc_diagnostic_item = "TryInto"]
609617
#[stable(feature = "try_from", since = "1.34.0")]
618+
#[const_trait]
619+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
610620
pub trait TryInto<T>: Sized {
611621
/// The type returned in the event of a conversion error.
612622
#[stable(feature = "try_from", since = "1.34.0")]
@@ -685,6 +695,8 @@ pub trait TryInto<T>: Sized {
685695
/// [`try_from`]: TryFrom::try_from
686696
#[rustc_diagnostic_item = "TryFrom"]
687697
#[stable(feature = "try_from", since = "1.34.0")]
698+
#[const_trait]
699+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
688700
pub trait TryFrom<T>: Sized {
689701
/// The type returned in the event of a conversion error.
690702
#[stable(feature = "try_from", since = "1.34.0")]
@@ -702,9 +714,10 @@ pub trait TryFrom<T>: Sized {
702714

703715
// As lifts over &
704716
#[stable(feature = "rust1", since = "1.0.0")]
705-
impl<T: PointeeSized, U: PointeeSized> AsRef<U> for &T
717+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
718+
impl<T: PointeeSized, U: PointeeSized> const AsRef<U> for &T
706719
where
707-
T: AsRef<U>,
720+
T: ~const AsRef<U>,
708721
{
709722
#[inline]
710723
fn as_ref(&self) -> &U {
@@ -714,9 +727,10 @@ where
714727

715728
// As lifts over &mut
716729
#[stable(feature = "rust1", since = "1.0.0")]
717-
impl<T: PointeeSized, U: PointeeSized> AsRef<U> for &mut T
730+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
731+
impl<T: PointeeSized, U: PointeeSized> const AsRef<U> for &mut T
718732
where
719-
T: AsRef<U>,
733+
T: ~const AsRef<U>,
720734
{
721735
#[inline]
722736
fn as_ref(&self) -> &U {
@@ -734,9 +748,10 @@ where
734748

735749
// AsMut lifts over &mut
736750
#[stable(feature = "rust1", since = "1.0.0")]
737-
impl<T: PointeeSized, U: PointeeSized> AsMut<U> for &mut T
751+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
752+
impl<T: PointeeSized, U: PointeeSized> const AsMut<U> for &mut T
738753
where
739-
T: AsMut<U>,
754+
T: ~const AsMut<U>,
740755
{
741756
#[inline]
742757
fn as_mut(&mut self) -> &mut U {
@@ -754,9 +769,10 @@ where
754769

755770
// From implies Into
756771
#[stable(feature = "rust1", since = "1.0.0")]
757-
impl<T, U> Into<U> for T
772+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
773+
impl<T, U> const Into<U> for T
758774
where
759-
U: From<T>,
775+
U: ~const From<T>,
760776
{
761777
/// Calls `U::from(self)`.
762778
///
@@ -771,7 +787,8 @@ where
771787

772788
// From (and thus Into) is reflexive
773789
#[stable(feature = "rust1", since = "1.0.0")]
774-
impl<T> From<T> for T {
790+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
791+
impl<T> const From<T> for T {
775792
/// Returns the argument unchanged.
776793
#[inline(always)]
777794
fn from(t: T) -> T {
@@ -795,9 +812,10 @@ impl<T> From<!> for T {
795812

796813
// TryFrom implies TryInto
797814
#[stable(feature = "try_from", since = "1.34.0")]
798-
impl<T, U> TryInto<U> for T
815+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
816+
impl<T, U> const TryInto<U> for T
799817
where
800-
U: TryFrom<T>,
818+
U: ~const TryFrom<T>,
801819
{
802820
type Error = U::Error;
803821

@@ -810,9 +828,10 @@ where
810828
// Infallible conversions are semantically equivalent to fallible conversions
811829
// with an uninhabited error type.
812830
#[stable(feature = "try_from", since = "1.34.0")]
813-
impl<T, U> TryFrom<U> for T
831+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
832+
impl<T, U> const TryFrom<U> for T
814833
where
815-
U: Into<T>,
834+
U: ~const Into<T>,
816835
{
817836
type Error = Infallible;
818837

@@ -827,31 +846,35 @@ where
827846
////////////////////////////////////////////////////////////////////////////////
828847

829848
#[stable(feature = "rust1", since = "1.0.0")]
830-
impl<T> AsRef<[T]> for [T] {
849+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
850+
impl<T> const AsRef<[T]> for [T] {
831851
#[inline(always)]
832852
fn as_ref(&self) -> &[T] {
833853
self
834854
}
835855
}
836856

837857
#[stable(feature = "rust1", since = "1.0.0")]
838-
impl<T> AsMut<[T]> for [T] {
858+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
859+
impl<T> const AsMut<[T]> for [T] {
839860
#[inline(always)]
840861
fn as_mut(&mut self) -> &mut [T] {
841862
self
842863
}
843864
}
844865

845866
#[stable(feature = "rust1", since = "1.0.0")]
846-
impl AsRef<str> for str {
867+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
868+
impl const AsRef<str> for str {
847869
#[inline(always)]
848870
fn as_ref(&self) -> &str {
849871
self
850872
}
851873
}
852874

853875
#[stable(feature = "as_mut_str_for_str", since = "1.51.0")]
854-
impl AsMut<str> for str {
876+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
877+
impl const AsMut<str> for str {
855878
#[inline(always)]
856879
fn as_mut(&mut self) -> &mut str {
857880
self
@@ -912,7 +935,8 @@ impl AsMut<str> for str {
912935
pub enum Infallible {}
913936

914937
#[stable(feature = "convert_infallible", since = "1.34.0")]
915-
impl Clone for Infallible {
938+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
939+
impl const Clone for Infallible {
916940
fn clone(&self) -> Infallible {
917941
match *self {}
918942
}
@@ -940,7 +964,8 @@ impl Error for Infallible {
940964
}
941965

942966
#[stable(feature = "convert_infallible", since = "1.34.0")]
943-
impl PartialEq for Infallible {
967+
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
968+
impl const PartialEq for Infallible {
944969
fn eq(&self, _: &Infallible) -> bool {
945970
match *self {}
946971
}
@@ -964,7 +989,8 @@ impl Ord for Infallible {
964989
}
965990

966991
#[stable(feature = "convert_infallible", since = "1.34.0")]
967-
impl From<!> for Infallible {
992+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
993+
impl const From<!> for Infallible {
968994
#[inline]
969995
fn from(x: !) -> Self {
970996
x

library/core/src/convert/num.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ macro_rules! impl_from {
6969
};
7070
($Small:ty => $Large:ty, #[$attr:meta], $doc:expr $(,)?) => {
7171
#[$attr]
72-
impl From<$Small> for $Large {
72+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
73+
impl const From<$Small> for $Large {
7374
// Rustdocs on the impl block show a "[+] show undocumented items" toggle.
7475
// Rustdocs on functions do not.
7576
#[doc = $doc]
@@ -200,7 +201,8 @@ macro_rules! impl_float_from_bool {
200201
)?
201202
) => {
202203
#[stable(feature = "float_from_bool", since = "1.68.0")]
203-
impl From<bool> for $float {
204+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
205+
impl const From<bool> for $float {
204206
#[doc = concat!("Converts a [`bool`] to [`", stringify!($float),"`] losslessly.")]
205207
/// The resulting value is positive `0.0` for `false` and `1.0` for `true` values.
206208
///
@@ -250,7 +252,8 @@ impl_float_from_bool!(
250252
macro_rules! impl_try_from_unbounded {
251253
($source:ty => $($target:ty),+) => {$(
252254
#[stable(feature = "try_from", since = "1.34.0")]
253-
impl TryFrom<$source> for $target {
255+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
256+
impl const TryFrom<$source> for $target {
254257
type Error = TryFromIntError;
255258

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

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

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

318324
/// Tries to create the target number type from a source
@@ -450,7 +456,8 @@ use crate::num::NonZero;
450456
macro_rules! impl_nonzero_int_from_nonzero_int {
451457
($Small:ty => $Large:ty) => {
452458
#[stable(feature = "nz_int_conv", since = "1.41.0")]
453-
impl From<NonZero<$Small>> for NonZero<$Large> {
459+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
460+
impl const From<NonZero<$Small>> for NonZero<$Large> {
454461
// Rustdocs on the impl block show a "[+] show undocumented items" toggle.
455462
// Rustdocs on functions do not.
456463
#[doc = concat!("Converts <code>[NonZero]\\<[", stringify!($Small), "]></code> ")]
@@ -540,7 +547,8 @@ impl_nonzero_int_try_from_int!(isize);
540547
macro_rules! impl_nonzero_int_try_from_nonzero_int {
541548
($source:ty => $($target:ty),+) => {$(
542549
#[stable(feature = "nzint_try_from_nzint_conv", since = "1.49.0")]
543-
impl TryFrom<NonZero<$source>> for NonZero<$target> {
550+
#[rustc_const_unstable(feature = "const_trait_impl", issue = "67792")]
551+
impl const TryFrom<NonZero<$source>> for NonZero<$target> {
544552
type Error = TryFromIntError;
545553

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

0 commit comments

Comments
 (0)