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
33 //! then 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
66 //! prints something to the screen.
68 #![stable(feature = "rust1", since = "1.0.0")]
70 use marker::{Sized, Unsize};
73 /// The `Drop` trait is used to run some code when a value goes out of scope.
74 /// This is sometimes called a 'destructor'.
78 /// A trivial implementation of `Drop`. The `drop` method is called when `_x`
79 /// goes 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 #[stable(feature = "rust1", since = "1.0.0")]
107 impl<'a> $imp for &'a $t {
108 type Output = <$t as $imp>::Output;
111 fn $method(self) -> <$t as $imp>::Output {
118 // implements binary operators "&T op U", "T op &U", "&T op &U"
119 // based on "T op U" where T and U are expected to be `Copy`able
120 macro_rules! forward_ref_binop {
121 (impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
122 #[stable(feature = "rust1", since = "1.0.0")]
123 impl<'a> $imp<$u> for &'a $t {
124 type Output = <$t as $imp<$u>>::Output;
127 fn $method(self, other: $u) -> <$t as $imp<$u>>::Output {
128 $imp::$method(*self, other)
132 #[stable(feature = "rust1", since = "1.0.0")]
133 impl<'a> $imp<&'a $u> for $t {
134 type Output = <$t as $imp<$u>>::Output;
137 fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output {
138 $imp::$method(self, *other)
142 #[stable(feature = "rust1", since = "1.0.0")]
143 impl<'a, 'b> $imp<&'a $u> for &'b $t {
144 type Output = <$t as $imp<$u>>::Output;
147 fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output {
148 $imp::$method(*self, *other)
154 /// The `Add` trait is used to specify the functionality of `+`.
158 /// A trivial implementation of `Add`. When `Foo + Foo` happens, it ends up
159 /// calling `add`, and therefore, `main` prints `Adding!`.
162 /// use std::ops::Add;
164 /// #[derive(Copy, Clone)]
167 /// impl Add for Foo {
168 /// type Output = Foo;
170 /// fn add(self, _rhs: Foo) -> Foo {
171 /// println!("Adding!");
181 #[stable(feature = "rust1", since = "1.0.0")]
182 pub trait Add<RHS=Self> {
183 /// The resulting type after applying the `+` operator
184 #[stable(feature = "rust1", since = "1.0.0")]
187 /// The method for the `+` operator
188 #[stable(feature = "rust1", since = "1.0.0")]
189 fn add(self, rhs: RHS) -> Self::Output;
192 macro_rules! add_impl {
194 #[stable(feature = "rust1", since = "1.0.0")]
199 fn add(self, other: $t) -> $t { self + other }
202 forward_ref_binop! { impl Add, add for $t, $t }
206 add_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
208 /// The `Sub` trait is used to specify the functionality of `-`.
212 /// A trivial implementation of `Sub`. When `Foo - Foo` happens, it ends up
213 /// calling `sub`, and therefore, `main` prints `Subtracting!`.
216 /// use std::ops::Sub;
218 /// #[derive(Copy, Clone)]
221 /// impl Sub for Foo {
222 /// type Output = Foo;
224 /// fn sub(self, _rhs: Foo) -> Foo {
225 /// println!("Subtracting!");
235 #[stable(feature = "rust1", since = "1.0.0")]
236 pub trait Sub<RHS=Self> {
237 /// The resulting type after applying the `-` operator
238 #[stable(feature = "rust1", since = "1.0.0")]
241 /// The method for the `-` operator
242 #[stable(feature = "rust1", since = "1.0.0")]
243 fn sub(self, rhs: RHS) -> Self::Output;
246 macro_rules! sub_impl {
248 #[stable(feature = "rust1", since = "1.0.0")]
253 fn sub(self, other: $t) -> $t { self - other }
256 forward_ref_binop! { impl Sub, sub for $t, $t }
260 sub_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
262 /// The `Mul` trait is used to specify the functionality of `*`.
266 /// A trivial implementation of `Mul`. When `Foo * Foo` happens, it ends up
267 /// calling `mul`, and therefore, `main` prints `Multiplying!`.
270 /// use std::ops::Mul;
272 /// #[derive(Copy, Clone)]
275 /// impl Mul for Foo {
276 /// type Output = Foo;
278 /// fn mul(self, _rhs: Foo) -> Foo {
279 /// println!("Multiplying!");
289 #[stable(feature = "rust1", since = "1.0.0")]
290 pub trait Mul<RHS=Self> {
291 /// The resulting type after applying the `*` operator
292 #[stable(feature = "rust1", since = "1.0.0")]
295 /// The method for the `*` operator
296 #[stable(feature = "rust1", since = "1.0.0")]
297 fn mul(self, rhs: RHS) -> Self::Output;
300 macro_rules! mul_impl {
302 #[stable(feature = "rust1", since = "1.0.0")]
307 fn mul(self, other: $t) -> $t { self * other }
310 forward_ref_binop! { impl Mul, mul for $t, $t }
314 mul_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
316 /// The `Div` trait is used to specify the functionality of `/`.
320 /// A trivial implementation of `Div`. When `Foo / Foo` happens, it ends up
321 /// calling `div`, and therefore, `main` prints `Dividing!`.
324 /// use std::ops::Div;
326 /// #[derive(Copy, Clone)]
329 /// impl Div for Foo {
330 /// type Output = Foo;
332 /// fn div(self, _rhs: Foo) -> Foo {
333 /// println!("Dividing!");
343 #[stable(feature = "rust1", since = "1.0.0")]
344 pub trait Div<RHS=Self> {
345 /// The resulting type after applying the `/` operator
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! { usize u8 u16 u32 u64 isize 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;
380 /// #[derive(Copy, Clone)]
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 /// The resulting type after applying the `%` operator
400 #[stable(feature = "rust1", since = "1.0.0")]
403 /// The method for the `%` operator
404 #[stable(feature = "rust1", since = "1.0.0")]
405 fn rem(self, rhs: RHS) -> Self::Output;
408 macro_rules! rem_impl {
410 #[stable(feature = "rust1", since = "1.0.0")]
415 fn rem(self, other: $t) -> $t { self % other }
418 forward_ref_binop! { impl Rem, rem for $t, $t }
422 macro_rules! rem_float_impl {
423 ($t:ty, $fmod:ident) => {
424 #[stable(feature = "rust1", since = "1.0.0")]
429 fn rem(self, other: $t) -> $t {
430 extern { fn $fmod(a: $t, b: $t) -> $t; }
431 unsafe { $fmod(self, other) }
435 forward_ref_binop! { impl Rem, rem for $t, $t }
439 rem_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
440 rem_float_impl! { f32, fmodf }
441 rem_float_impl! { f64, fmod }
443 /// The `Neg` trait is used to specify the functionality of unary `-`.
447 /// A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
448 /// `neg`, and therefore, `main` prints `Negating!`.
451 /// use std::ops::Neg;
453 /// #[derive(Copy, Clone)]
456 /// impl Neg for Foo {
457 /// type Output = Foo;
459 /// fn neg(self) -> Foo {
460 /// println!("Negating!");
470 #[stable(feature = "rust1", since = "1.0.0")]
472 /// The resulting type after applying the `-` operator
473 #[stable(feature = "rust1", since = "1.0.0")]
476 /// The method for the unary `-` operator
477 #[stable(feature = "rust1", since = "1.0.0")]
478 fn neg(self) -> Self::Output;
483 macro_rules! neg_impl_core {
484 ($id:ident => $body:expr, $($t:ty)*) => ($(
485 #[stable(feature = "rust1", since = "1.0.0")]
486 #[allow(unsigned_negation)]
488 #[stable(feature = "rust1", since = "1.0.0")]
492 #[stable(feature = "rust1", since = "1.0.0")]
493 fn neg(self) -> $t { let $id = self; $body }
496 forward_ref_unop! { impl Neg, neg for $t }
500 macro_rules! neg_impl_numeric {
501 ($($t:ty)*) => { neg_impl_core!{ x => -x, $($t)*} }
504 macro_rules! neg_impl_unsigned {
506 neg_impl_core!{ x => {
511 // neg_impl_unsigned! { usize u8 u16 u32 u64 }
512 neg_impl_numeric! { isize i8 i16 i32 i64 f32 f64 }
514 /// The `Not` trait is used to specify the functionality of unary `!`.
518 /// A trivial implementation of `Not`. When `!Foo` happens, it ends up calling
519 /// `not`, and therefore, `main` prints `Not-ing!`.
522 /// use std::ops::Not;
524 /// #[derive(Copy, Clone)]
527 /// impl Not for Foo {
528 /// type Output = Foo;
530 /// fn not(self) -> Foo {
531 /// println!("Not-ing!");
541 #[stable(feature = "rust1", since = "1.0.0")]
543 /// The resulting type after applying the `!` operator
544 #[stable(feature = "rust1", since = "1.0.0")]
547 /// The method for the unary `!` operator
548 #[stable(feature = "rust1", since = "1.0.0")]
549 fn not(self) -> Self::Output;
552 macro_rules! not_impl {
554 #[stable(feature = "rust1", since = "1.0.0")]
559 fn not(self) -> $t { !self }
562 forward_ref_unop! { impl Not, not for $t }
566 not_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
568 /// The `BitAnd` trait is used to specify the functionality of `&`.
572 /// A trivial implementation of `BitAnd`. When `Foo & Foo` happens, it ends up
573 /// calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`.
576 /// use std::ops::BitAnd;
578 /// #[derive(Copy, Clone)]
581 /// impl BitAnd for Foo {
582 /// type Output = Foo;
584 /// fn bitand(self, _rhs: Foo) -> Foo {
585 /// println!("Bitwise And-ing!");
595 #[stable(feature = "rust1", since = "1.0.0")]
596 pub trait BitAnd<RHS=Self> {
597 /// The resulting type after applying the `&` operator
598 #[stable(feature = "rust1", since = "1.0.0")]
601 /// The method for the `&` operator
602 #[stable(feature = "rust1", since = "1.0.0")]
603 fn bitand(self, rhs: RHS) -> Self::Output;
606 macro_rules! bitand_impl {
608 #[stable(feature = "rust1", since = "1.0.0")]
613 fn bitand(self, rhs: $t) -> $t { self & rhs }
616 forward_ref_binop! { impl BitAnd, bitand for $t, $t }
620 bitand_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
622 /// The `BitOr` trait is used to specify the functionality of `|`.
626 /// A trivial implementation of `BitOr`. When `Foo | Foo` happens, it ends up
627 /// calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`.
630 /// use std::ops::BitOr;
632 /// #[derive(Copy, Clone)]
635 /// impl BitOr for Foo {
636 /// type Output = Foo;
638 /// fn bitor(self, _rhs: Foo) -> Foo {
639 /// println!("Bitwise Or-ing!");
649 #[stable(feature = "rust1", since = "1.0.0")]
650 pub trait BitOr<RHS=Self> {
651 /// The resulting type after applying the `|` operator
652 #[stable(feature = "rust1", since = "1.0.0")]
655 /// The method for the `|` operator
656 #[stable(feature = "rust1", since = "1.0.0")]
657 fn bitor(self, rhs: RHS) -> Self::Output;
660 macro_rules! bitor_impl {
662 #[stable(feature = "rust1", since = "1.0.0")]
667 fn bitor(self, rhs: $t) -> $t { self | rhs }
670 forward_ref_binop! { impl BitOr, bitor for $t, $t }
674 bitor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
676 /// The `BitXor` trait is used to specify the functionality of `^`.
680 /// A trivial implementation of `BitXor`. When `Foo ^ Foo` happens, it ends up
681 /// calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`.
684 /// use std::ops::BitXor;
686 /// #[derive(Copy, Clone)]
689 /// impl BitXor for Foo {
690 /// type Output = Foo;
692 /// fn bitxor(self, _rhs: Foo) -> Foo {
693 /// println!("Bitwise Xor-ing!");
703 #[stable(feature = "rust1", since = "1.0.0")]
704 pub trait BitXor<RHS=Self> {
705 /// The resulting type after applying the `^` operator
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 usize u8 u16 u32 u64 isize 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;
740 /// #[derive(Copy, Clone)]
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 /// The resulting type after applying the `<<` operator
760 #[stable(feature = "rust1", since = "1.0.0")]
763 /// The method for the `<<` operator
764 #[stable(feature = "rust1", since = "1.0.0")]
765 fn shl(self, rhs: RHS) -> Self::Output;
768 macro_rules! shl_impl {
770 #[stable(feature = "rust1", since = "1.0.0")]
771 impl Shl<$f> for $t {
775 fn shl(self, other: $f) -> $t {
780 forward_ref_binop! { impl Shl, shl for $t, $f }
784 macro_rules! shl_impl_all {
787 shl_impl! { $t, u16 }
788 shl_impl! { $t, u32 }
789 shl_impl! { $t, u64 }
790 shl_impl! { $t, usize }
793 shl_impl! { $t, i16 }
794 shl_impl! { $t, i32 }
795 shl_impl! { $t, i64 }
796 shl_impl! { $t, isize }
800 shl_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
802 /// The `Shr` trait is used to specify the functionality of `>>`.
806 /// A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
807 /// calling `shr`, and therefore, `main` prints `Shifting right!`.
810 /// use std::ops::Shr;
812 /// #[derive(Copy, Clone)]
815 /// impl Shr<Foo> for Foo {
816 /// type Output = Foo;
818 /// fn shr(self, _rhs: Foo) -> Foo {
819 /// println!("Shifting right!");
829 #[stable(feature = "rust1", since = "1.0.0")]
831 /// The resulting type after applying the `>>` operator
832 #[stable(feature = "rust1", since = "1.0.0")]
835 /// The method for the `>>` operator
836 #[stable(feature = "rust1", since = "1.0.0")]
837 fn shr(self, rhs: RHS) -> Self::Output;
840 macro_rules! shr_impl {
842 impl Shr<$f> for $t {
846 fn shr(self, other: $f) -> $t {
851 forward_ref_binop! { impl Shr, shr for $t, $f }
855 macro_rules! shr_impl_all {
858 shr_impl! { $t, u16 }
859 shr_impl! { $t, u32 }
860 shr_impl! { $t, u64 }
861 shr_impl! { $t, usize }
864 shr_impl! { $t, i16 }
865 shr_impl! { $t, i32 }
866 shr_impl! { $t, i64 }
867 shr_impl! { $t, isize }
871 shr_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
873 /// The `Index` trait is used to specify the functionality of indexing operations
874 /// like `arr[idx]` when used in an immutable context.
878 /// A trivial implementation of `Index`. When `Foo[Bar]` happens, it ends up
879 /// calling `index`, and therefore, `main` prints `Indexing!`.
882 /// use std::ops::Index;
884 /// #[derive(Copy, Clone)]
888 /// impl Index<Bar> for Foo {
889 /// type Output = Foo;
891 /// fn index<'a>(&'a self, _index: Bar) -> &'a Foo {
892 /// println!("Indexing!");
902 #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
903 #[stable(feature = "rust1", since = "1.0.0")]
904 pub trait Index<Idx: ?Sized> {
905 /// The returned type after indexing
906 #[stable(feature = "rust1", since = "1.0.0")]
909 /// The method for the indexing (`Foo[Bar]`) operation
910 #[stable(feature = "rust1", since = "1.0.0")]
911 fn index<'a>(&'a self, index: Idx) -> &'a Self::Output;
914 /// The `IndexMut` trait is used to specify the functionality of indexing
915 /// operations like `arr[idx]`, when used in a mutable context.
919 /// A trivial implementation of `IndexMut`. When `Foo[Bar]` happens, it ends up
920 /// calling `index_mut`, and therefore, `main` prints `Indexing!`.
923 /// use std::ops::{Index, IndexMut};
925 /// #[derive(Copy, Clone)]
929 /// impl Index<Bar> for Foo {
930 /// type Output = Foo;
932 /// fn index<'a>(&'a self, _index: Bar) -> &'a Foo {
937 /// impl IndexMut<Bar> for Foo {
938 /// fn index_mut<'a>(&'a mut self, _index: Bar) -> &'a mut Foo {
939 /// println!("Indexing!");
948 #[lang = "index_mut"]
949 #[rustc_on_unimplemented = "the type `{Self}` cannot be mutably indexed by `{Idx}`"]
950 #[stable(feature = "rust1", since = "1.0.0")]
951 pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
952 /// The method for the indexing (`Foo[Bar]`) operation
953 #[stable(feature = "rust1", since = "1.0.0")]
954 fn index_mut<'a>(&'a mut self, index: Idx) -> &'a mut Self::Output;
957 /// An unbounded range.
958 #[derive(Copy, Clone, PartialEq, Eq)]
959 #[lang = "range_full"]
960 #[stable(feature = "rust1", since = "1.0.0")]
961 pub struct RangeFull;
963 #[stable(feature = "rust1", since = "1.0.0")]
964 impl fmt::Debug for RangeFull {
965 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
970 /// A (half-open) range which is bounded at both ends.
971 #[derive(Clone, PartialEq, Eq)]
973 #[stable(feature = "rust1", since = "1.0.0")]
974 pub struct Range<Idx> {
975 /// The lower bound of the range (inclusive).
976 #[stable(feature = "rust1", since = "1.0.0")]
978 /// The upper bound of the range (exclusive).
979 #[stable(feature = "rust1", since = "1.0.0")]
983 #[stable(feature = "rust1", since = "1.0.0")]
984 impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
985 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
986 write!(fmt, "{:?}..{:?}", self.start, self.end)
990 /// A range which is only bounded below.
991 #[derive(Clone, PartialEq, Eq)]
992 #[lang = "range_from"]
993 #[stable(feature = "rust1", since = "1.0.0")]
994 pub struct RangeFrom<Idx> {
995 /// The lower bound of the range (inclusive).
996 #[stable(feature = "rust1", since = "1.0.0")]
1000 #[stable(feature = "rust1", since = "1.0.0")]
1001 impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
1002 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1003 write!(fmt, "{:?}..", self.start)
1007 /// A range which is only bounded above.
1008 #[derive(Copy, Clone, PartialEq, Eq)]
1009 #[lang = "range_to"]
1010 #[stable(feature = "rust1", since = "1.0.0")]
1011 pub struct RangeTo<Idx> {
1012 /// The upper bound of the range (exclusive).
1013 #[stable(feature = "rust1", since = "1.0.0")]
1017 #[stable(feature = "rust1", since = "1.0.0")]
1018 impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
1019 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1020 write!(fmt, "..{:?}", self.end)
1024 /// The `Deref` trait is used to specify the functionality of dereferencing
1025 /// operations like `*v`.
1029 /// A struct with a single field which is accessible via dereferencing the
1033 /// use std::ops::Deref;
1035 /// struct DerefExample<T> {
1039 /// impl<T> Deref for DerefExample<T> {
1040 /// type Target = T;
1042 /// fn deref<'a>(&'a self) -> &'a T {
1048 /// let x = DerefExample { value: 'a' };
1049 /// assert_eq!('a', *x);
1053 #[stable(feature = "rust1", since = "1.0.0")]
1055 /// The resulting type after dereferencing
1056 #[stable(feature = "rust1", since = "1.0.0")]
1057 type Target: ?Sized;
1059 /// The method called to dereference a value
1060 #[stable(feature = "rust1", since = "1.0.0")]
1061 fn deref<'a>(&'a self) -> &'a Self::Target;
1064 #[stable(feature = "rust1", since = "1.0.0")]
1065 impl<'a, T: ?Sized> Deref for &'a T {
1068 fn deref(&self) -> &T { *self }
1071 #[stable(feature = "rust1", since = "1.0.0")]
1072 impl<'a, T: ?Sized> Deref for &'a mut T {
1075 fn deref(&self) -> &T { *self }
1078 /// The `DerefMut` trait is used to specify the functionality of dereferencing
1079 /// mutably like `*v = 1;`
1083 /// A struct with a single field which is modifiable via dereferencing the
1087 /// use std::ops::{Deref, DerefMut};
1089 /// struct DerefMutExample<T> {
1093 /// impl<T> Deref for DerefMutExample<T> {
1094 /// type Target = T;
1096 /// fn deref<'a>(&'a self) -> &'a T {
1101 /// impl<T> DerefMut for DerefMutExample<T> {
1102 /// fn deref_mut<'a>(&'a mut self) -> &'a mut T {
1108 /// let mut x = DerefMutExample { value: 'a' };
1110 /// assert_eq!('b', *x);
1113 #[lang = "deref_mut"]
1114 #[stable(feature = "rust1", since = "1.0.0")]
1115 pub trait DerefMut: Deref {
1116 /// The method called to mutably dereference a value
1117 #[stable(feature = "rust1", since = "1.0.0")]
1118 fn deref_mut<'a>(&'a mut self) -> &'a mut Self::Target;
1121 #[stable(feature = "rust1", since = "1.0.0")]
1122 impl<'a, T: ?Sized> DerefMut for &'a mut T {
1123 fn deref_mut(&mut self) -> &mut T { *self }
1126 /// A version of the call operator that takes an immutable receiver.
1128 #[stable(feature = "rust1", since = "1.0.0")]
1129 #[rustc_paren_sugar]
1130 #[fundamental] // so that regex can rely that `&str: !FnMut`
1131 pub trait Fn<Args> : FnMut<Args> {
1132 /// This is called when the call operator is used.
1133 extern "rust-call" fn call(&self, args: Args) -> Self::Output;
1136 /// A version of the call operator that takes a mutable receiver.
1138 #[stable(feature = "rust1", since = "1.0.0")]
1139 #[rustc_paren_sugar]
1140 #[fundamental] // so that regex can rely that `&str: !FnMut`
1141 pub trait FnMut<Args> : FnOnce<Args> {
1142 /// This is called when the call operator is used.
1143 extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
1146 /// A version of the call operator that takes a by-value receiver.
1148 #[stable(feature = "rust1", since = "1.0.0")]
1149 #[rustc_paren_sugar]
1150 #[fundamental] // so that regex can rely that `&str: !FnMut`
1151 pub trait FnOnce<Args> {
1152 /// The returned type after the call operator is used.
1155 /// This is called when the call operator is used.
1156 extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
1161 use super::{Fn, FnMut, FnOnce};
1163 impl<'a,A,F:?Sized> Fn<A> for &'a F
1166 extern "rust-call" fn call(&self, args: A) -> F::Output {
1171 impl<'a,A,F:?Sized> FnMut<A> for &'a F
1174 extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
1179 impl<'a,A,F:?Sized> FnOnce<A> for &'a F
1182 type Output = F::Output;
1184 extern "rust-call" fn call_once(self, args: A) -> F::Output {
1189 impl<'a,A,F:?Sized> FnMut<A> for &'a mut F
1192 extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
1193 (*self).call_mut(args)
1197 impl<'a,A,F:?Sized> FnOnce<A> for &'a mut F
1200 type Output = F::Output;
1201 extern "rust-call" fn call_once(mut self, args: A) -> F::Output {
1202 (*self).call_mut(args)
1207 /// Trait that indicates that this is a pointer or a wrapper for one,
1208 /// where unsizing can be performed on the pointee.
1209 #[unstable(feature = "coerce_unsized")]
1210 #[lang="coerce_unsized"]
1211 pub trait CoerceUnsized<T> {
1216 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
1218 impl<'a, 'b: 'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b mut T {}
1220 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for &'a mut T {}
1221 // &mut T -> *const U
1222 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a mut T {}
1225 impl<'a, 'b: 'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
1227 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a T {}
1230 impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
1231 // *mut T -> *const U
1232 impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T {}
1234 // *const T -> *const U
1235 impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}