requested changes · rust-lang/rust@f2e8807 (original) (raw)

`@@ -1413,32 +1413,37 @@ macro_rules! int_impl {

`

1413

1413

`}

`

1414

1414

`}

`

1415

1415

``

1416

``

`` -

/// Exact shift left. Computes self << rhs, returning None if any bits disagreeing with

``

1417

``

`-

/// the resulting sign bit would be shifted out.

`

``

1416

`` +

/// Exact shift left. Computes self << rhs, returning None if any bits that would be

``

``

1417

`` +

/// shifted out differ from the resulting sign bit or if rhs >=

``

``

1418

`` +

#[doc = concat!("", stringify!($SelfT), "::BITS.")]

``

1418

1419

`///

`

1419

1420

`/// # Examples

`

1420

1421

`///

`

1421

1422

```` /// ```


`1422`

`1423`

`/// #![feature(exact_bitshifts)]

`

`1423`

`1424`

`///

`

`1424`

`1425`

` #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".exact_shl(4), Some(0x10));")]

`

`1425`

``

`-

#[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".exact_shl(129), None);")]

`

``

`1426`

`+

#[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".exact_shl(", stringify!($SelfT), "::BITS - 2), Some(1 << ", stringify!($SelfT), "::BITS - 2));")]

`

``

`1427`

`+

#[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".exact_shl(", stringify!($SelfT), "::BITS - 1), None);")]

`

``

`1428`

`+

#[doc = concat!("assert_eq!((-0x2", stringify!($SelfT), ").exact_shl(", stringify!($SelfT), "::BITS - 2), Some(-0x2 << ", stringify!($SelfT), "::BITS - 2));")]

`

``

`1429`

`+

#[doc = concat!("assert_eq!((-0x2", stringify!($SelfT), ").exact_shl(", stringify!($SelfT), "::BITS - 1), None);")]

`

`1426`

`1430`

```` /// ```

1427

1431

` #[unstable(feature = "exact_bitshifts", issue = "144336")]

`

1428

1432

` #[must_use = "this returns the result of the operation, \

`

1429

1433

` without modifying the original"]

`

1430

1434

` #[inline]

`

1431

``

`-

pub fn exact_shl(self, rhs: u32) -> Option<$SelfT> {

`

1432

``

`-

if rhs < self.leading_zeros().max(self.leading_ones()) {

`

``

1435

`+

pub const fn exact_shl(self, rhs: u32) -> Option<$SelfT> {

`

``

1436

`+

if rhs < self.leading_zeros() || rhs < self.leading_ones() {

`

1433

1437

`// SAFETY: rhs is checked above

`

1434

1438

`Some(unsafe { self.unchecked_shl(rhs) })

`

1435

1439

`} else {

`

1436

1440

`None

`

1437

1441

`}

`

1438

1442

`}

`

1439

1443

``

1440

``

`` -

/// Unchecked exact shift left. Computes self << rhs, assuming bits disagreeing with the

``

1441

``

`-

/// resulting sign bit cannot be shifted out.

`

``

1444

`` +

/// Unchecked exact shift left. Computes self << rhs, assuming all bits that are shifted

``

``

1445

`+

/// out are the same as the resulting sign bit and rhs cannot be larger than

`

``

1446

`` +

#[doc = concat!("", stringify!($SelfT), "::BITS.")]

``

1442

1447

`///

`

1443

1448

`/// # Safety

`

1444

1449

`///

`

`@@ -1450,14 +1455,15 @@ macro_rules! int_impl {

`

1450

1455

` #[must_use = "this returns the result of the operation, \

`

1451

1456

` without modifying the original"]

`

1452

1457

` #[inline]

`

1453

``

`-

pub unsafe fn exact_shl_unchecked(self, rhs: u32) -> $SelfT {

`

``

1458

`+

pub const unsafe fn unchecked_exact_shl(self, rhs: u32) -> $SelfT {

`

1454

1459

` assert_unsafe_precondition!(

`

1455

1460

` check_language_ub,

`

1456

``

`-

concat!(stringify!($SelfT), "::exact_shl_unchecked cannot shift out non-zero bits"),

`

``

1461

`+

concat!(stringify!($SelfT), "::unchecked_exact_shl cannot shift out non-zero bits"),

`

1457

1462

`(

`

1458

``

`-

len: u32 = self.leading_zeros().max(self.leading_ones()),

`

``

1463

`+

zeros: u32 = self.leading_zeros(),

`

``

1464

`+

ones: u32 = self.leading_ones(),

`

1459

1465

` rhs: u32 = rhs,

`

1460

``

`-

) => rhs < len,

`

``

1466

`+

) => rhs < zeros || rhs < ones,

`

1461

1467

`);

`

1462

1468

``

1463

1469

`// SAFETY: this is guaranteed to be safe by the caller

`

`@@ -1595,14 +1601,14 @@ macro_rules! int_impl {

`

1595

1601

`/// #![feature(exact_bitshifts)]

`

1596

1602

`///

`

1597

1603

` #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".exact_shr(4), Some(0x1));")]

`

1598

``

`-

#[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".exact_shr(129), None);")]

`

``

1604

`+

#[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".exact_shr(5), None);")]

`

1599

1605

```` /// ```

````

1600

1606

` #[unstable(feature = "exact_bitshifts", issue = "144336")]

`

1601

1607

` #[must_use = "this returns the result of the operation, \

`

1602

1608

` without modifying the original"]

`

1603

1609

` #[inline]

`

1604

``

`-

pub fn exact_shr(self, rhs: u32) -> Option<$SelfT> {

`

1605

``

`-

if rhs <= self.trailing_zeros().max(<$SelfT>::BITS - 1) {

`

``

1610

`+

pub const fn exact_shr(self, rhs: u32) -> Option<$SelfT> {

`

``

1611

`+

if rhs <= self.trailing_zeros() && rhs < <$SelfT>::BITS {

`

1606

1612

`// SAFETY: rhs is checked above

`

1607

1613

`Some(unsafe { self.unchecked_shr(rhs) })

`

1608

1614

`} else {

`

`@@ -1625,14 +1631,15 @@ macro_rules! int_impl {

`

1625

1631

` #[must_use = "this returns the result of the operation, \

`

1626

1632

` without modifying the original"]

`

1627

1633

` #[inline]

`

1628

``

`-

pub unsafe fn exact_shr_unchecked(self, rhs: u32) -> $SelfT {

`

``

1634

`+

pub const unsafe fn unchecked_exact_shr(self, rhs: u32) -> $SelfT {

`

1629

1635

` assert_unsafe_precondition!(

`

1630

1636

` check_language_ub,

`

1631

``

`-

concat!(stringify!($SelfT), "::exact_shr_unchecked cannot shift out non-zero bits"),

`

``

1637

`+

concat!(stringify!($SelfT), "::unchecked_exact_shr cannot shift out non-zero bits"),

`

1632

1638

`(

`

1633

``

`-

len: u32 = self.trailing_zeros().max(<$SelfT>::BITS - 1),

`

``

1639

`+

zeros: u32 = self.trailing_zeros(),

`

``

1640

`+

bits: u32 = <$SelfT>::BITS,

`

1634

1641

` rhs: u32 = rhs,

`

1635

``

`-

) => rhs <= len,

`

``

1642

`+

) => rhs <= zeros && rhs < bits,

`

1636

1643

`);

`

1637

1644

``

1638

1645

`// SAFETY: this is guaranteed to be safe by the caller

`