Skip to content

Constify remaining traits/impls for const_ops #143949

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
38 changes: 13 additions & 25 deletions library/core/src/internal_macros.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
// implements the unary operator "op &T"
// based on "op T" where T is expected to be `Copy`able
macro_rules! forward_ref_unop {
(impl $imp:ident, $method:ident for $t:ty) => {
forward_ref_unop!(impl $imp, $method for $t,
#[stable(feature = "rust1", since = "1.0.0")]);
};
(impl $imp:ident, $method:ident for $t:ty, #[$attr:meta]) => {
#[$attr]
impl $imp for &$t {
(impl $imp:ident, $method:ident for $t:ty, $(#[$attr:meta])+) => {
$(#[$attr])+
impl const $imp for &$t {
type Output = <$t as $imp>::Output;

#[inline]
Expand All @@ -21,13 +17,9 @@ macro_rules! forward_ref_unop {
// implements binary operators "&T op U", "T op &U", "&T op &U"
// based on "T op U" where T and U are expected to be `Copy`able
macro_rules! forward_ref_binop {
(impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
forward_ref_binop!(impl $imp, $method for $t, $u,
#[stable(feature = "rust1", since = "1.0.0")]);
};
(impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
#[$attr]
impl<'a> $imp<$u> for &'a $t {
(impl $imp:ident, $method:ident for $t:ty, $u:ty, $(#[$attr:meta])+) => {
$(#[$attr])+
impl const $imp<$u> for &$t {
type Output = <$t as $imp<$u>>::Output;

#[inline]
Expand All @@ -37,8 +29,8 @@ macro_rules! forward_ref_binop {
}
}

#[$attr]
impl $imp<&$u> for $t {
$(#[$attr])+
impl const $imp<&$u> for $t {
type Output = <$t as $imp<$u>>::Output;

#[inline]
Expand All @@ -48,8 +40,8 @@ macro_rules! forward_ref_binop {
}
}

#[$attr]
impl $imp<&$u> for &$t {
$(#[$attr])+
impl const $imp<&$u> for &$t {
type Output = <$t as $imp<$u>>::Output;

#[inline]
Expand All @@ -64,13 +56,9 @@ macro_rules! forward_ref_binop {
// implements "T op= &U", based on "T op= U"
// where U is expected to be `Copy`able
macro_rules! forward_ref_op_assign {
(impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
forward_ref_op_assign!(impl $imp, $method for $t, $u,
#[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]);
};
(impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
#[$attr]
impl $imp<&$u> for $t {
(impl $imp:ident, $method:ident for $t:ty, $u:ty, $(#[$attr:meta])+) => {
$(#[$attr])+
impl const $imp<&$u> for $t {
#[inline]
#[track_caller]
fn $method(&mut self, other: &$u) {
Expand Down
47 changes: 30 additions & 17 deletions library/core/src/net/ip_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::display_buffer::DisplayBuffer;
use crate::cmp::Ordering;
use crate::fmt::{self, Write};
use crate::hash::{Hash, Hasher};
use crate::iter;
use crate::mem::transmute;
use crate::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not};

Expand Down Expand Up @@ -2337,20 +2336,24 @@ impl From<[u16; 8]> for IpAddr {
}

#[stable(feature = "ip_bitops", since = "1.75.0")]
impl Not for Ipv4Addr {
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Not for Ipv4Addr {
type Output = Ipv4Addr;

#[inline]
fn not(mut self) -> Ipv4Addr {
for octet in &mut self.octets {
*octet = !*octet;
let mut idx = 0;
while idx < 4 {
self.octets[idx] = !self.octets[idx];
idx += 1;
}
self
}
}

#[stable(feature = "ip_bitops", since = "1.75.0")]
impl Not for &'_ Ipv4Addr {
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Not for &'_ Ipv4Addr {
type Output = Ipv4Addr;

#[inline]
Expand All @@ -2360,20 +2363,24 @@ impl Not for &'_ Ipv4Addr {
}

#[stable(feature = "ip_bitops", since = "1.75.0")]
impl Not for Ipv6Addr {
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Not for Ipv6Addr {
type Output = Ipv6Addr;

#[inline]
fn not(mut self) -> Ipv6Addr {
for octet in &mut self.octets {
*octet = !*octet;
let mut idx = 0;
while idx < 16 {
self.octets[idx] = !self.octets[idx];
idx += 1;
}
self
}
}

#[stable(feature = "ip_bitops", since = "1.75.0")]
impl Not for &'_ Ipv6Addr {
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Not for &'_ Ipv6Addr {
type Output = Ipv6Addr;

#[inline]
Expand All @@ -2389,23 +2396,25 @@ macro_rules! bitop_impls {
)*) => {
$(
$(#[$attr])*
impl $BitOpAssign for $ty {
impl const $BitOpAssign for $ty {
fn $bitop_assign(&mut self, rhs: $ty) {
for (lhs, rhs) in iter::zip(&mut self.octets, rhs.octets) {
lhs.$bitop_assign(rhs);
let mut idx = 0;
while idx < self.octets.len() {
self.octets[idx].$bitop_assign(rhs.octets[idx]);
idx += 1;
}
}
}

$(#[$attr])*
impl $BitOpAssign<&'_ $ty> for $ty {
impl const $BitOpAssign<&'_ $ty> for $ty {
fn $bitop_assign(&mut self, rhs: &'_ $ty) {
self.$bitop_assign(*rhs);
}
}

$(#[$attr])*
impl $BitOp for $ty {
impl const $BitOp for $ty {
type Output = $ty;

#[inline]
Expand All @@ -2416,7 +2425,7 @@ macro_rules! bitop_impls {
}

$(#[$attr])*
impl $BitOp<&'_ $ty> for $ty {
impl const $BitOp<&'_ $ty> for $ty {
type Output = $ty;

#[inline]
Expand All @@ -2427,7 +2436,7 @@ macro_rules! bitop_impls {
}

$(#[$attr])*
impl $BitOp<$ty> for &'_ $ty {
impl const $BitOp<$ty> for &'_ $ty {
type Output = $ty;

#[inline]
Expand All @@ -2439,7 +2448,7 @@ macro_rules! bitop_impls {
}

$(#[$attr])*
impl $BitOp<&'_ $ty> for &'_ $ty {
impl const $BitOp<&'_ $ty> for &'_ $ty {
type Output = $ty;

#[inline]
Expand All @@ -2455,12 +2464,16 @@ macro_rules! bitop_impls {

bitop_impls! {
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl (BitAnd, BitAndAssign) for Ipv4Addr = (bitand, bitand_assign);
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl (BitOr, BitOrAssign) for Ipv4Addr = (bitor, bitor_assign);

#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl (BitAnd, BitAndAssign) for Ipv6Addr = (bitand, bitand_assign);
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl (BitOr, BitOrAssign) for Ipv6Addr = (bitor, bitor_assign);
}
18 changes: 12 additions & 6 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
// Impls for unsigned nonzero types only.
(unsigned $Int:ty) => {
#[stable(feature = "nonzero_div", since = "1.51.0")]
impl Div<NonZero<$Int>> for $Int {
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Div<NonZero<$Int>> for $Int {
type Output = $Int;

/// Same as `self / other.get()`, but because `other` is a `NonZero<_>`,
Expand All @@ -1256,7 +1257,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
}

#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
impl DivAssign<NonZero<$Int>> for $Int {
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const DivAssign<NonZero<$Int>> for $Int {
/// Same as `self /= other.get()`, but because `other` is a `NonZero<_>`,
/// there's never a runtime check for division-by-zero.
///
Expand All @@ -1269,7 +1271,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
}

#[stable(feature = "nonzero_div", since = "1.51.0")]
impl Rem<NonZero<$Int>> for $Int {
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Rem<NonZero<$Int>> for $Int {
type Output = $Int;

/// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic.
Expand All @@ -1282,7 +1285,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
}

#[stable(feature = "nonzero_div_assign", since = "1.79.0")]
impl RemAssign<NonZero<$Int>> for $Int {
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const RemAssign<NonZero<$Int>> for $Int {
/// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic.
#[inline]
fn rem_assign(&mut self, other: NonZero<$Int>) {
Expand Down Expand Up @@ -1322,7 +1326,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
// Impls for signed nonzero types only.
(signed $Int:ty) => {
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
impl Neg for NonZero<$Int> {
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
impl const Neg for NonZero<$Int> {
type Output = Self;

#[inline]
Expand All @@ -1333,7 +1338,8 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
}

forward_ref_unop! { impl Neg, neg for NonZero<$Int>,
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")] }
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
};
}

Expand Down
Loading
Loading