Skip to content

Commit 4c78293

Browse files
authored
Unrolled build for rust-lang#125884
Rollup merge of rust-lang#125884 - Rua:integer_sign_cast, r=Mark-Simulacrum Implement feature `integer_sign_cast` Tracking issue: rust-lang#125882 Since this is my first time making a library addition I wasn't sure where to place the new code relative to existing code. I decided to place it near the top where there are already some other basic bitwise manipulation functions. If there is an official guideline for the ordering of functions, please let me know.
2 parents a6416d8 + b181e81 commit 4c78293

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

library/core/src/num/int_macros.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,30 @@ macro_rules! int_impl {
183183
(self as $UnsignedT).trailing_ones()
184184
}
185185

186+
/// Returns the bit pattern of `self` reinterpreted as an unsigned integer of the same size.
187+
///
188+
/// This produces the same result as an `as` cast, but ensures that the bit-width remains
189+
/// the same.
190+
///
191+
/// # Examples
192+
///
193+
/// Basic usage:
194+
///
195+
/// ```
196+
/// #![feature(integer_sign_cast)]
197+
///
198+
#[doc = concat!("let n = -1", stringify!($SelfT), ";")]
199+
///
200+
#[doc = concat!("assert_eq!(n.cast_unsigned(), ", stringify!($UnsignedT), "::MAX);")]
201+
/// ```
202+
#[unstable(feature = "integer_sign_cast", issue = "125882")]
203+
#[must_use = "this returns the result of the operation, \
204+
without modifying the original"]
205+
#[inline(always)]
206+
pub const fn cast_unsigned(self) -> $UnsignedT {
207+
self as $UnsignedT
208+
}
209+
186210
/// Shifts the bits to the left by a specified amount, `n`,
187211
/// wrapping the truncated bits to the end of the resulting integer.
188212
///

library/core/src/num/uint_macros.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,30 @@ macro_rules! uint_impl {
184184
(!self).trailing_zeros()
185185
}
186186

187+
/// Returns the bit pattern of `self` reinterpreted as a signed integer of the same size.
188+
///
189+
/// This produces the same result as an `as` cast, but ensures that the bit-width remains
190+
/// the same.
191+
///
192+
/// # Examples
193+
///
194+
/// Basic usage:
195+
///
196+
/// ```
197+
/// #![feature(integer_sign_cast)]
198+
///
199+
#[doc = concat!("let n = ", stringify!($SelfT), "::MAX;")]
200+
///
201+
#[doc = concat!("assert_eq!(n.cast_signed(), -1", stringify!($SignedT), ");")]
202+
/// ```
203+
#[unstable(feature = "integer_sign_cast", issue = "125882")]
204+
#[must_use = "this returns the result of the operation, \
205+
without modifying the original"]
206+
#[inline(always)]
207+
pub const fn cast_signed(self) -> $SignedT {
208+
self as $SignedT
209+
}
210+
187211
/// Shifts the bits to the left by a specified amount, `n`,
188212
/// wrapping the truncated bits to the end of the resulting integer.
189213
///

0 commit comments

Comments
 (0)