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 3 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
41 changes: 16 additions & 25 deletions library/core/src/internal_macros.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// 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 {
// FIXME: use macro_named_capture_groups to directly do impl $const(const)? instead
(impl $imp:ident, $method:ident for $t:ty, $(#[$attr:meta])+ $(, $const:tt)?) => {
$(#[$attr])+
impl $($const)? $imp for &$t {
type Output = <$t as $imp>::Output;

#[inline]
Expand All @@ -21,13 +18,10 @@ 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 {
// FIXME: use macro_named_capture_groups to directly do impl $const(const)? instead
(impl $imp:ident, $method:ident for $t:ty, $u:ty, $(#[$attr:meta])+ $(, $const:tt)?) => {
$(#[$attr])+
impl<'a> $($const)? $imp<$u> for &'a $t {
type Output = <$t as $imp<$u>>::Output;

#[inline]
Expand All @@ -37,8 +31,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 +42,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 +58,10 @@ 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 {
// FIXME: use macro_named_capture_groups to directly do impl $const(const)? instead
(impl $imp:ident, $method:ident for $t:ty, $u:ty, $(#[$attr:meta])+ $(, $const:tt)?) => {
$(#[$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);
}
Loading
Loading