Skip to content

Commit b6e03c4

Browse files
authored
Rollup merge of #69813 - thomcc:nonzero-bitor, r=Amanieu
Implement BitOr and BitOrAssign for the NonZero integer types This provides overloaded operators for `NonZero$Int | NonZero$Int`, `NonZero$Int | $Int`, and `$Int | NonZero$Int`. It also provides `BitOrAssign` where `self` is `NonZero$Int`, for symmetry. It's a pretty small conceptual addition, but is good becasue but avoids a case where the operation is obviously sound, but you'd otherwise need unsafe to do it. In crates trying to minimize `unsafe` usage, this is unfortunate and makes working with `NonZero` types often not worth it, even if the operations you're doing are clearly sound. I've marked these as stable as I've been told in the past that trait impls are automatically stable. I'm happy to change it to unstable if this wasn't correct information. I'm not entirely confident what version I should have put down, so I followed https://www.whatrustisit.com. Hopefully it's correct for this. Apologies in advance if this has come up before, but I couldn't find it.
2 parents 7b7c63c + 6cdb825 commit b6e03c4

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/libcore/num/mod.rs

+52
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::convert::Infallible;
88
use crate::fmt;
99
use crate::intrinsics;
1010
use crate::mem;
11+
use crate::ops::{BitOr, BitOrAssign};
1112
use crate::str::FromStr;
1213

1314
// Used because the `?` operator is not allowed in a const context.
@@ -110,6 +111,57 @@ assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", s
110111
}
111112
}
112113

114+
#[stable(feature = "nonzero_bitor", since = "1.43.0")]
115+
impl BitOr for $Ty {
116+
type Output = Self;
117+
#[inline]
118+
fn bitor(self, rhs: Self) -> Self::Output {
119+
// Safety: since `self` and `rhs` are both nonzero, the
120+
// result of the bitwise-or will be nonzero.
121+
unsafe { $Ty::new_unchecked(self.get() | rhs.get()) }
122+
}
123+
}
124+
125+
#[stable(feature = "nonzero_bitor", since = "1.43.0")]
126+
impl BitOr<$Int> for $Ty {
127+
type Output = Self;
128+
#[inline]
129+
fn bitor(self, rhs: $Int) -> Self::Output {
130+
// Safety: since `self` is nonzero, the result of the
131+
// bitwise-or will be nonzero regardless of the value of
132+
// `rhs`.
133+
unsafe { $Ty::new_unchecked(self.get() | rhs) }
134+
}
135+
}
136+
137+
#[stable(feature = "nonzero_bitor", since = "1.43.0")]
138+
impl BitOr<$Ty> for $Int {
139+
type Output = $Ty;
140+
#[inline]
141+
fn bitor(self, rhs: $Ty) -> Self::Output {
142+
// Safety: since `rhs` is nonzero, the result of the
143+
// bitwise-or will be nonzero regardless of the value of
144+
// `self`.
145+
unsafe { $Ty::new_unchecked(self | rhs.get()) }
146+
}
147+
}
148+
149+
#[stable(feature = "nonzero_bitor", since = "1.43.0")]
150+
impl BitOrAssign for $Ty {
151+
#[inline]
152+
fn bitor_assign(&mut self, rhs: Self) {
153+
*self = *self | rhs;
154+
}
155+
}
156+
157+
#[stable(feature = "nonzero_bitor", since = "1.43.0")]
158+
impl BitOrAssign<$Int> for $Ty {
159+
#[inline]
160+
fn bitor_assign(&mut self, rhs: $Int) {
161+
*self = *self | rhs;
162+
}
163+
}
164+
113165
impl_nonzero_fmt! {
114166
#[$stability] (Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
115167
}

src/libcore/tests/nonzero.rs

+35
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,38 @@ fn test_from_str() {
141141
Some(IntErrorKind::Overflow)
142142
);
143143
}
144+
145+
#[test]
146+
fn test_nonzero_bitor() {
147+
let nz_alt = NonZeroU8::new(0b1010_1010).unwrap();
148+
let nz_low = NonZeroU8::new(0b0000_1111).unwrap();
149+
150+
let both_nz: NonZeroU8 = nz_alt | nz_low;
151+
assert_eq!(both_nz.get(), 0b1010_1111);
152+
153+
let rhs_int: NonZeroU8 = nz_low | 0b1100_0000u8;
154+
assert_eq!(rhs_int.get(), 0b1100_1111);
155+
156+
let rhs_zero: NonZeroU8 = nz_alt | 0u8;
157+
assert_eq!(rhs_zero.get(), 0b1010_1010);
158+
159+
let lhs_int: NonZeroU8 = 0b0110_0110u8 | nz_alt;
160+
assert_eq!(lhs_int.get(), 0b1110_1110);
161+
162+
let lhs_zero: NonZeroU8 = 0u8 | nz_low;
163+
assert_eq!(lhs_zero.get(), 0b0000_1111);
164+
}
165+
166+
#[test]
167+
fn test_nonzero_bitor_assign() {
168+
let mut target = NonZeroU8::new(0b1010_1010).unwrap();
169+
170+
target |= NonZeroU8::new(0b0000_1111).unwrap();
171+
assert_eq!(target.get(), 0b1010_1111);
172+
173+
target |= 0b0001_0000;
174+
assert_eq!(target.get(), 0b1011_1111);
175+
176+
target |= 0;
177+
assert_eq!(target.get(), 0b1011_1111);
178+
}

0 commit comments

Comments
 (0)