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")]
36 /// The resulting type after applying the `!` operator.
37 #[stable(feature = "rust1", since = "1.0.0")]
40 /// Performs the unary `!` operation.
45 /// assert_eq!(!true, false);
46 /// assert_eq!(!false, true);
47 /// assert_eq!(!1u8, 254);
48 /// assert_eq!(!0u8, 255);
51 #[stable(feature = "rust1", since = "1.0.0")]
52 fn not(self) -> Self::Output;
55 macro_rules! not_impl {
57 #[stable(feature = "rust1", since = "1.0.0")]
58 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
59 impl const Not for $t {
63 fn not(self) -> $t { !self }
66 forward_ref_unop! { impl const Not, not for $t }
70 not_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
72 #[stable(feature = "not_never", since = "1.60.0")]
73 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
74 impl const Not for ! {
83 /// The bitwise AND operator `&`.
85 /// Note that `Rhs` is `Self` by default, but this is not mandatory.
89 /// An implementation of `BitAnd` for a wrapper around `bool`.
92 /// use std::ops::BitAnd;
94 /// #[derive(Debug, PartialEq)]
95 /// struct Scalar(bool);
97 /// impl BitAnd for Scalar {
98 /// type Output = Self;
100 /// // rhs is the "right-hand side" of the expression `a & b`
101 /// fn bitand(self, rhs: Self) -> Self::Output {
102 /// Self(self.0 & rhs.0)
106 /// assert_eq!(Scalar(true) & Scalar(true), Scalar(true));
107 /// assert_eq!(Scalar(true) & Scalar(false), Scalar(false));
108 /// assert_eq!(Scalar(false) & Scalar(true), Scalar(false));
109 /// assert_eq!(Scalar(false) & Scalar(false), Scalar(false));
112 /// An implementation of `BitAnd` for a wrapper around `Vec<bool>`.
115 /// use std::ops::BitAnd;
117 /// #[derive(Debug, PartialEq)]
118 /// struct BooleanVector(Vec<bool>);
120 /// impl BitAnd for BooleanVector {
121 /// type Output = Self;
123 /// fn bitand(self, Self(rhs): Self) -> Self::Output {
124 /// let Self(lhs) = self;
125 /// assert_eq!(lhs.len(), rhs.len());
129 /// .map(|(x, y)| *x & *y)
135 /// let bv1 = BooleanVector(vec![true, true, false, false]);
136 /// let bv2 = BooleanVector(vec![true, false, true, false]);
137 /// let expected = BooleanVector(vec![true, false, false, false]);
138 /// assert_eq!(bv1 & bv2, expected);
142 #[stable(feature = "rust1", since = "1.0.0")]
143 #[rustc_on_unimplemented(
144 message = "no implementation for `{Self} & {Rhs}`",
145 label = "no implementation for `{Self} & {Rhs}`"
148 pub trait BitAnd<Rhs = Self> {
149 /// The resulting type after applying the `&` operator.
150 #[stable(feature = "rust1", since = "1.0.0")]
153 /// Performs the `&` operation.
158 /// assert_eq!(true & false, false);
159 /// assert_eq!(true & true, true);
160 /// assert_eq!(5u8 & 1u8, 1);
161 /// assert_eq!(5u8 & 2u8, 0);
164 #[stable(feature = "rust1", since = "1.0.0")]
165 fn bitand(self, rhs: Rhs) -> Self::Output;
168 macro_rules! bitand_impl {
170 #[stable(feature = "rust1", since = "1.0.0")]
171 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
172 impl const BitAnd for $t {
176 fn bitand(self, rhs: $t) -> $t { self & rhs }
179 forward_ref_binop! { impl const BitAnd, bitand for $t, $t }
183 bitand_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
185 /// The bitwise OR operator `|`.
187 /// Note that `Rhs` is `Self` by default, but this is not mandatory.
191 /// An implementation of `BitOr` for a wrapper around `bool`.
194 /// use std::ops::BitOr;
196 /// #[derive(Debug, PartialEq)]
197 /// struct Scalar(bool);
199 /// impl BitOr for Scalar {
200 /// type Output = Self;
202 /// // rhs is the "right-hand side" of the expression `a | b`
203 /// fn bitor(self, rhs: Self) -> Self::Output {
204 /// Self(self.0 | rhs.0)
208 /// assert_eq!(Scalar(true) | Scalar(true), Scalar(true));
209 /// assert_eq!(Scalar(true) | Scalar(false), Scalar(true));
210 /// assert_eq!(Scalar(false) | Scalar(true), Scalar(true));
211 /// assert_eq!(Scalar(false) | Scalar(false), Scalar(false));
214 /// An implementation of `BitOr` for a wrapper around `Vec<bool>`.
217 /// use std::ops::BitOr;
219 /// #[derive(Debug, PartialEq)]
220 /// struct BooleanVector(Vec<bool>);
222 /// impl BitOr for BooleanVector {
223 /// type Output = Self;
225 /// fn bitor(self, Self(rhs): Self) -> Self::Output {
226 /// let Self(lhs) = self;
227 /// assert_eq!(lhs.len(), rhs.len());
231 /// .map(|(x, y)| *x | *y)
237 /// let bv1 = BooleanVector(vec![true, true, false, false]);
238 /// let bv2 = BooleanVector(vec![true, false, true, false]);
239 /// let expected = BooleanVector(vec![true, true, true, false]);
240 /// assert_eq!(bv1 | bv2, expected);
244 #[stable(feature = "rust1", since = "1.0.0")]
245 #[rustc_on_unimplemented(
246 message = "no implementation for `{Self} | {Rhs}`",
247 label = "no implementation for `{Self} | {Rhs}`"
250 pub trait BitOr<Rhs = Self> {
251 /// The resulting type after applying the `|` operator.
252 #[stable(feature = "rust1", since = "1.0.0")]
255 /// Performs the `|` operation.
260 /// assert_eq!(true | false, true);
261 /// assert_eq!(false | false, false);
262 /// assert_eq!(5u8 | 1u8, 5);
263 /// assert_eq!(5u8 | 2u8, 7);
266 #[stable(feature = "rust1", since = "1.0.0")]
267 fn bitor(self, rhs: Rhs) -> Self::Output;
270 macro_rules! bitor_impl {
272 #[stable(feature = "rust1", since = "1.0.0")]
273 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
274 impl const BitOr for $t {
278 fn bitor(self, rhs: $t) -> $t { self | rhs }
281 forward_ref_binop! { impl const BitOr, bitor for $t, $t }
285 bitor_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
287 /// The bitwise XOR operator `^`.
289 /// Note that `Rhs` is `Self` by default, but this is not mandatory.
293 /// An implementation of `BitXor` that lifts `^` to a wrapper around `bool`.
296 /// use std::ops::BitXor;
298 /// #[derive(Debug, PartialEq)]
299 /// struct Scalar(bool);
301 /// impl BitXor for Scalar {
302 /// type Output = Self;
304 /// // rhs is the "right-hand side" of the expression `a ^ b`
305 /// fn bitxor(self, rhs: Self) -> Self::Output {
306 /// Self(self.0 ^ rhs.0)
310 /// assert_eq!(Scalar(true) ^ Scalar(true), Scalar(false));
311 /// assert_eq!(Scalar(true) ^ Scalar(false), Scalar(true));
312 /// assert_eq!(Scalar(false) ^ Scalar(true), Scalar(true));
313 /// assert_eq!(Scalar(false) ^ Scalar(false), Scalar(false));
316 /// An implementation of `BitXor` trait for a wrapper around `Vec<bool>`.
319 /// use std::ops::BitXor;
321 /// #[derive(Debug, PartialEq)]
322 /// struct BooleanVector(Vec<bool>);
324 /// impl BitXor for BooleanVector {
325 /// type Output = Self;
327 /// fn bitxor(self, Self(rhs): Self) -> Self::Output {
328 /// let Self(lhs) = self;
329 /// assert_eq!(lhs.len(), rhs.len());
333 /// .map(|(x, y)| *x ^ *y)
339 /// let bv1 = BooleanVector(vec![true, true, false, false]);
340 /// let bv2 = BooleanVector(vec![true, false, true, false]);
341 /// let expected = BooleanVector(vec![false, true, true, false]);
342 /// assert_eq!(bv1 ^ bv2, expected);
346 #[stable(feature = "rust1", since = "1.0.0")]
347 #[rustc_on_unimplemented(
348 message = "no implementation for `{Self} ^ {Rhs}`",
349 label = "no implementation for `{Self} ^ {Rhs}`"
352 pub trait BitXor<Rhs = Self> {
353 /// The resulting type after applying the `^` operator.
354 #[stable(feature = "rust1", since = "1.0.0")]
357 /// Performs the `^` operation.
362 /// assert_eq!(true ^ false, true);
363 /// assert_eq!(true ^ true, false);
364 /// assert_eq!(5u8 ^ 1u8, 4);
365 /// assert_eq!(5u8 ^ 2u8, 7);
368 #[stable(feature = "rust1", since = "1.0.0")]
369 fn bitxor(self, rhs: Rhs) -> Self::Output;
372 macro_rules! bitxor_impl {
374 #[stable(feature = "rust1", since = "1.0.0")]
375 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
376 impl const BitXor for $t {
380 fn bitxor(self, other: $t) -> $t { self ^ other }
383 forward_ref_binop! { impl const BitXor, bitxor for $t, $t }
387 bitxor_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
389 /// The left shift operator `<<`. Note that because this trait is implemented
390 /// for all integer types with multiple right-hand-side types, Rust's type
391 /// checker has special handling for `_ << _`, setting the result type for
392 /// integer operations to the type of the left-hand-side operand. This means
393 /// that though `a << b` and `a.shl(b)` are one and the same from an evaluation
394 /// standpoint, they are different when it comes to type inference.
398 /// An implementation of `Shl` that lifts the `<<` operation on integers to a
399 /// wrapper around `usize`.
402 /// use std::ops::Shl;
404 /// #[derive(PartialEq, Debug)]
405 /// struct Scalar(usize);
407 /// impl Shl<Scalar> for Scalar {
408 /// type Output = Self;
410 /// fn shl(self, Self(rhs): Self) -> Self::Output {
411 /// let Self(lhs) = self;
416 /// assert_eq!(Scalar(4) << Scalar(2), Scalar(16));
419 /// An implementation of `Shl` that spins a vector leftward by a given amount.
422 /// use std::ops::Shl;
424 /// #[derive(PartialEq, Debug)]
425 /// struct SpinVector<T: Clone> {
429 /// impl<T: Clone> Shl<usize> for SpinVector<T> {
430 /// type Output = Self;
432 /// fn shl(self, rhs: usize) -> Self::Output {
433 /// // Rotate the vector by `rhs` places.
434 /// let (a, b) = self.vec.split_at(rhs);
435 /// let mut spun_vector = vec![];
436 /// spun_vector.extend_from_slice(b);
437 /// spun_vector.extend_from_slice(a);
438 /// Self { vec: spun_vector }
442 /// assert_eq!(SpinVector { vec: vec![0, 1, 2, 3, 4] } << 2,
443 /// SpinVector { vec: vec![2, 3, 4, 0, 1] });
447 #[stable(feature = "rust1", since = "1.0.0")]
448 #[rustc_on_unimplemented(
449 message = "no implementation for `{Self} << {Rhs}`",
450 label = "no implementation for `{Self} << {Rhs}`"
453 pub trait Shl<Rhs = Self> {
454 /// The resulting type after applying the `<<` operator.
455 #[stable(feature = "rust1", since = "1.0.0")]
458 /// Performs the `<<` operation.
463 /// assert_eq!(5u8 << 1, 10);
464 /// assert_eq!(1u8 << 1, 2);
467 #[stable(feature = "rust1", since = "1.0.0")]
468 fn shl(self, rhs: Rhs) -> Self::Output;
471 macro_rules! shl_impl {
473 #[stable(feature = "rust1", since = "1.0.0")]
474 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
475 impl const Shl<$f> for $t {
479 #[rustc_inherit_overflow_checks]
480 fn shl(self, other: $f) -> $t {
485 forward_ref_binop! { impl const Shl, shl for $t, $f }
489 macro_rules! shl_impl_all {
492 shl_impl! { $t, u16 }
493 shl_impl! { $t, u32 }
494 shl_impl! { $t, u64 }
495 shl_impl! { $t, u128 }
496 shl_impl! { $t, usize }
499 shl_impl! { $t, i16 }
500 shl_impl! { $t, i32 }
501 shl_impl! { $t, i64 }
502 shl_impl! { $t, i128 }
503 shl_impl! { $t, isize }
507 shl_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 isize i128 }
509 /// The right shift operator `>>`. Note that because this trait is implemented
510 /// for all integer types with multiple right-hand-side types, Rust's type
511 /// checker has special handling for `_ >> _`, setting the result type for
512 /// integer operations to the type of the left-hand-side operand. This means
513 /// that though `a >> b` and `a.shr(b)` are one and the same from an evaluation
514 /// standpoint, they are different when it comes to type inference.
518 /// An implementation of `Shr` that lifts the `>>` operation on integers to a
519 /// wrapper around `usize`.
522 /// use std::ops::Shr;
524 /// #[derive(PartialEq, Debug)]
525 /// struct Scalar(usize);
527 /// impl Shr<Scalar> for Scalar {
528 /// type Output = Self;
530 /// fn shr(self, Self(rhs): Self) -> Self::Output {
531 /// let Self(lhs) = self;
536 /// assert_eq!(Scalar(16) >> Scalar(2), Scalar(4));
539 /// An implementation of `Shr` that spins a vector rightward by a given amount.
542 /// use std::ops::Shr;
544 /// #[derive(PartialEq, Debug)]
545 /// struct SpinVector<T: Clone> {
549 /// impl<T: Clone> Shr<usize> for SpinVector<T> {
550 /// type Output = Self;
552 /// fn shr(self, rhs: usize) -> Self::Output {
553 /// // Rotate the vector by `rhs` places.
554 /// let (a, b) = self.vec.split_at(self.vec.len() - rhs);
555 /// let mut spun_vector = vec![];
556 /// spun_vector.extend_from_slice(b);
557 /// spun_vector.extend_from_slice(a);
558 /// Self { vec: spun_vector }
562 /// assert_eq!(SpinVector { vec: vec![0, 1, 2, 3, 4] } >> 2,
563 /// SpinVector { vec: vec![3, 4, 0, 1, 2] });
567 #[stable(feature = "rust1", since = "1.0.0")]
568 #[rustc_on_unimplemented(
569 message = "no implementation for `{Self} >> {Rhs}`",
570 label = "no implementation for `{Self} >> {Rhs}`"
573 pub trait Shr<Rhs = Self> {
574 /// The resulting type after applying the `>>` operator.
575 #[stable(feature = "rust1", since = "1.0.0")]
578 /// Performs the `>>` operation.
583 /// assert_eq!(5u8 >> 1, 2);
584 /// assert_eq!(2u8 >> 1, 1);
587 #[stable(feature = "rust1", since = "1.0.0")]
588 fn shr(self, rhs: Rhs) -> Self::Output;
591 macro_rules! shr_impl {
593 #[stable(feature = "rust1", since = "1.0.0")]
594 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
595 impl const Shr<$f> for $t {
599 #[rustc_inherit_overflow_checks]
600 fn shr(self, other: $f) -> $t {
605 forward_ref_binop! { impl const Shr, shr for $t, $f }
609 macro_rules! shr_impl_all {
612 shr_impl! { $t, u16 }
613 shr_impl! { $t, u32 }
614 shr_impl! { $t, u64 }
615 shr_impl! { $t, u128 }
616 shr_impl! { $t, usize }
619 shr_impl! { $t, i16 }
620 shr_impl! { $t, i32 }
621 shr_impl! { $t, i64 }
622 shr_impl! { $t, i128 }
623 shr_impl! { $t, isize }
627 shr_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
629 /// The bitwise AND assignment operator `&=`.
633 /// An implementation of `BitAndAssign` that lifts the `&=` operator to a
634 /// wrapper around `bool`.
637 /// use std::ops::BitAndAssign;
639 /// #[derive(Debug, PartialEq)]
640 /// struct Scalar(bool);
642 /// impl BitAndAssign for Scalar {
643 /// // rhs is the "right-hand side" of the expression `a &= b`
644 /// fn bitand_assign(&mut self, rhs: Self) {
645 /// *self = Self(self.0 & rhs.0)
649 /// let mut scalar = Scalar(true);
650 /// scalar &= Scalar(true);
651 /// assert_eq!(scalar, Scalar(true));
653 /// let mut scalar = Scalar(true);
654 /// scalar &= Scalar(false);
655 /// assert_eq!(scalar, Scalar(false));
657 /// let mut scalar = Scalar(false);
658 /// scalar &= Scalar(true);
659 /// assert_eq!(scalar, Scalar(false));
661 /// let mut scalar = Scalar(false);
662 /// scalar &= Scalar(false);
663 /// assert_eq!(scalar, Scalar(false));
666 /// Here, the `BitAndAssign` trait is implemented for a wrapper around
670 /// use std::ops::BitAndAssign;
672 /// #[derive(Debug, PartialEq)]
673 /// struct BooleanVector(Vec<bool>);
675 /// impl BitAndAssign for BooleanVector {
676 /// // `rhs` is the "right-hand side" of the expression `a &= b`.
677 /// fn bitand_assign(&mut self, rhs: Self) {
678 /// assert_eq!(self.0.len(), rhs.0.len());
682 /// .zip(rhs.0.iter())
683 /// .map(|(x, y)| *x & *y)
689 /// let mut bv = BooleanVector(vec![true, true, false, false]);
690 /// bv &= BooleanVector(vec![true, false, true, false]);
691 /// let expected = BooleanVector(vec![true, false, false, false]);
692 /// assert_eq!(bv, expected);
694 #[lang = "bitand_assign"]
696 #[stable(feature = "op_assign_traits", since = "1.8.0")]
697 #[rustc_on_unimplemented(
698 message = "no implementation for `{Self} &= {Rhs}`",
699 label = "no implementation for `{Self} &= {Rhs}`"
702 pub trait BitAndAssign<Rhs = Self> {
703 /// Performs the `&=` operation.
708 /// let mut x = true;
710 /// assert_eq!(x, false);
712 /// let mut x = true;
714 /// assert_eq!(x, true);
716 /// let mut x: u8 = 5;
718 /// assert_eq!(x, 1);
720 /// let mut x: u8 = 5;
722 /// assert_eq!(x, 0);
724 #[stable(feature = "op_assign_traits", since = "1.8.0")]
725 fn bitand_assign(&mut self, rhs: Rhs);
728 macro_rules! bitand_assign_impl {
730 #[stable(feature = "op_assign_traits", since = "1.8.0")]
731 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
732 impl const BitAndAssign for $t {
734 fn bitand_assign(&mut self, other: $t) { *self &= other }
737 forward_ref_op_assign! { impl const BitAndAssign, bitand_assign for $t, $t }
741 bitand_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
743 /// The bitwise OR assignment operator `|=`.
748 /// use std::ops::BitOrAssign;
750 /// #[derive(Debug, PartialEq)]
751 /// struct PersonalPreferences {
752 /// likes_cats: bool,
753 /// likes_dogs: bool,
756 /// impl BitOrAssign for PersonalPreferences {
757 /// fn bitor_assign(&mut self, rhs: Self) {
758 /// self.likes_cats |= rhs.likes_cats;
759 /// self.likes_dogs |= rhs.likes_dogs;
763 /// let mut prefs = PersonalPreferences { likes_cats: true, likes_dogs: false };
764 /// prefs |= PersonalPreferences { likes_cats: false, likes_dogs: true };
765 /// assert_eq!(prefs, PersonalPreferences { likes_cats: true, likes_dogs: true });
767 #[lang = "bitor_assign"]
769 #[stable(feature = "op_assign_traits", since = "1.8.0")]
770 #[rustc_on_unimplemented(
771 message = "no implementation for `{Self} |= {Rhs}`",
772 label = "no implementation for `{Self} |= {Rhs}`"
775 pub trait BitOrAssign<Rhs = Self> {
776 /// Performs the `|=` operation.
781 /// let mut x = true;
783 /// assert_eq!(x, true);
785 /// let mut x = false;
787 /// assert_eq!(x, false);
789 /// let mut x: u8 = 5;
791 /// assert_eq!(x, 5);
793 /// let mut x: u8 = 5;
795 /// assert_eq!(x, 7);
797 #[stable(feature = "op_assign_traits", since = "1.8.0")]
798 fn bitor_assign(&mut self, rhs: Rhs);
801 macro_rules! bitor_assign_impl {
803 #[stable(feature = "op_assign_traits", since = "1.8.0")]
804 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
805 impl const BitOrAssign for $t {
807 fn bitor_assign(&mut self, other: $t) { *self |= other }
810 forward_ref_op_assign! { impl const BitOrAssign, bitor_assign for $t, $t }
814 bitor_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
816 /// The bitwise XOR assignment operator `^=`.
821 /// use std::ops::BitXorAssign;
823 /// #[derive(Debug, PartialEq)]
824 /// struct Personality {
826 /// likes_knitting: bool,
829 /// impl BitXorAssign for Personality {
830 /// fn bitxor_assign(&mut self, rhs: Self) {
831 /// self.has_soul ^= rhs.has_soul;
832 /// self.likes_knitting ^= rhs.likes_knitting;
836 /// let mut personality = Personality { has_soul: false, likes_knitting: true };
837 /// personality ^= Personality { has_soul: true, likes_knitting: true };
838 /// assert_eq!(personality, Personality { has_soul: true, likes_knitting: false});
840 #[lang = "bitxor_assign"]
842 #[stable(feature = "op_assign_traits", since = "1.8.0")]
843 #[rustc_on_unimplemented(
844 message = "no implementation for `{Self} ^= {Rhs}`",
845 label = "no implementation for `{Self} ^= {Rhs}`"
848 pub trait BitXorAssign<Rhs = Self> {
849 /// Performs the `^=` operation.
854 /// let mut x = true;
856 /// assert_eq!(x, true);
858 /// let mut x = true;
860 /// assert_eq!(x, false);
862 /// let mut x: u8 = 5;
864 /// assert_eq!(x, 4);
866 /// let mut x: u8 = 5;
868 /// assert_eq!(x, 7);
870 #[stable(feature = "op_assign_traits", since = "1.8.0")]
871 fn bitxor_assign(&mut self, rhs: Rhs);
874 macro_rules! bitxor_assign_impl {
876 #[stable(feature = "op_assign_traits", since = "1.8.0")]
877 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
878 impl const BitXorAssign for $t {
880 fn bitxor_assign(&mut self, other: $t) { *self ^= other }
883 forward_ref_op_assign! { impl const BitXorAssign, bitxor_assign for $t, $t }
887 bitxor_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
889 /// The left shift assignment operator `<<=`.
893 /// An implementation of `ShlAssign` for a wrapper around `usize`.
896 /// use std::ops::ShlAssign;
898 /// #[derive(Debug, PartialEq)]
899 /// struct Scalar(usize);
901 /// impl ShlAssign<usize> for Scalar {
902 /// fn shl_assign(&mut self, rhs: usize) {
907 /// let mut scalar = Scalar(4);
909 /// assert_eq!(scalar, Scalar(16));
911 #[lang = "shl_assign"]
912 #[doc(alias = "<<=")]
913 #[stable(feature = "op_assign_traits", since = "1.8.0")]
914 #[rustc_on_unimplemented(
915 message = "no implementation for `{Self} <<= {Rhs}`",
916 label = "no implementation for `{Self} <<= {Rhs}`"
919 pub trait ShlAssign<Rhs = Self> {
920 /// Performs the `<<=` operation.
925 /// let mut x: u8 = 5;
927 /// assert_eq!(x, 10);
929 /// let mut x: u8 = 1;
931 /// assert_eq!(x, 2);
933 #[stable(feature = "op_assign_traits", since = "1.8.0")]
934 fn shl_assign(&mut self, rhs: Rhs);
937 macro_rules! shl_assign_impl {
939 #[stable(feature = "op_assign_traits", since = "1.8.0")]
940 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
941 impl const ShlAssign<$f> for $t {
943 #[rustc_inherit_overflow_checks]
944 fn shl_assign(&mut self, other: $f) {
949 forward_ref_op_assign! { impl const ShlAssign, shl_assign for $t, $f }
953 macro_rules! shl_assign_impl_all {
955 shl_assign_impl! { $t, u8 }
956 shl_assign_impl! { $t, u16 }
957 shl_assign_impl! { $t, u32 }
958 shl_assign_impl! { $t, u64 }
959 shl_assign_impl! { $t, u128 }
960 shl_assign_impl! { $t, usize }
962 shl_assign_impl! { $t, i8 }
963 shl_assign_impl! { $t, i16 }
964 shl_assign_impl! { $t, i32 }
965 shl_assign_impl! { $t, i64 }
966 shl_assign_impl! { $t, i128 }
967 shl_assign_impl! { $t, isize }
971 shl_assign_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
973 /// The right shift assignment operator `>>=`.
977 /// An implementation of `ShrAssign` for a wrapper around `usize`.
980 /// use std::ops::ShrAssign;
982 /// #[derive(Debug, PartialEq)]
983 /// struct Scalar(usize);
985 /// impl ShrAssign<usize> for Scalar {
986 /// fn shr_assign(&mut self, rhs: usize) {
991 /// let mut scalar = Scalar(16);
993 /// assert_eq!(scalar, Scalar(4));
995 #[lang = "shr_assign"]
996 #[doc(alias = ">>=")]
997 #[stable(feature = "op_assign_traits", since = "1.8.0")]
998 #[rustc_on_unimplemented(
999 message = "no implementation for `{Self} >>= {Rhs}`",
1000 label = "no implementation for `{Self} >>= {Rhs}`"
1003 pub trait ShrAssign<Rhs = Self> {
1004 /// Performs the `>>=` operation.
1009 /// let mut x: u8 = 5;
1011 /// assert_eq!(x, 2);
1013 /// let mut x: u8 = 2;
1015 /// assert_eq!(x, 1);
1017 #[stable(feature = "op_assign_traits", since = "1.8.0")]
1018 fn shr_assign(&mut self, rhs: Rhs);
1021 macro_rules! shr_assign_impl {
1023 #[stable(feature = "op_assign_traits", since = "1.8.0")]
1024 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
1025 impl const ShrAssign<$f> for $t {
1027 #[rustc_inherit_overflow_checks]
1028 fn shr_assign(&mut self, other: $f) {
1033 forward_ref_op_assign! { impl const ShrAssign, shr_assign for $t, $f }
1037 macro_rules! shr_assign_impl_all {
1039 shr_assign_impl! { $t, u8 }
1040 shr_assign_impl! { $t, u16 }
1041 shr_assign_impl! { $t, u32 }
1042 shr_assign_impl! { $t, u64 }
1043 shr_assign_impl! { $t, u128 }
1044 shr_assign_impl! { $t, usize }
1046 shr_assign_impl! { $t, i8 }
1047 shr_assign_impl! { $t, i16 }
1048 shr_assign_impl! { $t, i32 }
1049 shr_assign_impl! { $t, i64 }
1050 shr_assign_impl! { $t, i128 }
1051 shr_assign_impl! { $t, isize }
1055 shr_assign_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }