Skip to content

Commit 68c8339

Browse files
committed
Make truncate and extend use const traits to work in const contexts
1 parent ea421bf commit 68c8339

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

‎library/core/src/num/int_macros.rs‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3958,9 +3958,10 @@ macro_rules! int_impl {
39583958
/// ```
39593959
#[must_use = "this returns the truncated value and does not modify the original"]
39603960
#[unstable(feature = "integer_extend_truncate", issue = "154330")]
3961+
#[rustc_const_unstable(feature = "integer_truncate_extend", issue = "154330")]
39613962
#[inline]
3962-
pub fn truncate<Target>(self) -> Target
3963-
where Self: traits::TruncateTarget<Target>
3963+
pub const fn truncate<Target>(self) -> Target
3964+
where Self: [const] traits::TruncateTarget<Target>
39643965
{
39653966
traits::TruncateTarget::internal_truncate(self)
39663967
}
@@ -3979,9 +3980,10 @@ macro_rules! int_impl {
39793980
/// ```
39803981
#[must_use = "this returns the truncated value and does not modify the original"]
39813982
#[unstable(feature = "integer_extend_truncate", issue = "154330")]
3983+
#[rustc_const_unstable(feature = "integer_truncate_extend", issue = "154330")]
39823984
#[inline]
3983-
pub fn saturating_truncate<Target>(self) -> Target
3984-
where Self: traits::TruncateTarget<Target>
3985+
pub const fn saturating_truncate<Target>(self) -> Target
3986+
where Self: [const] traits::TruncateTarget<Target>
39853987
{
39863988
traits::TruncateTarget::internal_saturating_truncate(self)
39873989
}
@@ -4000,9 +4002,10 @@ macro_rules! int_impl {
40004002
/// ```
40014003
#[must_use = "this returns the truncated value and does not modify the original"]
40024004
#[unstable(feature = "integer_extend_truncate", issue = "154330")]
4005+
#[rustc_const_unstable(feature = "integer_truncate_extend", issue = "154330")]
40034006
#[inline]
4004-
pub fn checked_truncate<Target>(self) -> Option<Target>
4005-
where Self: traits::TruncateTarget<Target>
4007+
pub const fn checked_truncate<Target>(self) -> Option<Target>
4008+
where Self: [const] traits::TruncateTarget<Target>
40064009
{
40074010
traits::TruncateTarget::internal_checked_truncate(self)
40084011
}
@@ -4018,9 +4021,10 @@ macro_rules! int_impl {
40184021
/// ```
40194022
#[must_use = "this returns the extended value and does not modify the original"]
40204023
#[unstable(feature = "integer_extend_truncate", issue = "154330")]
4024+
#[rustc_const_unstable(feature = "integer_truncate_extend", issue = "154330")]
40214025
#[inline]
4022-
pub fn extend<Target>(self) -> Target
4023-
where Self: traits::ExtendTarget<Target>
4026+
pub const fn extend<Target>(self) -> Target
4027+
where Self: [const] traits::ExtendTarget<Target>
40244028
{
40254029
traits::ExtendTarget::internal_extend(self)
40264030
}

‎library/core/src/num/traits.rs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
/// Trait for types that this type can be truncated to
55
#[unstable(feature = "num_internals", reason = "internal implementation detail", issue = "none")]
6-
pub trait TruncateTarget<Target>: crate::sealed::Sealed {
6+
#[rustc_const_unstable(feature = "integer_truncate_extend", issue = "154330")]
7+
pub const trait TruncateTarget<Target>: crate::sealed::Sealed {
78
#[doc(hidden)]
89
fn internal_truncate(self) -> Target;
910

@@ -16,7 +17,8 @@ pub trait TruncateTarget<Target>: crate::sealed::Sealed {
1617

1718
/// Trait for types that this type can be truncated to
1819
#[unstable(feature = "num_internals", reason = "internal implementation detail", issue = "none")]
19-
pub trait ExtendTarget<Target>: crate::sealed::Sealed {
20+
#[rustc_const_unstable(feature = "integer_truncate_extend", issue = "154330")]
21+
pub const trait ExtendTarget<Target>: crate::sealed::Sealed {
2022
#[doc(hidden)]
2123
fn internal_extend(self) -> Target;
2224
}
@@ -38,7 +40,8 @@ macro_rules! impl_truncate {
3840
);
3941

4042
#[unstable(feature = "num_internals", reason = "internal implementation detail", issue = "none")]
41-
impl TruncateTarget<$to> for $from {
43+
#[rustc_const_unstable(feature = "integer_truncate_extend", issue = "154330")]
44+
impl const TruncateTarget<$to> for $from {
4245
#[inline]
4346
fn internal_truncate(self) -> $to {
4447
self as _
@@ -84,7 +87,8 @@ macro_rules! impl_extend {
8487
);
8588

8689
#[unstable(feature = "num_internals", reason = "internal implementation detail", issue = "none")]
87-
impl ExtendTarget<$to> for $from {
90+
#[rustc_const_unstable(feature = "integer_truncate_extend", issue = "154330")]
91+
impl const ExtendTarget<$to> for $from {
8892
fn internal_extend(self) -> $to {
8993
self as _
9094
}

‎library/core/src/num/uint_macros.rs‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4120,9 +4120,10 @@ macro_rules! uint_impl {
41204120
/// ```
41214121
#[must_use = "this returns the truncated value and does not modify the original"]
41224122
#[unstable(feature = "integer_extend_truncate", issue = "154330")]
4123+
#[rustc_const_unstable(feature = "integer_truncate_extend", issue = "154330")]
41234124
#[inline]
4124-
pub fn truncate<Target>(self) -> Target
4125-
where Self: traits::TruncateTarget<Target>
4125+
pub const fn truncate<Target>(self) -> Target
4126+
where Self: [const] traits::TruncateTarget<Target>
41264127
{
41274128
traits::TruncateTarget::internal_truncate(self)
41284129
}
@@ -4139,9 +4140,10 @@ macro_rules! uint_impl {
41394140
/// ```
41404141
#[must_use = "this returns the truncated value and does not modify the original"]
41414142
#[unstable(feature = "integer_extend_truncate", issue = "154330")]
4143+
#[rustc_const_unstable(feature = "integer_truncate_extend", issue = "154330")]
41424144
#[inline]
4143-
pub fn saturating_truncate<Target>(self) -> Target
4144-
where Self: traits::TruncateTarget<Target>
4145+
pub const fn saturating_truncate<Target>(self) -> Target
4146+
where Self: [const] traits::TruncateTarget<Target>
41454147
{
41464148
traits::TruncateTarget::internal_saturating_truncate(self)
41474149
}
@@ -4158,9 +4160,10 @@ macro_rules! uint_impl {
41584160
/// ```
41594161
#[must_use = "this returns the truncated value and does not modify the original"]
41604162
#[unstable(feature = "integer_extend_truncate", issue = "154330")]
4163+
#[rustc_const_unstable(feature = "integer_truncate_extend", issue = "154330")]
41614164
#[inline]
4162-
pub fn checked_truncate<Target>(self) -> Option<Target>
4163-
where Self: traits::TruncateTarget<Target>
4165+
pub const fn checked_truncate<Target>(self) -> Option<Target>
4166+
where Self: [const] traits::TruncateTarget<Target>
41644167
{
41654168
traits::TruncateTarget::internal_checked_truncate(self)
41664169
}
@@ -4175,9 +4178,10 @@ macro_rules! uint_impl {
41754178
/// ```
41764179
#[must_use = "this returns the extended value and does not modify the original"]
41774180
#[unstable(feature = "integer_extend_truncate", issue = "154330")]
4181+
#[rustc_const_unstable(feature = "integer_truncate_extend", issue = "154330")]
41784182
#[inline]
4179-
pub fn extend<Target>(self) -> Target
4180-
where Self: traits::ExtendTarget<Target>
4183+
pub const fn extend<Target>(self) -> Target
4184+
where Self: [const] traits::ExtendTarget<Target>
41814185
{
41824186
traits::ExtendTarget::internal_extend(self)
41834187
}

0 commit comments

Comments
 (0)