1 /// The unary logical negation operator `!`.
5 /// An implementation of `Not` for `Answer`, which enables the use of `!` to
11 /// #[derive(Debug, PartialEq)]
17 /// impl Not for Answer {
18 /// type Output = Self;
20 /// fn not(self) -> Self::Output {
22 /// Answer::Yes => Answer::No,
23 /// Answer::No => Answer::Yes
28 /// assert_eq!(!Answer::Yes, Answer::No);
29 /// assert_eq!(!Answer::No, Answer::Yes);
32 #[stable(feature = "rust1", since = "1.0.0")]
35 /// The resulting type after applying the `!` operator.
36 #[stable(feature = "rust1", since = "1.0.0")]
39 /// Performs the unary `!` operation.
44 /// assert_eq!(!true, false);
45 /// assert_eq!(!false, true);
46 /// assert_eq!(!1u8, 254);
47 /// assert_eq!(!0u8, 255);
50 #[stable(feature = "rust1", since = "1.0.0")]
51 fn not(self) -> Self::Output;
54 macro_rules! not_impl {
56 #[stable(feature = "rust1", since = "1.0.0")]
57 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
58 impl const Not for $t {
62 fn not(self) -> $t { !self }
65 forward_ref_unop! { impl const Not, not for $t }
69 not_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
71 #[stable(feature = "not_never", since = "1.60.0")]
72 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
73 impl const Not for ! {
82 /// The bitwise AND operator `&`.
84 /// Note that `Rhs` is `Self` by default, but this is not mandatory.
88 /// An implementation of `BitAnd` for a wrapper around `bool`.
91 /// use std::ops::BitAnd;
93 /// #[derive(Debug, PartialEq)]
94 /// struct Scalar(bool);
96 /// impl BitAnd for Scalar {
97 /// type Output = Self;
99 /// // rhs is the "right-hand side" of the expression `a & b`
100 /// fn bitand(self, rhs: Self) -> Self::Output {
101 /// Self(self.0 & rhs.0)
105 /// assert_eq!(Scalar(true) & Scalar(true), Scalar(true));
106 /// assert_eq!(Scalar(true) & Scalar(false), Scalar(false));
107 /// assert_eq!(Scalar(false) & Scalar(true), Scalar(false));
108 /// assert_eq!(Scalar(false) & Scalar(false), Scalar(false));
111 /// An implementation of `BitAnd` for a wrapper around `Vec<bool>`.
114 /// use std::ops::BitAnd;
116 /// #[derive(Debug, PartialEq)]
117 /// struct BooleanVector(Vec<bool>);
119 /// impl BitAnd for BooleanVector {
120 /// type Output = Self;
122 /// fn bitand(self, Self(rhs): Self) -> Self::Output {
123 /// let Self(lhs) = self;
124 /// assert_eq!(lhs.len(), rhs.len());
128 /// .map(|(x, y)| *x & *y)
134 /// let bv1 = BooleanVector(vec![true, true, false, false]);
135 /// let bv2 = BooleanVector(vec![true, false, true, false]);
136 /// let expected = BooleanVector(vec![true, false, false, false]);
137 /// assert_eq!(bv1 & bv2, expected);
141 #[stable(feature = "rust1", since = "1.0.0")]
142 #[rustc_on_unimplemented(
143 message = "no implementation for `{Self} & {Rhs}`",
144 label = "no implementation for `{Self} & {Rhs}`"
146 pub trait BitAnd<Rhs = Self> {
147 /// The resulting type after applying the `&` operator.
148 #[stable(feature = "rust1", since = "1.0.0")]
151 /// Performs the `&` operation.
156 /// assert_eq!(true & false, false);
157 /// assert_eq!(true & true, true);
158 /// assert_eq!(5u8 & 1u8, 1);
159 /// assert_eq!(5u8 & 2u8, 0);
162 #[stable(feature = "rust1", since = "1.0.0")]
163 fn bitand(self, rhs: Rhs) -> Self::Output;
166 macro_rules! bitand_impl {
168 #[stable(feature = "rust1", since = "1.0.0")]
169 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
170 impl const BitAnd for $t {
174 fn bitand(self, rhs: $t) -> $t { self & rhs }
177 forward_ref_binop! { impl const BitAnd, bitand for $t, $t }
181 bitand_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
183 /// The bitwise OR operator `|`.
185 /// Note that `Rhs` is `Self` by default, but this is not mandatory.
189 /// An implementation of `BitOr` for a wrapper around `bool`.
192 /// use std::ops::BitOr;
194 /// #[derive(Debug, PartialEq)]
195 /// struct Scalar(bool);
197 /// impl BitOr for Scalar {
198 /// type Output = Self;
200 /// // rhs is the "right-hand side" of the expression `a | b`
201 /// fn bitor(self, rhs: Self) -> Self::Output {
202 /// Self(self.0 | rhs.0)
206 /// assert_eq!(Scalar(true) | Scalar(true), Scalar(true));
207 /// assert_eq!(Scalar(true) | Scalar(false), Scalar(true));
208 /// assert_eq!(Scalar(false) | Scalar(true), Scalar(true));
209 /// assert_eq!(Scalar(false) | Scalar(false), Scalar(false));
212 /// An implementation of `BitOr` for a wrapper around `Vec<bool>`.
215 /// use std::ops::BitOr;
217 /// #[derive(Debug, PartialEq)]
218 /// struct BooleanVector(Vec<bool>);
220 /// impl BitOr for BooleanVector {
221 /// type Output = Self;
223 /// fn bitor(self, Self(rhs): Self) -> Self::Output {
224 /// let Self(lhs) = self;
225 /// assert_eq!(lhs.len(), rhs.len());
229 /// .map(|(x, y)| *x | *y)
235 /// let bv1 = BooleanVector(vec![true, true, false, false]);
236 /// let bv2 = BooleanVector(vec![true, false, true, false]);
237 /// let expected = BooleanVector(vec![true, true, true, false]);
238 /// assert_eq!(bv1 | bv2, expected);
242 #[stable(feature = "rust1", since = "1.0.0")]
243 #[rustc_on_unimplemented(
244 message = "no implementation for `{Self} | {Rhs}`",
245 label = "no implementation for `{Self} | {Rhs}`"
247 pub trait BitOr<Rhs = Self> {
248 /// The resulting type after applying the `|` operator.
249 #[stable(feature = "rust1", since = "1.0.0")]
252 /// Performs the `|` operation.
257 /// assert_eq!(true | false, true);
258 /// assert_eq!(false | false, false);
259 /// assert_eq!(5u8 | 1u8, 5);
260 /// assert_eq!(5u8 | 2u8, 7);
263 #[stable(feature = "rust1", since = "1.0.0")]
264 fn bitor(self, rhs: Rhs) -> Self::Output;
267 macro_rules! bitor_impl {
269 #[stable(feature = "rust1", since = "1.0.0")]
270 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
271 impl const BitOr for $t {
275 fn bitor(self, rhs: $t) -> $t { self | rhs }
278 forward_ref_binop! { impl const BitOr, bitor for $t, $t }
282 bitor_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
284 /// The bitwise XOR operator `^`.
286 /// Note that `Rhs` is `Self` by default, but this is not mandatory.
290 /// An implementation of `BitXor` that lifts `^` to a wrapper around `bool`.
293 /// use std::ops::BitXor;
295 /// #[derive(Debug, PartialEq)]
296 /// struct Scalar(bool);
298 /// impl BitXor for Scalar {
299 /// type Output = Self;
301 /// // rhs is the "right-hand side" of the expression `a ^ b`
302 /// fn bitxor(self, rhs: Self) -> Self::Output {
303 /// Self(self.0 ^ rhs.0)
307 /// assert_eq!(Scalar(true) ^ Scalar(true), Scalar(false));
308 /// assert_eq!(Scalar(true) ^ Scalar(false), Scalar(true));
309 /// assert_eq!(Scalar(false) ^ Scalar(true), Scalar(true));
310 /// assert_eq!(Scalar(false) ^ Scalar(false), Scalar(false));
313 /// An implementation of `BitXor` trait for a wrapper around `Vec<bool>`.
316 /// use std::ops::BitXor;
318 /// #[derive(Debug, PartialEq)]
319 /// struct BooleanVector(Vec<bool>);
321 /// impl BitXor for BooleanVector {
322 /// type Output = Self;
324 /// fn bitxor(self, Self(rhs): Self) -> Self::Output {
325 /// let Self(lhs) = self;
326 /// assert_eq!(lhs.len(), rhs.len());
330 /// .map(|(x, y)| *x ^ *y)
336 /// let bv1 = BooleanVector(vec![true, true, false, false]);
337 /// let bv2 = BooleanVector(vec![true, false, true, false]);
338 /// let expected = BooleanVector(vec![false, true, true, false]);
339 /// assert_eq!(bv1 ^ bv2, expected);
343 #[stable(feature = "rust1", since = "1.0.0")]
344 #[rustc_on_unimplemented(
345 message = "no implementation for `{Self} ^ {Rhs}`",
346 label = "no implementation for `{Self} ^ {Rhs}`"
348 pub trait BitXor<Rhs = Self> {
349 /// The resulting type after applying the `^` operator.
350 #[stable(feature = "rust1", since = "1.0.0")]
353 /// Performs the `^` operation.
358 /// assert_eq!(true ^ false, true);
359 /// assert_eq!(true ^ true, false);
360 /// assert_eq!(5u8 ^ 1u8, 4);
361 /// assert_eq!(5u8 ^ 2u8, 7);
364 #[stable(feature = "rust1", since = "1.0.0")]
365 fn bitxor(self, rhs: Rhs) -> Self::Output;
368 macro_rules! bitxor_impl {
370 #[stable(feature = "rust1", since = "1.0.0")]
371 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
372 impl const BitXor for $t {
376 fn bitxor(self, other: $t) -> $t { self ^ other }
379 forward_ref_binop! { impl const BitXor, bitxor for $t, $t }
383 bitxor_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
385 /// The left shift operator `<<`. Note that because this trait is implemented
386 /// for all integer types with multiple right-hand-side types, Rust's type
387 /// checker has special handling for `_ << _`, setting the result type for
388 /// integer operations to the type of the left-hand-side operand. This means
389 /// that though `a << b` and `a.shl(b)` are one and the same from an evaluation
390 /// standpoint, they are different when it comes to type inference.
394 /// An implementation of `Shl` that lifts the `<<` operation on integers to a
395 /// wrapper around `usize`.
398 /// use std::ops::Shl;
400 /// #[derive(PartialEq, Debug)]
401 /// struct Scalar(usize);
403 /// impl Shl<Scalar> for Scalar {
404 /// type Output = Self;
406 /// fn shl(self, Self(rhs): Self) -> Self::Output {
407 /// let Self(lhs) = self;
412 /// assert_eq!(Scalar(4) << Scalar(2), Scalar(16));
415 /// An implementation of `Shl` that spins a vector leftward by a given amount.
418 /// use std::ops::Shl;
420 /// #[derive(PartialEq, Debug)]
421 /// struct SpinVector<T: Clone> {
425 /// impl<T: Clone> Shl<usize> for SpinVector<T> {
426 /// type Output = Self;
428 /// fn shl(self, rhs: usize) -> Self::Output {
429 /// // Rotate the vector by `rhs` places.
430 /// let (a, b) = self.vec.split_at(rhs);
431 /// let mut spun_vector = vec![];
432 /// spun_vector.extend_from_slice(b);
433 /// spun_vector.extend_from_slice(a);
434 /// Self { vec: spun_vector }
438 /// assert_eq!(SpinVector { vec: vec![0, 1, 2, 3, 4] } << 2,
439 /// SpinVector { vec: vec![2, 3, 4, 0, 1] });
443 #[stable(feature = "rust1", since = "1.0.0")]
444 #[rustc_on_unimplemented(
445 message = "no implementation for `{Self} << {Rhs}`",
446 label = "no implementation for `{Self} << {Rhs}`"
448 pub trait Shl<Rhs = Self> {
449 /// The resulting type after applying the `<<` operator.
450 #[stable(feature = "rust1", since = "1.0.0")]
453 /// Performs the `<<` operation.
458 /// assert_eq!(5u8 << 1, 10);
459 /// assert_eq!(1u8 << 1, 2);
462 #[stable(feature = "rust1", since = "1.0.0")]
463 fn shl(self, rhs: Rhs) -> Self::Output;
466 macro_rules! shl_impl {
468 #[stable(feature = "rust1", since = "1.0.0")]
469 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
470 impl const Shl<$f> for $t {
474 #[rustc_inherit_overflow_checks]
475 fn shl(self, other: $f) -> $t {
480 forward_ref_binop! { impl const Shl, shl for $t, $f }
484 macro_rules! shl_impl_all {
487 shl_impl! { $t, u16 }
488 shl_impl! { $t, u32 }
489 shl_impl! { $t, u64 }
490 shl_impl! { $t, u128 }
491 shl_impl! { $t, usize }
494 shl_impl! { $t, i16 }
495 shl_impl! { $t, i32 }
496 shl_impl! { $t, i64 }
497 shl_impl! { $t, i128 }
498 shl_impl! { $t, isize }
502 shl_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 isize i128 }
504 /// The right shift operator `>>`. Note that because this trait is implemented
505 /// for all integer types with multiple right-hand-side types, Rust's type
506 /// checker has special handling for `_ >> _`, setting the result type for
507 /// integer operations to the type of the left-hand-side operand. This means
508 /// that though `a >> b` and `a.shr(b)` are one and the same from an evaluation
509 /// standpoint, they are different when it comes to type inference.
513 /// An implementation of `Shr` that lifts the `>>` operation on integers to a
514 /// wrapper around `usize`.
517 /// use std::ops::Shr;
519 /// #[derive(PartialEq, Debug)]
520 /// struct Scalar(usize);
522 /// impl Shr<Scalar> for Scalar {
523 /// type Output = Self;
525 /// fn shr(self, Self(rhs): Self) -> Self::Output {
526 /// let Self(lhs) = self;
531 /// assert_eq!(Scalar(16) >> Scalar(2), Scalar(4));
534 /// An implementation of `Shr` that spins a vector rightward by a given amount.
537 /// use std::ops::Shr;
539 /// #[derive(PartialEq, Debug)]
540 /// struct SpinVector<T: Clone> {
544 /// impl<T: Clone> Shr<usize> for SpinVector<T> {
545 /// type Output = Self;
547 /// fn shr(self, rhs: usize) -> Self::Output {
548 /// // Rotate the vector by `rhs` places.
549 /// let (a, b) = self.vec.split_at(self.vec.len() - rhs);
550 /// let mut spun_vector = vec![];
551 /// spun_vector.extend_from_slice(b);
552 /// spun_vector.extend_from_slice(a);
553 /// Self { vec: spun_vector }
557 /// assert_eq!(SpinVector { vec: vec![0, 1, 2, 3, 4] } >> 2,
558 /// SpinVector { vec: vec![3, 4, 0, 1, 2] });
562 #[stable(feature = "rust1", since = "1.0.0")]
563 #[rustc_on_unimplemented(
564 message = "no implementation for `{Self} >> {Rhs}`",
565 label = "no implementation for `{Self} >> {Rhs}`"
567 pub trait Shr<Rhs = Self> {
568 /// The resulting type after applying the `>>` operator.
569 #[stable(feature = "rust1", since = "1.0.0")]
572 /// Performs the `>>` operation.
577 /// assert_eq!(5u8 >> 1, 2);
578 /// assert_eq!(2u8 >> 1, 1);
581 #[stable(feature = "rust1", since = "1.0.0")]
582 fn shr(self, rhs: Rhs) -> Self::Output;
585 macro_rules! shr_impl {
587 #[stable(feature = "rust1", since = "1.0.0")]
588 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
589 impl const Shr<$f> for $t {
593 #[rustc_inherit_overflow_checks]
594 fn shr(self, other: $f) -> $t {
599 forward_ref_binop! { impl const Shr, shr for $t, $f }
603 macro_rules! shr_impl_all {
606 shr_impl! { $t, u16 }
607 shr_impl! { $t, u32 }
608 shr_impl! { $t, u64 }
609 shr_impl! { $t, u128 }
610 shr_impl! { $t, usize }
613 shr_impl! { $t, i16 }
614 shr_impl! { $t, i32 }
615 shr_impl! { $t, i64 }
616 shr_impl! { $t, i128 }
617 shr_impl! { $t, isize }
621 shr_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
623 /// The bitwise AND assignment operator `&=`.
627 /// An implementation of `BitAndAssign` that lifts the `&=` operator to a
628 /// wrapper around `bool`.
631 /// use std::ops::BitAndAssign;
633 /// #[derive(Debug, PartialEq)]
634 /// struct Scalar(bool);
636 /// impl BitAndAssign for Scalar {
637 /// // rhs is the "right-hand side" of the expression `a &= b`
638 /// fn bitand_assign(&mut self, rhs: Self) {
639 /// *self = Self(self.0 & rhs.0)
643 /// let mut scalar = Scalar(true);
644 /// scalar &= Scalar(true);
645 /// assert_eq!(scalar, Scalar(true));
647 /// let mut scalar = Scalar(true);
648 /// scalar &= Scalar(false);
649 /// assert_eq!(scalar, Scalar(false));
651 /// let mut scalar = Scalar(false);
652 /// scalar &= Scalar(true);
653 /// assert_eq!(scalar, Scalar(false));
655 /// let mut scalar = Scalar(false);
656 /// scalar &= Scalar(false);
657 /// assert_eq!(scalar, Scalar(false));
660 /// Here, the `BitAndAssign` trait is implemented for a wrapper around
664 /// use std::ops::BitAndAssign;
666 /// #[derive(Debug, PartialEq)]
667 /// struct BooleanVector(Vec<bool>);
669 /// impl BitAndAssign for BooleanVector {
670 /// // `rhs` is the "right-hand side" of the expression `a &= b`.
671 /// fn bitand_assign(&mut self, rhs: Self) {
672 /// assert_eq!(self.0.len(), rhs.0.len());
676 /// .zip(rhs.0.iter())
677 /// .map(|(x, y)| *x & *y)
683 /// let mut bv = BooleanVector(vec![true, true, false, false]);
684 /// bv &= BooleanVector(vec![true, false, true, false]);
685 /// let expected = BooleanVector(vec![true, false, false, false]);
686 /// assert_eq!(bv, expected);
688 #[lang = "bitand_assign"]
690 #[stable(feature = "op_assign_traits", since = "1.8.0")]
691 #[rustc_on_unimplemented(
692 message = "no implementation for `{Self} &= {Rhs}`",
693 label = "no implementation for `{Self} &= {Rhs}`"
695 pub trait BitAndAssign<Rhs = Self> {
696 /// Performs the `&=` operation.
701 /// let mut x = true;
703 /// assert_eq!(x, false);
705 /// let mut x = true;
707 /// assert_eq!(x, true);
709 /// let mut x: u8 = 5;
711 /// assert_eq!(x, 1);
713 /// let mut x: u8 = 5;
715 /// assert_eq!(x, 0);
717 #[stable(feature = "op_assign_traits", since = "1.8.0")]
718 fn bitand_assign(&mut self, rhs: Rhs);
721 macro_rules! bitand_assign_impl {
723 #[stable(feature = "op_assign_traits", since = "1.8.0")]
724 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
725 impl const BitAndAssign for $t {
727 fn bitand_assign(&mut self, other: $t) { *self &= other }
730 forward_ref_op_assign! { impl const BitAndAssign, bitand_assign for $t, $t }
734 bitand_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
736 /// The bitwise OR assignment operator `|=`.
741 /// use std::ops::BitOrAssign;
743 /// #[derive(Debug, PartialEq)]
744 /// struct PersonalPreferences {
745 /// likes_cats: bool,
746 /// likes_dogs: bool,
749 /// impl BitOrAssign for PersonalPreferences {
750 /// fn bitor_assign(&mut self, rhs: Self) {
751 /// self.likes_cats |= rhs.likes_cats;
752 /// self.likes_dogs |= rhs.likes_dogs;
756 /// let mut prefs = PersonalPreferences { likes_cats: true, likes_dogs: false };
757 /// prefs |= PersonalPreferences { likes_cats: false, likes_dogs: true };
758 /// assert_eq!(prefs, PersonalPreferences { likes_cats: true, likes_dogs: true });
760 #[lang = "bitor_assign"]
762 #[stable(feature = "op_assign_traits", since = "1.8.0")]
763 #[rustc_on_unimplemented(
764 message = "no implementation for `{Self} |= {Rhs}`",
765 label = "no implementation for `{Self} |= {Rhs}`"
767 pub trait BitOrAssign<Rhs = Self> {
768 /// Performs the `|=` operation.
773 /// let mut x = true;
775 /// assert_eq!(x, true);
777 /// let mut x = false;
779 /// assert_eq!(x, false);
781 /// let mut x: u8 = 5;
783 /// assert_eq!(x, 5);
785 /// let mut x: u8 = 5;
787 /// assert_eq!(x, 7);
789 #[stable(feature = "op_assign_traits", since = "1.8.0")]
790 fn bitor_assign(&mut self, rhs: Rhs);
793 macro_rules! bitor_assign_impl {
795 #[stable(feature = "op_assign_traits", since = "1.8.0")]
796 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
797 impl const BitOrAssign for $t {
799 fn bitor_assign(&mut self, other: $t) { *self |= other }
802 forward_ref_op_assign! { impl const BitOrAssign, bitor_assign for $t, $t }
806 bitor_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
808 /// The bitwise XOR assignment operator `^=`.
813 /// use std::ops::BitXorAssign;
815 /// #[derive(Debug, PartialEq)]
816 /// struct Personality {
818 /// likes_knitting: bool,
821 /// impl BitXorAssign for Personality {
822 /// fn bitxor_assign(&mut self, rhs: Self) {
823 /// self.has_soul ^= rhs.has_soul;
824 /// self.likes_knitting ^= rhs.likes_knitting;
828 /// let mut personality = Personality { has_soul: false, likes_knitting: true };
829 /// personality ^= Personality { has_soul: true, likes_knitting: true };
830 /// assert_eq!(personality, Personality { has_soul: true, likes_knitting: false});
832 #[lang = "bitxor_assign"]
834 #[stable(feature = "op_assign_traits", since = "1.8.0")]
835 #[rustc_on_unimplemented(
836 message = "no implementation for `{Self} ^= {Rhs}`",
837 label = "no implementation for `{Self} ^= {Rhs}`"
839 pub trait BitXorAssign<Rhs = Self> {
840 /// Performs the `^=` operation.
845 /// let mut x = true;
847 /// assert_eq!(x, true);
849 /// let mut x = true;
851 /// assert_eq!(x, false);
853 /// let mut x: u8 = 5;
855 /// assert_eq!(x, 4);
857 /// let mut x: u8 = 5;
859 /// assert_eq!(x, 7);
861 #[stable(feature = "op_assign_traits", since = "1.8.0")]
862 fn bitxor_assign(&mut self, rhs: Rhs);
865 macro_rules! bitxor_assign_impl {
867 #[stable(feature = "op_assign_traits", since = "1.8.0")]
868 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
869 impl const BitXorAssign for $t {
871 fn bitxor_assign(&mut self, other: $t) { *self ^= other }
874 forward_ref_op_assign! { impl const BitXorAssign, bitxor_assign for $t, $t }
878 bitxor_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
880 /// The left shift assignment operator `<<=`.
884 /// An implementation of `ShlAssign` for a wrapper around `usize`.
887 /// use std::ops::ShlAssign;
889 /// #[derive(Debug, PartialEq)]
890 /// struct Scalar(usize);
892 /// impl ShlAssign<usize> for Scalar {
893 /// fn shl_assign(&mut self, rhs: usize) {
898 /// let mut scalar = Scalar(4);
900 /// assert_eq!(scalar, Scalar(16));
902 #[lang = "shl_assign"]
903 #[doc(alias = "<<=")]
904 #[stable(feature = "op_assign_traits", since = "1.8.0")]
905 #[rustc_on_unimplemented(
906 message = "no implementation for `{Self} <<= {Rhs}`",
907 label = "no implementation for `{Self} <<= {Rhs}`"
909 pub trait ShlAssign<Rhs = Self> {
910 /// Performs the `<<=` operation.
915 /// let mut x: u8 = 5;
917 /// assert_eq!(x, 10);
919 /// let mut x: u8 = 1;
921 /// assert_eq!(x, 2);
923 #[stable(feature = "op_assign_traits", since = "1.8.0")]
924 fn shl_assign(&mut self, rhs: Rhs);
927 macro_rules! shl_assign_impl {
929 #[stable(feature = "op_assign_traits", since = "1.8.0")]
930 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
931 impl const ShlAssign<$f> for $t {
933 #[rustc_inherit_overflow_checks]
934 fn shl_assign(&mut self, other: $f) {
939 forward_ref_op_assign! { impl const ShlAssign, shl_assign for $t, $f }
943 macro_rules! shl_assign_impl_all {
945 shl_assign_impl! { $t, u8 }
946 shl_assign_impl! { $t, u16 }
947 shl_assign_impl! { $t, u32 }
948 shl_assign_impl! { $t, u64 }
949 shl_assign_impl! { $t, u128 }
950 shl_assign_impl! { $t, usize }
952 shl_assign_impl! { $t, i8 }
953 shl_assign_impl! { $t, i16 }
954 shl_assign_impl! { $t, i32 }
955 shl_assign_impl! { $t, i64 }
956 shl_assign_impl! { $t, i128 }
957 shl_assign_impl! { $t, isize }
961 shl_assign_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
963 /// The right shift assignment operator `>>=`.
967 /// An implementation of `ShrAssign` for a wrapper around `usize`.
970 /// use std::ops::ShrAssign;
972 /// #[derive(Debug, PartialEq)]
973 /// struct Scalar(usize);
975 /// impl ShrAssign<usize> for Scalar {
976 /// fn shr_assign(&mut self, rhs: usize) {
981 /// let mut scalar = Scalar(16);
983 /// assert_eq!(scalar, Scalar(4));
985 #[lang = "shr_assign"]
986 #[doc(alias = ">>=")]
987 #[stable(feature = "op_assign_traits", since = "1.8.0")]
988 #[rustc_on_unimplemented(
989 message = "no implementation for `{Self} >>= {Rhs}`",
990 label = "no implementation for `{Self} >>= {Rhs}`"
992 pub trait ShrAssign<Rhs = Self> {
993 /// Performs the `>>=` operation.
998 /// let mut x: u8 = 5;
1000 /// assert_eq!(x, 2);
1002 /// let mut x: u8 = 2;
1004 /// assert_eq!(x, 1);
1006 #[stable(feature = "op_assign_traits", since = "1.8.0")]
1007 fn shr_assign(&mut self, rhs: Rhs);
1010 macro_rules! shr_assign_impl {
1012 #[stable(feature = "op_assign_traits", since = "1.8.0")]
1013 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
1014 impl const ShrAssign<$f> for $t {
1016 #[rustc_inherit_overflow_checks]
1017 fn shr_assign(&mut self, other: $f) {
1022 forward_ref_op_assign! { impl const ShrAssign, shr_assign for $t, $f }
1026 macro_rules! shr_assign_impl_all {
1028 shr_assign_impl! { $t, u8 }
1029 shr_assign_impl! { $t, u16 }
1030 shr_assign_impl! { $t, u32 }
1031 shr_assign_impl! { $t, u64 }
1032 shr_assign_impl! { $t, u128 }
1033 shr_assign_impl! { $t, usize }
1035 shr_assign_impl! { $t, i8 }
1036 shr_assign_impl! { $t, i16 }
1037 shr_assign_impl! { $t, i32 }
1038 shr_assign_impl! { $t, i64 }
1039 shr_assign_impl! { $t, i128 }
1040 shr_assign_impl! { $t, isize }
1044 shr_assign_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }