1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Overloadable operators
13 //! Implementing these traits allows you to get an effect similar to
14 //! overloading operators.
16 //! Some of these traits are imported by the prelude, so they are available in
17 //! every Rust program.
19 //! Many of the operators take their operands by value. In non-generic
20 //! contexts involving built-in types, this is usually not a problem.
21 //! However, using these operators in generic code, requires some
22 //! attention if values have to be reused as opposed to letting the operators
23 //! consume them. One option is to occasionally use `clone()`.
24 //! Another option is to rely on the types involved providing additional
25 //! operator implementations for references. For example, for a user-defined
26 //! type `T` which is supposed to support addition, it is probably a good
27 //! idea to have both `T` and `&T` implement the traits `Add<T>` and `Add<&T>`
28 //! so that generic code can be written without unnecessary cloning.
32 //! This example creates a `Point` struct that implements `Add` and `Sub`, and then
33 //! demonstrates adding and subtracting two `Point`s.
36 //! use std::ops::{Add, Sub};
44 //! impl Add for Point {
45 //! type Output = Point;
47 //! fn add(self, other: Point) -> Point {
48 //! Point {x: self.x + other.x, y: self.y + other.y}
52 //! impl Sub for Point {
53 //! type Output = Point;
55 //! fn sub(self, other: Point) -> Point {
56 //! Point {x: self.x - other.x, y: self.y - other.y}
60 //! println!("{:?}", Point {x: 1, y: 0} + Point {x: 2, y: 3});
61 //! println!("{:?}", Point {x: 1, y: 0} - Point {x: 2, y: 3});
65 //! See the documentation for each trait for a minimum implementation that prints
66 //! something to the screen.
68 #![stable(feature = "rust1", since = "1.0.0")]
73 /// The `Drop` trait is used to run some code when a value goes out of scope. This
74 /// is sometimes called a 'destructor'.
78 /// A trivial implementation of `Drop`. The `drop` method is called when `_x` goes
79 /// out of scope, and therefore `main` prints `Dropping!`.
84 /// impl Drop for HasDrop {
85 /// fn drop(&mut self) {
86 /// println!("Dropping!");
95 #[stable(feature = "rust1", since = "1.0.0")]
97 /// The `drop` method, called when the value goes out of scope.
98 #[stable(feature = "rust1", since = "1.0.0")]
102 // implements the unary operator "op &T"
103 // based on "op T" where T is expected to be `Copy`able
104 macro_rules! forward_ref_unop {
105 (impl $imp:ident, $method:ident for $t:ty) => {
106 #[unstable(feature = "core",
107 reason = "recently added, waiting for dust to settle")]
108 impl<'a> $imp for &'a $t {
109 type Output = <$t as $imp>::Output;
112 fn $method(self) -> <$t as $imp>::Output {
119 // implements binary operators "&T op U", "T op &U", "&T op &U"
120 // based on "T op U" where T and U are expected to be `Copy`able
121 macro_rules! forward_ref_binop {
122 (impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
123 #[unstable(feature = "core",
124 reason = "recently added, waiting for dust to settle")]
125 impl<'a> $imp<$u> for &'a $t {
126 type Output = <$t as $imp<$u>>::Output;
129 fn $method(self, other: $u) -> <$t as $imp<$u>>::Output {
130 $imp::$method(*self, other)
134 #[unstable(feature = "core",
135 reason = "recently added, waiting for dust to settle")]
136 impl<'a> $imp<&'a $u> for $t {
137 type Output = <$t as $imp<$u>>::Output;
140 fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output {
141 $imp::$method(self, *other)
145 #[unstable(feature = "core",
146 reason = "recently added, waiting for dust to settle")]
147 impl<'a, 'b> $imp<&'a $u> for &'b $t {
148 type Output = <$t as $imp<$u>>::Output;
151 fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output {
152 $imp::$method(*self, *other)
158 /// The `Add` trait is used to specify the functionality of `+`.
162 /// A trivial implementation of `Add`. When `Foo + Foo` happens, it ends up
163 /// calling `add`, and therefore, `main` prints `Adding!`.
166 /// use std::ops::Add;
171 /// impl Add for Foo {
172 /// type Output = Foo;
174 /// fn add(self, _rhs: Foo) -> Foo {
175 /// println!("Adding!");
185 #[stable(feature = "rust1", since = "1.0.0")]
186 pub trait Add<RHS=Self> {
187 #[stable(feature = "rust1", since = "1.0.0")]
190 /// The method for the `+` operator
191 #[stable(feature = "rust1", since = "1.0.0")]
192 fn add(self, rhs: RHS) -> Self::Output;
195 macro_rules! add_impl {
197 #[stable(feature = "rust1", since = "1.0.0")]
202 fn add(self, other: $t) -> $t { self + other }
205 forward_ref_binop! { impl Add, add for $t, $t }
209 add_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 }
211 /// The `Sub` trait is used to specify the functionality of `-`.
215 /// A trivial implementation of `Sub`. When `Foo - Foo` happens, it ends up
216 /// calling `sub`, and therefore, `main` prints `Subtracting!`.
219 /// use std::ops::Sub;
224 /// impl Sub for Foo {
225 /// type Output = Foo;
227 /// fn sub(self, _rhs: Foo) -> Foo {
228 /// println!("Subtracting!");
238 #[stable(feature = "rust1", since = "1.0.0")]
239 pub trait Sub<RHS=Self> {
240 #[stable(feature = "rust1", since = "1.0.0")]
243 /// The method for the `-` operator
244 #[stable(feature = "rust1", since = "1.0.0")]
245 fn sub(self, rhs: RHS) -> Self::Output;
248 macro_rules! sub_impl {
250 #[stable(feature = "rust1", since = "1.0.0")]
255 fn sub(self, other: $t) -> $t { self - other }
258 forward_ref_binop! { impl Sub, sub for $t, $t }
262 sub_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 }
264 /// The `Mul` trait is used to specify the functionality of `*`.
268 /// A trivial implementation of `Mul`. When `Foo * Foo` happens, it ends up
269 /// calling `mul`, and therefore, `main` prints `Multiplying!`.
272 /// use std::ops::Mul;
277 /// impl Mul for Foo {
278 /// type Output = Foo;
280 /// fn mul(self, _rhs: Foo) -> Foo {
281 /// println!("Multiplying!");
291 #[stable(feature = "rust1", since = "1.0.0")]
292 pub trait Mul<RHS=Self> {
293 #[stable(feature = "rust1", since = "1.0.0")]
296 /// The method for the `*` operator
297 #[stable(feature = "rust1", since = "1.0.0")]
298 fn mul(self, rhs: RHS) -> Self::Output;
301 macro_rules! mul_impl {
303 #[stable(feature = "rust1", since = "1.0.0")]
308 fn mul(self, other: $t) -> $t { self * other }
311 forward_ref_binop! { impl Mul, mul for $t, $t }
315 mul_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 }
317 /// The `Div` trait is used to specify the functionality of `/`.
321 /// A trivial implementation of `Div`. When `Foo / Foo` happens, it ends up
322 /// calling `div`, and therefore, `main` prints `Dividing!`.
325 /// use std::ops::Div;
330 /// impl Div for Foo {
331 /// type Output = Foo;
333 /// fn div(self, _rhs: Foo) -> Foo {
334 /// println!("Dividing!");
344 #[stable(feature = "rust1", since = "1.0.0")]
345 pub trait Div<RHS=Self> {
346 #[stable(feature = "rust1", since = "1.0.0")]
349 /// The method for the `/` operator
350 #[stable(feature = "rust1", since = "1.0.0")]
351 fn div(self, rhs: RHS) -> Self::Output;
354 macro_rules! div_impl {
356 #[stable(feature = "rust1", since = "1.0.0")]
361 fn div(self, other: $t) -> $t { self / other }
364 forward_ref_binop! { impl Div, div for $t, $t }
368 div_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 }
370 /// The `Rem` trait is used to specify the functionality of `%`.
374 /// A trivial implementation of `Rem`. When `Foo % Foo` happens, it ends up
375 /// calling `rem`, and therefore, `main` prints `Remainder-ing!`.
378 /// use std::ops::Rem;
383 /// impl Rem for Foo {
384 /// type Output = Foo;
386 /// fn rem(self, _rhs: Foo) -> Foo {
387 /// println!("Remainder-ing!");
397 #[stable(feature = "rust1", since = "1.0.0")]
398 pub trait Rem<RHS=Self> {
399 #[stable(feature = "rust1", since = "1.0.0")]
402 /// The method for the `%` operator
403 #[stable(feature = "rust1", since = "1.0.0")]
404 fn rem(self, rhs: RHS) -> Self::Output;
407 macro_rules! rem_impl {
409 #[stable(feature = "rust1", since = "1.0.0")]
414 fn rem(self, other: $t) -> $t { self % other }
417 forward_ref_binop! { impl Rem, rem for $t, $t }
421 macro_rules! rem_float_impl {
422 ($t:ty, $fmod:ident) => {
423 #[stable(feature = "rust1", since = "1.0.0")]
428 fn rem(self, other: $t) -> $t {
429 extern { fn $fmod(a: $t, b: $t) -> $t; }
430 unsafe { $fmod(self, other) }
434 forward_ref_binop! { impl Rem, rem for $t, $t }
438 rem_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 }
439 rem_float_impl! { f32, fmodf }
440 rem_float_impl! { f64, fmod }
442 /// The `Neg` trait is used to specify the functionality of unary `-`.
446 /// A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
447 /// `neg`, and therefore, `main` prints `Negating!`.
450 /// use std::ops::Neg;
455 /// impl Neg for Foo {
456 /// type Output = Foo;
458 /// fn neg(self) -> Foo {
459 /// println!("Negating!");
469 #[stable(feature = "rust1", since = "1.0.0")]
471 #[stable(feature = "rust1", since = "1.0.0")]
474 /// The method for the unary `-` operator
475 #[stable(feature = "rust1", since = "1.0.0")]
476 fn neg(self) -> Self::Output;
479 macro_rules! neg_impl {
481 #[stable(feature = "rust1", since = "1.0.0")]
483 #[stable(feature = "rust1", since = "1.0.0")]
487 #[stable(feature = "rust1", since = "1.0.0")]
488 fn neg(self) -> $t { -self }
491 forward_ref_unop! { impl Neg, neg for $t }
495 macro_rules! neg_uint_impl {
496 ($t:ty, $t_signed:ty) => {
497 #[stable(feature = "rust1", since = "1.0.0")]
502 fn neg(self) -> $t { -(self as $t_signed) as $t }
505 forward_ref_unop! { impl Neg, neg for $t }
509 neg_impl! { int i8 i16 i32 i64 f32 f64 }
511 neg_uint_impl! { uint, int }
512 neg_uint_impl! { u8, i8 }
513 neg_uint_impl! { u16, i16 }
514 neg_uint_impl! { u32, i32 }
515 neg_uint_impl! { u64, i64 }
518 /// The `Not` trait is used to specify the functionality of unary `!`.
522 /// A trivial implementation of `Not`. When `!Foo` happens, it ends up calling
523 /// `not`, and therefore, `main` prints `Not-ing!`.
526 /// use std::ops::Not;
531 /// impl Not for Foo {
532 /// type Output = Foo;
534 /// fn not(self) -> Foo {
535 /// println!("Not-ing!");
545 #[stable(feature = "rust1", since = "1.0.0")]
547 #[stable(feature = "rust1", since = "1.0.0")]
550 /// The method for the unary `!` operator
551 #[stable(feature = "rust1", since = "1.0.0")]
552 fn not(self) -> Self::Output;
555 macro_rules! not_impl {
557 #[stable(feature = "rust1", since = "1.0.0")]
562 fn not(self) -> $t { !self }
565 forward_ref_unop! { impl Not, not for $t }
569 not_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 }
571 /// The `BitAnd` trait is used to specify the functionality of `&`.
575 /// A trivial implementation of `BitAnd`. When `Foo & Foo` happens, it ends up
576 /// calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`.
579 /// use std::ops::BitAnd;
584 /// impl BitAnd for Foo {
585 /// type Output = Foo;
587 /// fn bitand(self, _rhs: Foo) -> Foo {
588 /// println!("Bitwise And-ing!");
598 #[stable(feature = "rust1", since = "1.0.0")]
599 pub trait BitAnd<RHS=Self> {
600 #[stable(feature = "rust1", since = "1.0.0")]
603 /// The method for the `&` operator
604 #[stable(feature = "rust1", since = "1.0.0")]
605 fn bitand(self, rhs: RHS) -> Self::Output;
608 macro_rules! bitand_impl {
610 #[stable(feature = "rust1", since = "1.0.0")]
615 fn bitand(self, rhs: $t) -> $t { self & rhs }
618 forward_ref_binop! { impl BitAnd, bitand for $t, $t }
622 bitand_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 }
624 /// The `BitOr` trait is used to specify the functionality of `|`.
628 /// A trivial implementation of `BitOr`. When `Foo | Foo` happens, it ends up
629 /// calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`.
632 /// use std::ops::BitOr;
637 /// impl BitOr for Foo {
638 /// type Output = Foo;
640 /// fn bitor(self, _rhs: Foo) -> Foo {
641 /// println!("Bitwise Or-ing!");
651 #[stable(feature = "rust1", since = "1.0.0")]
652 pub trait BitOr<RHS=Self> {
653 #[stable(feature = "rust1", since = "1.0.0")]
656 /// The method for the `|` operator
657 #[stable(feature = "rust1", since = "1.0.0")]
658 fn bitor(self, rhs: RHS) -> Self::Output;
661 macro_rules! bitor_impl {
663 #[stable(feature = "rust1", since = "1.0.0")]
668 fn bitor(self, rhs: $t) -> $t { self | rhs }
671 forward_ref_binop! { impl BitOr, bitor for $t, $t }
675 bitor_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 }
677 /// The `BitXor` trait is used to specify the functionality of `^`.
681 /// A trivial implementation of `BitXor`. When `Foo ^ Foo` happens, it ends up
682 /// calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`.
685 /// use std::ops::BitXor;
690 /// impl BitXor for Foo {
691 /// type Output = Foo;
693 /// fn bitxor(self, _rhs: Foo) -> Foo {
694 /// println!("Bitwise Xor-ing!");
704 #[stable(feature = "rust1", since = "1.0.0")]
705 pub trait BitXor<RHS=Self> {
706 #[stable(feature = "rust1", since = "1.0.0")]
709 /// The method for the `^` operator
710 #[stable(feature = "rust1", since = "1.0.0")]
711 fn bitxor(self, rhs: RHS) -> Self::Output;
714 macro_rules! bitxor_impl {
716 #[stable(feature = "rust1", since = "1.0.0")]
721 fn bitxor(self, other: $t) -> $t { self ^ other }
724 forward_ref_binop! { impl BitXor, bitxor for $t, $t }
728 bitxor_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 }
730 /// The `Shl` trait is used to specify the functionality of `<<`.
734 /// A trivial implementation of `Shl`. When `Foo << Foo` happens, it ends up
735 /// calling `shl`, and therefore, `main` prints `Shifting left!`.
738 /// use std::ops::Shl;
743 /// impl Shl<Foo> for Foo {
744 /// type Output = Foo;
746 /// fn shl(self, _rhs: Foo) -> Foo {
747 /// println!("Shifting left!");
757 #[stable(feature = "rust1", since = "1.0.0")]
759 #[stable(feature = "rust1", since = "1.0.0")]
762 /// The method for the `<<` operator
763 #[stable(feature = "rust1", since = "1.0.0")]
764 fn shl(self, rhs: RHS) -> Self::Output;
767 macro_rules! shl_impl {
769 #[stable(feature = "rust1", since = "1.0.0")]
770 impl Shl<$f> for $t {
774 fn shl(self, other: $f) -> $t {
779 forward_ref_binop! { impl Shl, shl for $t, $f }
783 macro_rules! shl_impl_all {
786 shl_impl! { $t, u16 }
787 shl_impl! { $t, u32 }
788 shl_impl! { $t, u64 }
789 shl_impl! { $t, usize }
792 shl_impl! { $t, i16 }
793 shl_impl! { $t, i32 }
794 shl_impl! { $t, i64 }
795 shl_impl! { $t, isize }
799 shl_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
801 /// The `Shr` trait is used to specify the functionality of `>>`.
805 /// A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
806 /// calling `shr`, and therefore, `main` prints `Shifting right!`.
809 /// use std::ops::Shr;
814 /// impl Shr<Foo> for Foo {
815 /// type Output = Foo;
817 /// fn shr(self, _rhs: Foo) -> Foo {
818 /// println!("Shifting right!");
828 #[stable(feature = "rust1", since = "1.0.0")]
830 #[stable(feature = "rust1", since = "1.0.0")]
833 /// The method for the `>>` operator
834 #[stable(feature = "rust1", since = "1.0.0")]
835 fn shr(self, rhs: RHS) -> Self::Output;
838 macro_rules! shr_impl {
840 impl Shr<$f> for $t {
844 fn shr(self, other: $f) -> $t {
849 forward_ref_binop! { impl Shr, shr for $t, $f }
853 macro_rules! shr_impl_all {
856 shr_impl! { $t, u16 }
857 shr_impl! { $t, u32 }
858 shr_impl! { $t, u64 }
859 shr_impl! { $t, usize }
862 shr_impl! { $t, i16 }
863 shr_impl! { $t, i32 }
864 shr_impl! { $t, i64 }
865 shr_impl! { $t, isize }
869 shr_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
871 /// The `Index` trait is used to specify the functionality of indexing operations
872 /// like `arr[idx]` when used in an immutable context.
876 /// A trivial implementation of `Index`. When `Foo[Bar]` happens, it ends up
877 /// calling `index`, and therefore, `main` prints `Indexing!`.
880 /// use std::ops::Index;
886 /// impl Index<Bar> for Foo {
887 /// type Output = Foo;
889 /// fn index<'a>(&'a self, _index: &Bar) -> &'a Foo {
890 /// println!("Indexing!");
900 #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
901 #[stable(feature = "rust1", since = "1.0.0")]
902 pub trait Index<Idx: ?Sized> {
905 /// The method for the indexing (`Foo[Bar]`) operation
906 #[stable(feature = "rust1", since = "1.0.0")]
907 fn index<'a>(&'a self, index: &Idx) -> &'a Self::Output;
910 /// The `IndexMut` trait is used to specify the functionality of indexing
911 /// operations like `arr[idx]`, when used in a mutable context.
915 /// A trivial implementation of `IndexMut`. When `Foo[Bar]` happens, it ends up
916 /// calling `index_mut`, and therefore, `main` prints `Indexing!`.
919 /// use std::ops::{Index, IndexMut};
925 /// impl Index<Bar> for Foo {
926 /// type Output = Foo;
928 /// fn index<'a>(&'a self, _index: &Bar) -> &'a Foo {
933 /// impl IndexMut<Bar> for Foo {
934 /// fn index_mut<'a>(&'a mut self, _index: &Bar) -> &'a mut Foo {
935 /// println!("Indexing!");
945 #[rustc_on_unimplemented = "the type `{Self}` cannot be mutably indexed by `{Idx}`"]
946 #[stable(feature = "rust1", since = "1.0.0")]
947 pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
948 /// The method for the indexing (`Foo[Bar]`) operation
949 #[stable(feature = "rust1", since = "1.0.0")]
950 fn index_mut<'a>(&'a mut self, index: &Idx) -> &'a mut Self::Output;
953 /// An unbounded range.
954 #[derive(Copy, Clone, PartialEq, Eq)]
956 #[stable(feature = "rust1", since = "1.0.0")]
957 pub struct RangeFull;
959 #[stable(feature = "rust1", since = "1.0.0")]
960 impl fmt::Debug for RangeFull {
961 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
962 fmt::Debug::fmt("..", fmt)
966 /// A (half-open) range which is bounded at both ends.
967 #[derive(Clone, PartialEq, Eq)]
969 #[stable(feature = "rust1", since = "1.0.0")]
970 pub struct Range<Idx> {
971 /// The lower bound of the range (inclusive).
973 /// The upper bound of the range (exclusive).
977 #[stable(feature = "rust1", since = "1.0.0")]
978 impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
979 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
980 write!(fmt, "{:?}..{:?}", self.start, self.end)
984 /// A range which is only bounded below.
985 #[derive(Clone, PartialEq, Eq)]
987 #[stable(feature = "rust1", since = "1.0.0")]
988 pub struct RangeFrom<Idx> {
989 /// The lower bound of the range (inclusive).
995 #[stable(feature = "rust1", since = "1.0.0")]
996 impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
997 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
998 write!(fmt, "{:?}..", self.start)
1002 /// A range which is only bounded above.
1003 #[derive(Copy, Clone, PartialEq, Eq)]
1005 #[stable(feature = "rust1", since = "1.0.0")]
1006 pub struct RangeTo<Idx> {
1007 /// The upper bound of the range (exclusive).
1011 #[stable(feature = "rust1", since = "1.0.0")]
1012 impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
1013 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1014 write!(fmt, "..{:?}", self.end)
1019 /// The `Deref` trait is used to specify the functionality of dereferencing
1020 /// operations like `*v`.
1024 /// A struct with a single field which is accessible via dereferencing the
1028 /// use std::ops::Deref;
1030 /// struct DerefExample<T> {
1034 /// impl<T> Deref for DerefExample<T> {
1035 /// type Target = T;
1037 /// fn deref<'a>(&'a self) -> &'a T {
1043 /// let x = DerefExample { value: 'a' };
1044 /// assert_eq!('a', *x);
1048 #[stable(feature = "rust1", since = "1.0.0")]
1050 #[stable(feature = "rust1", since = "1.0.0")]
1051 type Target: ?Sized;
1053 /// The method called to dereference a value
1054 #[stable(feature = "rust1", since = "1.0.0")]
1055 fn deref<'a>(&'a self) -> &'a Self::Target;
1058 #[stable(feature = "rust1", since = "1.0.0")]
1059 impl<'a, T: ?Sized> Deref for &'a T {
1062 fn deref(&self) -> &T { *self }
1065 #[stable(feature = "rust1", since = "1.0.0")]
1066 impl<'a, T: ?Sized> Deref for &'a mut T {
1069 fn deref(&self) -> &T { *self }
1072 /// The `DerefMut` trait is used to specify the functionality of dereferencing
1073 /// mutably like `*v = 1;`
1077 /// A struct with a single field which is modifiable via dereferencing the
1081 /// use std::ops::{Deref, DerefMut};
1083 /// struct DerefMutExample<T> {
1087 /// impl<T> Deref for DerefMutExample<T> {
1088 /// type Target = T;
1090 /// fn deref<'a>(&'a self) -> &'a T {
1095 /// impl<T> DerefMut for DerefMutExample<T> {
1096 /// fn deref_mut<'a>(&'a mut self) -> &'a mut T {
1102 /// let mut x = DerefMutExample { value: 'a' };
1104 /// assert_eq!('b', *x);
1108 #[stable(feature = "rust1", since = "1.0.0")]
1109 pub trait DerefMut: Deref {
1110 /// The method called to mutably dereference a value
1111 #[stable(feature = "rust1", since = "1.0.0")]
1112 fn deref_mut<'a>(&'a mut self) -> &'a mut Self::Target;
1115 #[stable(feature = "rust1", since = "1.0.0")]
1116 impl<'a, T: ?Sized> DerefMut for &'a mut T {
1117 fn deref_mut(&mut self) -> &mut T { *self }
1120 /// A version of the call operator that takes an immutable receiver.
1122 #[unstable(feature = "core",
1123 reason = "uncertain about variadic generics, input versus associated types")]
1124 #[rustc_paren_sugar]
1125 pub trait Fn<Args> {
1128 /// This is called when the call operator is used.
1129 extern "rust-call" fn call(&self, args: Args) -> Self::Output;
1132 /// A version of the call operator that takes a mutable receiver.
1134 #[unstable(feature = "core",
1135 reason = "uncertain about variadic generics, input versus associated types")]
1136 #[rustc_paren_sugar]
1137 pub trait FnMut<Args> {
1140 /// This is called when the call operator is used.
1141 extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
1144 /// A version of the call operator that takes a by-value receiver.
1146 #[unstable(feature = "core",
1147 reason = "uncertain about variadic generics, input versus associated types")]
1148 #[rustc_paren_sugar]
1149 pub trait FnOnce<Args> {
1152 /// This is called when the call operator is used.
1153 extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
1156 impl<F: ?Sized, A> FnMut<A> for F
1159 type Output = <F as Fn<A>>::Output;
1161 extern "rust-call" fn call_mut(&mut self, args: A) -> <F as Fn<A>>::Output {
1166 impl<F,A> FnOnce<A> for F
1169 type Output = <F as FnMut<A>>::Output;
1171 extern "rust-call" fn call_once(mut self, args: A) -> <F as FnMut<A>>::Output {