Skip to main content

core/ops/
bit.rs

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