core/ops/bit.rs
1/// The unary negation operator `!`. It performs either logical or bitwise negation,
2/// depending on the type.
3///
4/// # Examples
5///
6/// An implementation of `Not` for `Answer`, which enables the use of `!` to
7/// invert its value.
8///
9/// ```
10/// use std::ops::Not;
11///
12/// #[derive(Debug, PartialEq)]
13/// enum Answer {
14/// Yes,
15/// No,
16/// }
17///
18/// impl Not for Answer {
19/// type Output = Self;
20///
21/// fn not(self) -> Self::Output {
22/// match self {
23/// Answer::Yes => Answer::No,
24/// Answer::No => Answer::Yes
25/// }
26/// }
27/// }
28///
29/// assert_eq!(!Answer::Yes, Answer::No);
30/// assert_eq!(!Answer::No, Answer::Yes);
31/// ```
32#[lang = "not"]
33#[stable(feature = "rust1", since = "1.0.0")]
34#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
35#[doc(alias = "!")]
36pub const trait Not {
37 /// The resulting type after applying the `!` operator.
38 #[stable(feature = "rust1", since = "1.0.0")]
39 type Output;
40
41 /// Performs the unary `!` operation using logical or bitwise negation.
42 ///
43 /// # Examples
44 ///
45 /// ```
46 /// // Logical negation changes between true and false:
47 /// assert_eq!(!true, false);
48 /// assert_eq!(!false, true);
49 ///
50 /// // Bitwise negation changes between 0 and 1 in the binary
51 /// // representation of integers:
52 /// assert_eq!(!1u8, 254);
53 /// assert_eq!(!0u8, 255);
54 ///
55 /// // The same examples, represented in binary:
56 /// assert_eq!(!0b0000_0001u8, 0b1111_1110);
57 /// assert_eq!(!0b0000_0000u8, 0b1111_1111);
58 /// ```
59 #[must_use]
60 #[stable(feature = "rust1", since = "1.0.0")]
61 fn not(self) -> Self::Output;
62}
63
64macro_rules! not_impl {
65 ($($t:ty)*) => ($(
66 #[stable(feature = "rust1", since = "1.0.0")]
67 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
68 const impl Not for $t {
69 type Output = $t;
70
71 #[inline]
72 #[ferrocene::prevalidated]
73 fn not(self) -> $t { !self }
74 }
75
76 forward_ref_unop! { impl Not, not for $t,
77 #[stable(feature = "rust1", since = "1.0.0")]
78 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
79 )*)
80}
81
82not_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
83
84#[stable(feature = "not_never", since = "1.60.0")]
85#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
86const impl Not for ! {
87 type Output = !;
88
89 #[inline]
90 fn not(self) -> ! {
91 match self {}
92 }
93}
94
95/// The bitwise AND operator `&`.
96///
97/// Note that `Rhs` is `Self` by default, but this is not mandatory.
98///
99/// # Examples
100///
101/// An implementation of `BitAnd` for a wrapper around `bool`.
102///
103/// ```
104/// use std::ops::BitAnd;
105///
106/// #[derive(Debug, PartialEq)]
107/// struct Scalar(bool);
108///
109/// impl BitAnd for Scalar {
110/// type Output = Self;
111///
112/// // rhs is the "right-hand side" of the expression `a & b`
113/// fn bitand(self, rhs: Self) -> Self::Output {
114/// Self(self.0 & rhs.0)
115/// }
116/// }
117///
118/// assert_eq!(Scalar(true) & Scalar(true), Scalar(true));
119/// assert_eq!(Scalar(true) & Scalar(false), Scalar(false));
120/// assert_eq!(Scalar(false) & Scalar(true), Scalar(false));
121/// assert_eq!(Scalar(false) & Scalar(false), Scalar(false));
122/// ```
123///
124/// An implementation of `BitAnd` for a wrapper around `Vec<bool>`.
125///
126/// ```
127/// use std::ops::BitAnd;
128///
129/// #[derive(Debug, PartialEq)]
130/// struct BooleanVector(Vec<bool>);
131///
132/// impl BitAnd for BooleanVector {
133/// type Output = Self;
134///
135/// fn bitand(self, Self(rhs): Self) -> Self::Output {
136/// let Self(lhs) = self;
137/// assert_eq!(lhs.len(), rhs.len());
138/// Self(
139/// lhs.iter()
140/// .zip(rhs.iter())
141/// .map(|(x, y)| *x & *y)
142/// .collect()
143/// )
144/// }
145/// }
146///
147/// let bv1 = BooleanVector(vec![true, true, false, false]);
148/// let bv2 = BooleanVector(vec![true, false, true, false]);
149/// let expected = BooleanVector(vec![true, false, false, false]);
150/// assert_eq!(bv1 & bv2, expected);
151/// ```
152#[lang = "bitand"]
153#[doc(alias = "&")]
154#[stable(feature = "rust1", since = "1.0.0")]
155#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
156#[diagnostic::on_unimplemented(
157 message = "no implementation for `{Self} & {Rhs}`",
158 label = "no implementation for `{Self} & {Rhs}`"
159)]
160pub const trait BitAnd<Rhs = Self> {
161 /// The resulting type after applying the `&` operator.
162 #[stable(feature = "rust1", since = "1.0.0")]
163 type Output;
164
165 /// Performs the `&` operation.
166 ///
167 /// # Examples
168 ///
169 /// ```
170 /// assert_eq!(true & false, false);
171 /// assert_eq!(true & true, true);
172 /// assert_eq!(5u8 & 1u8, 1);
173 /// assert_eq!(5u8 & 2u8, 0);
174 /// ```
175 #[must_use]
176 #[stable(feature = "rust1", since = "1.0.0")]
177 fn bitand(self, rhs: Rhs) -> Self::Output;
178}
179
180macro_rules! bitand_impl {
181 ($($t:ty)*) => ($(
182 #[stable(feature = "rust1", since = "1.0.0")]
183 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
184 const impl BitAnd for $t {
185 type Output = $t;
186
187 #[inline]
188 #[ferrocene::prevalidated]
189 fn bitand(self, rhs: $t) -> $t { self & rhs }
190 }
191
192 forward_ref_binop! { impl BitAnd, bitand for $t, $t,
193 #[stable(feature = "rust1", since = "1.0.0")]
194 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
195 )*)
196}
197
198bitand_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
199
200/// The bitwise OR operator `|`.
201///
202/// Note that `Rhs` is `Self` by default, but this is not mandatory.
203///
204/// # Examples
205///
206/// An implementation of `BitOr` for a wrapper around `bool`.
207///
208/// ```
209/// use std::ops::BitOr;
210///
211/// #[derive(Debug, PartialEq)]
212/// struct Scalar(bool);
213///
214/// impl BitOr for Scalar {
215/// type Output = Self;
216///
217/// // rhs is the "right-hand side" of the expression `a | b`
218/// fn bitor(self, rhs: Self) -> Self::Output {
219/// Self(self.0 | rhs.0)
220/// }
221/// }
222///
223/// assert_eq!(Scalar(true) | Scalar(true), Scalar(true));
224/// assert_eq!(Scalar(true) | Scalar(false), Scalar(true));
225/// assert_eq!(Scalar(false) | Scalar(true), Scalar(true));
226/// assert_eq!(Scalar(false) | Scalar(false), Scalar(false));
227/// ```
228///
229/// An implementation of `BitOr` for a wrapper around `Vec<bool>`.
230///
231/// ```
232/// use std::ops::BitOr;
233///
234/// #[derive(Debug, PartialEq)]
235/// struct BooleanVector(Vec<bool>);
236///
237/// impl BitOr for BooleanVector {
238/// type Output = Self;
239///
240/// fn bitor(self, Self(rhs): Self) -> Self::Output {
241/// let Self(lhs) = self;
242/// assert_eq!(lhs.len(), rhs.len());
243/// Self(
244/// lhs.iter()
245/// .zip(rhs.iter())
246/// .map(|(x, y)| *x | *y)
247/// .collect()
248/// )
249/// }
250/// }
251///
252/// let bv1 = BooleanVector(vec![true, true, false, false]);
253/// let bv2 = BooleanVector(vec![true, false, true, false]);
254/// let expected = BooleanVector(vec![true, true, true, false]);
255/// assert_eq!(bv1 | bv2, expected);
256/// ```
257#[lang = "bitor"]
258#[doc(alias = "|")]
259#[stable(feature = "rust1", since = "1.0.0")]
260#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
261#[diagnostic::on_unimplemented(
262 message = "no implementation for `{Self} | {Rhs}`",
263 label = "no implementation for `{Self} | {Rhs}`"
264)]
265pub const trait BitOr<Rhs = Self> {
266 /// The resulting type after applying the `|` operator.
267 #[stable(feature = "rust1", since = "1.0.0")]
268 type Output;
269
270 /// Performs the `|` operation.
271 ///
272 /// # Examples
273 ///
274 /// ```
275 /// assert_eq!(true | false, true);
276 /// assert_eq!(false | false, false);
277 /// assert_eq!(5u8 | 1u8, 5);
278 /// assert_eq!(5u8 | 2u8, 7);
279 /// ```
280 #[must_use]
281 #[stable(feature = "rust1", since = "1.0.0")]
282 fn bitor(self, rhs: Rhs) -> Self::Output;
283}
284
285macro_rules! bitor_impl {
286 ($($t:ty)*) => ($(
287 #[stable(feature = "rust1", since = "1.0.0")]
288 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
289 const impl BitOr for $t {
290 type Output = $t;
291
292 #[inline]
293 #[ferrocene::prevalidated]
294 fn bitor(self, rhs: $t) -> $t { self | rhs }
295 }
296
297 forward_ref_binop! { impl BitOr, bitor for $t, $t,
298 #[stable(feature = "rust1", since = "1.0.0")]
299 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
300 )*)
301}
302
303bitor_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
304
305/// The bitwise XOR operator `^`.
306///
307/// Note that `Rhs` is `Self` by default, but this is not mandatory.
308///
309/// # Examples
310///
311/// An implementation of `BitXor` that lifts `^` to a wrapper around `bool`.
312///
313/// ```
314/// use std::ops::BitXor;
315///
316/// #[derive(Debug, PartialEq)]
317/// struct Scalar(bool);
318///
319/// impl BitXor for Scalar {
320/// type Output = Self;
321///
322/// // rhs is the "right-hand side" of the expression `a ^ b`
323/// fn bitxor(self, rhs: Self) -> Self::Output {
324/// Self(self.0 ^ rhs.0)
325/// }
326/// }
327///
328/// assert_eq!(Scalar(true) ^ Scalar(true), Scalar(false));
329/// assert_eq!(Scalar(true) ^ Scalar(false), Scalar(true));
330/// assert_eq!(Scalar(false) ^ Scalar(true), Scalar(true));
331/// assert_eq!(Scalar(false) ^ Scalar(false), Scalar(false));
332/// ```
333///
334/// An implementation of `BitXor` trait for a wrapper around `Vec<bool>`.
335///
336/// ```
337/// use std::ops::BitXor;
338///
339/// #[derive(Debug, PartialEq)]
340/// struct BooleanVector(Vec<bool>);
341///
342/// impl BitXor for BooleanVector {
343/// type Output = Self;
344///
345/// fn bitxor(self, Self(rhs): Self) -> Self::Output {
346/// let Self(lhs) = self;
347/// assert_eq!(lhs.len(), rhs.len());
348/// Self(
349/// lhs.iter()
350/// .zip(rhs.iter())
351/// .map(|(x, y)| *x ^ *y)
352/// .collect()
353/// )
354/// }
355/// }
356///
357/// let bv1 = BooleanVector(vec![true, true, false, false]);
358/// let bv2 = BooleanVector(vec![true, false, true, false]);
359/// let expected = BooleanVector(vec![false, true, true, false]);
360/// assert_eq!(bv1 ^ bv2, expected);
361/// ```
362#[lang = "bitxor"]
363#[doc(alias = "^")]
364#[stable(feature = "rust1", since = "1.0.0")]
365#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
366#[diagnostic::on_unimplemented(
367 message = "no implementation for `{Self} ^ {Rhs}`",
368 label = "no implementation for `{Self} ^ {Rhs}`"
369)]
370pub const trait BitXor<Rhs = Self> {
371 /// The resulting type after applying the `^` operator.
372 #[stable(feature = "rust1", since = "1.0.0")]
373 type Output;
374
375 /// Performs the `^` operation.
376 ///
377 /// # Examples
378 ///
379 /// ```
380 /// assert_eq!(true ^ false, true);
381 /// assert_eq!(true ^ true, false);
382 /// assert_eq!(5u8 ^ 1u8, 4);
383 /// assert_eq!(5u8 ^ 2u8, 7);
384 /// ```
385 #[must_use]
386 #[stable(feature = "rust1", since = "1.0.0")]
387 fn bitxor(self, rhs: Rhs) -> Self::Output;
388}
389
390macro_rules! bitxor_impl {
391 ($($t:ty)*) => ($(
392 #[stable(feature = "rust1", since = "1.0.0")]
393 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
394 const impl BitXor for $t {
395 type Output = $t;
396
397 #[inline]
398 #[ferrocene::prevalidated]
399 fn bitxor(self, other: $t) -> $t { self ^ other }
400 }
401
402 forward_ref_binop! { impl BitXor, bitxor for $t, $t,
403 #[stable(feature = "rust1", since = "1.0.0")]
404 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
405 )*)
406}
407
408bitxor_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
409
410/// The left shift operator `<<`. Note that because this trait is implemented
411/// for all integer types with multiple right-hand-side types, Rust's type
412/// checker has special handling for `_ << _`, setting the result type for
413/// integer operations to the type of the left-hand-side operand. This means
414/// that though `a << b` and `a.shl(b)` are one and the same from an evaluation
415/// standpoint, they are different when it comes to type inference.
416///
417/// # Examples
418///
419/// An implementation of `Shl` that lifts the `<<` operation on integers to a
420/// wrapper around `usize`.
421///
422/// ```
423/// use std::ops::Shl;
424///
425/// #[derive(PartialEq, Debug)]
426/// struct Scalar(usize);
427///
428/// impl Shl<Scalar> for Scalar {
429/// type Output = Self;
430///
431/// fn shl(self, Self(rhs): Self) -> Self::Output {
432/// let Self(lhs) = self;
433/// Self(lhs << rhs)
434/// }
435/// }
436///
437/// assert_eq!(Scalar(4) << Scalar(2), Scalar(16));
438/// ```
439///
440/// An implementation of `Shl` that spins a vector leftward by a given amount.
441///
442/// ```
443/// use std::ops::Shl;
444///
445/// #[derive(PartialEq, Debug)]
446/// struct SpinVector<T: Clone> {
447/// vec: Vec<T>,
448/// }
449///
450/// impl<T: Clone> Shl<usize> for SpinVector<T> {
451/// type Output = Self;
452///
453/// fn shl(self, rhs: usize) -> Self::Output {
454/// // Rotate the vector by `rhs` places.
455/// let (a, b) = self.vec.split_at(rhs);
456/// let mut spun_vector = vec![];
457/// spun_vector.extend_from_slice(b);
458/// spun_vector.extend_from_slice(a);
459/// Self { vec: spun_vector }
460/// }
461/// }
462///
463/// assert_eq!(SpinVector { vec: vec![0, 1, 2, 3, 4] } << 2,
464/// SpinVector { vec: vec![2, 3, 4, 0, 1] });
465/// ```
466#[lang = "shl"]
467#[doc(alias = "<<")]
468#[stable(feature = "rust1", since = "1.0.0")]
469#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
470#[diagnostic::on_unimplemented(
471 message = "no implementation for `{Self} << {Rhs}`",
472 label = "no implementation for `{Self} << {Rhs}`"
473)]
474pub const trait Shl<Rhs = Self> {
475 /// The resulting type after applying the `<<` operator.
476 #[stable(feature = "rust1", since = "1.0.0")]
477 type Output;
478
479 /// Performs the `<<` operation.
480 ///
481 /// # Examples
482 ///
483 /// ```
484 /// assert_eq!(5u8 << 1, 10);
485 /// assert_eq!(1u8 << 1, 2);
486 /// ```
487 #[must_use]
488 #[stable(feature = "rust1", since = "1.0.0")]
489 fn shl(self, rhs: Rhs) -> Self::Output;
490}
491
492macro_rules! shl_impl {
493 ($t:ty, $f:ty) => {
494 #[stable(feature = "rust1", since = "1.0.0")]
495 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
496 const impl Shl<$f> for $t {
497 type Output = $t;
498
499 #[inline]
500 #[track_caller]
501 #[rustc_inherit_overflow_checks]
502 #[ferrocene::prevalidated]
503 fn shl(self, other: $f) -> $t {
504 self << other
505 }
506 }
507
508 forward_ref_binop! { impl Shl, shl for $t, $f,
509 #[stable(feature = "rust1", since = "1.0.0")]
510 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
511 };
512}
513
514macro_rules! shl_impl_all {
515 ($($t:ty)*) => ($(
516 shl_impl! { $t, u8 }
517 shl_impl! { $t, u16 }
518 shl_impl! { $t, u32 }
519 shl_impl! { $t, u64 }
520 shl_impl! { $t, u128 }
521 shl_impl! { $t, usize }
522
523 shl_impl! { $t, i8 }
524 shl_impl! { $t, i16 }
525 shl_impl! { $t, i32 }
526 shl_impl! { $t, i64 }
527 shl_impl! { $t, i128 }
528 shl_impl! { $t, isize }
529 )*)
530}
531
532shl_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
533
534/// The right shift operator `>>`. Note that because this trait is implemented
535/// for all integer types with multiple right-hand-side types, Rust's type
536/// checker has special handling for `_ >> _`, setting the result type for
537/// integer operations to the type of the left-hand-side operand. This means
538/// that though `a >> b` and `a.shr(b)` are one and the same from an evaluation
539/// standpoint, they are different when it comes to type inference.
540///
541/// # Examples
542///
543/// An implementation of `Shr` that lifts the `>>` operation on integers to a
544/// wrapper around `usize`.
545///
546/// ```
547/// use std::ops::Shr;
548///
549/// #[derive(PartialEq, Debug)]
550/// struct Scalar(usize);
551///
552/// impl Shr<Scalar> for Scalar {
553/// type Output = Self;
554///
555/// fn shr(self, Self(rhs): Self) -> Self::Output {
556/// let Self(lhs) = self;
557/// Self(lhs >> rhs)
558/// }
559/// }
560///
561/// assert_eq!(Scalar(16) >> Scalar(2), Scalar(4));
562/// ```
563///
564/// An implementation of `Shr` that spins a vector rightward by a given amount.
565///
566/// ```
567/// use std::ops::Shr;
568///
569/// #[derive(PartialEq, Debug)]
570/// struct SpinVector<T: Clone> {
571/// vec: Vec<T>,
572/// }
573///
574/// impl<T: Clone> Shr<usize> for SpinVector<T> {
575/// type Output = Self;
576///
577/// fn shr(self, rhs: usize) -> Self::Output {
578/// // Rotate the vector by `rhs` places.
579/// let (a, b) = self.vec.split_at(self.vec.len() - rhs);
580/// let mut spun_vector = vec![];
581/// spun_vector.extend_from_slice(b);
582/// spun_vector.extend_from_slice(a);
583/// Self { vec: spun_vector }
584/// }
585/// }
586///
587/// assert_eq!(SpinVector { vec: vec![0, 1, 2, 3, 4] } >> 2,
588/// SpinVector { vec: vec![3, 4, 0, 1, 2] });
589/// ```
590#[lang = "shr"]
591#[doc(alias = ">>")]
592#[stable(feature = "rust1", since = "1.0.0")]
593#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
594#[diagnostic::on_unimplemented(
595 message = "no implementation for `{Self} >> {Rhs}`",
596 label = "no implementation for `{Self} >> {Rhs}`"
597)]
598pub const trait Shr<Rhs = Self> {
599 /// The resulting type after applying the `>>` operator.
600 #[stable(feature = "rust1", since = "1.0.0")]
601 type Output;
602
603 /// Performs the `>>` operation.
604 ///
605 /// # Examples
606 ///
607 /// ```
608 /// assert_eq!(5u8 >> 1, 2);
609 /// assert_eq!(2u8 >> 1, 1);
610 /// ```
611 #[must_use]
612 #[stable(feature = "rust1", since = "1.0.0")]
613 fn shr(self, rhs: Rhs) -> Self::Output;
614}
615
616macro_rules! shr_impl {
617 ($t:ty, $f:ty) => {
618 #[stable(feature = "rust1", since = "1.0.0")]
619 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
620 const impl Shr<$f> for $t {
621 type Output = $t;
622
623 #[inline]
624 #[track_caller]
625 #[rustc_inherit_overflow_checks]
626 #[ferrocene::prevalidated]
627 fn shr(self, other: $f) -> $t {
628 self >> other
629 }
630 }
631
632 forward_ref_binop! { impl Shr, shr for $t, $f,
633 #[stable(feature = "rust1", since = "1.0.0")]
634 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
635 };
636}
637
638macro_rules! shr_impl_all {
639 ($($t:ty)*) => ($(
640 shr_impl! { $t, u8 }
641 shr_impl! { $t, u16 }
642 shr_impl! { $t, u32 }
643 shr_impl! { $t, u64 }
644 shr_impl! { $t, u128 }
645 shr_impl! { $t, usize }
646
647 shr_impl! { $t, i8 }
648 shr_impl! { $t, i16 }
649 shr_impl! { $t, i32 }
650 shr_impl! { $t, i64 }
651 shr_impl! { $t, i128 }
652 shr_impl! { $t, isize }
653 )*)
654}
655
656shr_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
657
658/// The bitwise AND assignment operator `&=`.
659///
660/// # Examples
661///
662/// An implementation of `BitAndAssign` that lifts the `&=` operator to a
663/// wrapper around `bool`.
664///
665/// ```
666/// use std::ops::BitAndAssign;
667///
668/// #[derive(Debug, PartialEq)]
669/// struct Scalar(bool);
670///
671/// impl BitAndAssign for Scalar {
672/// // rhs is the "right-hand side" of the expression `a &= b`
673/// fn bitand_assign(&mut self, rhs: Self) {
674/// *self = Self(self.0 & rhs.0)
675/// }
676/// }
677///
678/// let mut scalar = Scalar(true);
679/// scalar &= Scalar(true);
680/// assert_eq!(scalar, Scalar(true));
681///
682/// let mut scalar = Scalar(true);
683/// scalar &= Scalar(false);
684/// assert_eq!(scalar, Scalar(false));
685///
686/// let mut scalar = Scalar(false);
687/// scalar &= Scalar(true);
688/// assert_eq!(scalar, Scalar(false));
689///
690/// let mut scalar = Scalar(false);
691/// scalar &= Scalar(false);
692/// assert_eq!(scalar, Scalar(false));
693/// ```
694///
695/// Here, the `BitAndAssign` trait is implemented for a wrapper around
696/// `Vec<bool>`.
697///
698/// ```
699/// use std::ops::BitAndAssign;
700///
701/// #[derive(Debug, PartialEq)]
702/// struct BooleanVector(Vec<bool>);
703///
704/// impl BitAndAssign for BooleanVector {
705/// // `rhs` is the "right-hand side" of the expression `a &= b`.
706/// fn bitand_assign(&mut self, rhs: Self) {
707/// assert_eq!(self.0.len(), rhs.0.len());
708/// *self = Self(
709/// self.0
710/// .iter()
711/// .zip(rhs.0.iter())
712/// .map(|(x, y)| *x & *y)
713/// .collect()
714/// );
715/// }
716/// }
717///
718/// let mut bv = BooleanVector(vec![true, true, false, false]);
719/// bv &= BooleanVector(vec![true, false, true, false]);
720/// let expected = BooleanVector(vec![true, false, false, false]);
721/// assert_eq!(bv, expected);
722/// ```
723#[lang = "bitand_assign"]
724#[doc(alias = "&=")]
725#[stable(feature = "op_assign_traits", since = "1.8.0")]
726#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
727#[diagnostic::on_unimplemented(
728 message = "no implementation for `{Self} &= {Rhs}`",
729 label = "no implementation for `{Self} &= {Rhs}`"
730)]
731pub const trait BitAndAssign<Rhs = Self> {
732 /// Performs the `&=` operation.
733 ///
734 /// # Examples
735 ///
736 /// ```
737 /// let mut x = true;
738 /// x &= false;
739 /// assert_eq!(x, false);
740 ///
741 /// let mut x = true;
742 /// x &= true;
743 /// assert_eq!(x, true);
744 ///
745 /// let mut x: u8 = 5;
746 /// x &= 1;
747 /// assert_eq!(x, 1);
748 ///
749 /// let mut x: u8 = 5;
750 /// x &= 2;
751 /// assert_eq!(x, 0);
752 /// ```
753 #[stable(feature = "op_assign_traits", since = "1.8.0")]
754 fn bitand_assign(&mut self, rhs: Rhs);
755}
756
757macro_rules! bitand_assign_impl {
758 ($($t:ty)+) => ($(
759 #[stable(feature = "op_assign_traits", since = "1.8.0")]
760 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
761 const impl BitAndAssign for $t {
762 #[inline]
763 #[ferrocene::prevalidated]
764 fn bitand_assign(&mut self, other: $t) { *self &= other }
765 }
766
767 forward_ref_op_assign! { impl BitAndAssign, bitand_assign for $t, $t,
768 #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
769 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
770 )+)
771}
772
773bitand_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
774
775/// The bitwise OR assignment operator `|=`.
776///
777/// # Examples
778///
779/// ```
780/// use std::ops::BitOrAssign;
781///
782/// #[derive(Debug, PartialEq)]
783/// struct PersonalPreferences {
784/// likes_cats: bool,
785/// likes_dogs: bool,
786/// }
787///
788/// impl BitOrAssign for PersonalPreferences {
789/// fn bitor_assign(&mut self, rhs: Self) {
790/// self.likes_cats |= rhs.likes_cats;
791/// self.likes_dogs |= rhs.likes_dogs;
792/// }
793/// }
794///
795/// let mut prefs = PersonalPreferences { likes_cats: true, likes_dogs: false };
796/// prefs |= PersonalPreferences { likes_cats: false, likes_dogs: true };
797/// assert_eq!(prefs, PersonalPreferences { likes_cats: true, likes_dogs: true });
798/// ```
799#[lang = "bitor_assign"]
800#[doc(alias = "|=")]
801#[stable(feature = "op_assign_traits", since = "1.8.0")]
802#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
803#[diagnostic::on_unimplemented(
804 message = "no implementation for `{Self} |= {Rhs}`",
805 label = "no implementation for `{Self} |= {Rhs}`"
806)]
807pub const trait BitOrAssign<Rhs = Self> {
808 /// Performs the `|=` operation.
809 ///
810 /// # Examples
811 ///
812 /// ```
813 /// let mut x = true;
814 /// x |= false;
815 /// assert_eq!(x, true);
816 ///
817 /// let mut x = false;
818 /// x |= false;
819 /// assert_eq!(x, false);
820 ///
821 /// let mut x: u8 = 5;
822 /// x |= 1;
823 /// assert_eq!(x, 5);
824 ///
825 /// let mut x: u8 = 5;
826 /// x |= 2;
827 /// assert_eq!(x, 7);
828 /// ```
829 #[stable(feature = "op_assign_traits", since = "1.8.0")]
830 fn bitor_assign(&mut self, rhs: Rhs);
831}
832
833macro_rules! bitor_assign_impl {
834 ($($t:ty)+) => ($(
835 #[stable(feature = "op_assign_traits", since = "1.8.0")]
836 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
837 const impl BitOrAssign for $t {
838 #[inline]
839 #[ferrocene::prevalidated]
840 fn bitor_assign(&mut self, other: $t) { *self |= other }
841 }
842
843 forward_ref_op_assign! { impl BitOrAssign, bitor_assign for $t, $t,
844 #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
845 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
846 )+)
847}
848
849bitor_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
850
851/// The bitwise XOR assignment operator `^=`.
852///
853/// # Examples
854///
855/// ```
856/// use std::ops::BitXorAssign;
857///
858/// #[derive(Debug, PartialEq)]
859/// struct Personality {
860/// has_soul: bool,
861/// likes_knitting: bool,
862/// }
863///
864/// impl BitXorAssign for Personality {
865/// fn bitxor_assign(&mut self, rhs: Self) {
866/// self.has_soul ^= rhs.has_soul;
867/// self.likes_knitting ^= rhs.likes_knitting;
868/// }
869/// }
870///
871/// let mut personality = Personality { has_soul: false, likes_knitting: true };
872/// personality ^= Personality { has_soul: true, likes_knitting: true };
873/// assert_eq!(personality, Personality { has_soul: true, likes_knitting: false});
874/// ```
875#[lang = "bitxor_assign"]
876#[doc(alias = "^=")]
877#[stable(feature = "op_assign_traits", since = "1.8.0")]
878#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
879#[diagnostic::on_unimplemented(
880 message = "no implementation for `{Self} ^= {Rhs}`",
881 label = "no implementation for `{Self} ^= {Rhs}`"
882)]
883pub const trait BitXorAssign<Rhs = Self> {
884 /// Performs the `^=` operation.
885 ///
886 /// # Examples
887 ///
888 /// ```
889 /// let mut x = true;
890 /// x ^= false;
891 /// assert_eq!(x, true);
892 ///
893 /// let mut x = true;
894 /// x ^= true;
895 /// assert_eq!(x, false);
896 ///
897 /// let mut x: u8 = 5;
898 /// x ^= 1;
899 /// assert_eq!(x, 4);
900 ///
901 /// let mut x: u8 = 5;
902 /// x ^= 2;
903 /// assert_eq!(x, 7);
904 /// ```
905 #[stable(feature = "op_assign_traits", since = "1.8.0")]
906 fn bitxor_assign(&mut self, rhs: Rhs);
907}
908
909macro_rules! bitxor_assign_impl {
910 ($($t:ty)+) => ($(
911 #[stable(feature = "op_assign_traits", since = "1.8.0")]
912 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
913 const impl BitXorAssign for $t {
914 #[inline]
915 #[ferrocene::prevalidated]
916 fn bitxor_assign(&mut self, other: $t) { *self ^= other }
917 }
918
919 forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for $t, $t,
920 #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
921 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
922 )+)
923}
924
925bitxor_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
926
927/// The left shift assignment operator `<<=`.
928///
929/// # Examples
930///
931/// An implementation of `ShlAssign` for a wrapper around `usize`.
932///
933/// ```
934/// use std::ops::ShlAssign;
935///
936/// #[derive(Debug, PartialEq)]
937/// struct Scalar(usize);
938///
939/// impl ShlAssign<usize> for Scalar {
940/// fn shl_assign(&mut self, rhs: usize) {
941/// self.0 <<= rhs;
942/// }
943/// }
944///
945/// let mut scalar = Scalar(4);
946/// scalar <<= 2;
947/// assert_eq!(scalar, Scalar(16));
948/// ```
949#[lang = "shl_assign"]
950#[doc(alias = "<<=")]
951#[stable(feature = "op_assign_traits", since = "1.8.0")]
952#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
953#[diagnostic::on_unimplemented(
954 message = "no implementation for `{Self} <<= {Rhs}`",
955 label = "no implementation for `{Self} <<= {Rhs}`"
956)]
957pub const trait ShlAssign<Rhs = Self> {
958 /// Performs the `<<=` operation.
959 ///
960 /// # Examples
961 ///
962 /// ```
963 /// let mut x: u8 = 5;
964 /// x <<= 1;
965 /// assert_eq!(x, 10);
966 ///
967 /// let mut x: u8 = 1;
968 /// x <<= 1;
969 /// assert_eq!(x, 2);
970 /// ```
971 #[stable(feature = "op_assign_traits", since = "1.8.0")]
972 fn shl_assign(&mut self, rhs: Rhs);
973}
974
975macro_rules! shl_assign_impl {
976 ($t:ty, $f:ty) => {
977 #[stable(feature = "op_assign_traits", since = "1.8.0")]
978 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
979 const impl ShlAssign<$f> for $t {
980 #[inline]
981 #[track_caller]
982 #[rustc_inherit_overflow_checks]
983 #[ferrocene::prevalidated]
984 fn shl_assign(&mut self, other: $f) {
985 *self <<= other
986 }
987 }
988
989 forward_ref_op_assign! { impl ShlAssign, shl_assign for $t, $f,
990 #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
991 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
992 };
993}
994
995macro_rules! shl_assign_impl_all {
996 ($($t:ty)*) => ($(
997 shl_assign_impl! { $t, u8 }
998 shl_assign_impl! { $t, u16 }
999 shl_assign_impl! { $t, u32 }
1000 shl_assign_impl! { $t, u64 }
1001 shl_assign_impl! { $t, u128 }
1002 shl_assign_impl! { $t, usize }
1003
1004 shl_assign_impl! { $t, i8 }
1005 shl_assign_impl! { $t, i16 }
1006 shl_assign_impl! { $t, i32 }
1007 shl_assign_impl! { $t, i64 }
1008 shl_assign_impl! { $t, i128 }
1009 shl_assign_impl! { $t, isize }
1010 )*)
1011}
1012
1013shl_assign_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
1014
1015/// The right shift assignment operator `>>=`.
1016///
1017/// # Examples
1018///
1019/// An implementation of `ShrAssign` for a wrapper around `usize`.
1020///
1021/// ```
1022/// use std::ops::ShrAssign;
1023///
1024/// #[derive(Debug, PartialEq)]
1025/// struct Scalar(usize);
1026///
1027/// impl ShrAssign<usize> for Scalar {
1028/// fn shr_assign(&mut self, rhs: usize) {
1029/// self.0 >>= rhs;
1030/// }
1031/// }
1032///
1033/// let mut scalar = Scalar(16);
1034/// scalar >>= 2;
1035/// assert_eq!(scalar, Scalar(4));
1036/// ```
1037#[lang = "shr_assign"]
1038#[doc(alias = ">>=")]
1039#[stable(feature = "op_assign_traits", since = "1.8.0")]
1040#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1041#[diagnostic::on_unimplemented(
1042 message = "no implementation for `{Self} >>= {Rhs}`",
1043 label = "no implementation for `{Self} >>= {Rhs}`"
1044)]
1045pub const trait ShrAssign<Rhs = Self> {
1046 /// Performs the `>>=` operation.
1047 ///
1048 /// # Examples
1049 ///
1050 /// ```
1051 /// let mut x: u8 = 5;
1052 /// x >>= 1;
1053 /// assert_eq!(x, 2);
1054 ///
1055 /// let mut x: u8 = 2;
1056 /// x >>= 1;
1057 /// assert_eq!(x, 1);
1058 /// ```
1059 #[stable(feature = "op_assign_traits", since = "1.8.0")]
1060 fn shr_assign(&mut self, rhs: Rhs);
1061}
1062
1063macro_rules! shr_assign_impl {
1064 ($t:ty, $f:ty) => {
1065 #[stable(feature = "op_assign_traits", since = "1.8.0")]
1066 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
1067 const impl ShrAssign<$f> for $t {
1068 #[inline]
1069 #[track_caller]
1070 #[rustc_inherit_overflow_checks]
1071 #[ferrocene::prevalidated]
1072 fn shr_assign(&mut self, other: $f) {
1073 *self >>= other
1074 }
1075 }
1076
1077 forward_ref_op_assign! { impl ShrAssign, shr_assign for $t, $f,
1078 #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]
1079 #[rustc_const_unstable(feature = "const_ops", issue = "143802")] }
1080 };
1081}
1082
1083macro_rules! shr_assign_impl_all {
1084 ($($t:ty)*) => ($(
1085 shr_assign_impl! { $t, u8 }
1086 shr_assign_impl! { $t, u16 }
1087 shr_assign_impl! { $t, u32 }
1088 shr_assign_impl! { $t, u64 }
1089 shr_assign_impl! { $t, u128 }
1090 shr_assign_impl! { $t, usize }
1091
1092 shr_assign_impl! { $t, i8 }
1093 shr_assign_impl! { $t, i16 }
1094 shr_assign_impl! { $t, i32 }
1095 shr_assign_impl! { $t, i64 }
1096 shr_assign_impl! { $t, i128 }
1097 shr_assign_impl! { $t, isize }
1098 )*)
1099}
1100
1101shr_assign_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }