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 //! The values for the right hand side of an operator are automatically
17 //! borrowed, so `a + b` is sugar for `a.add(&b)`.
19 //! All of these traits are imported by the prelude, so they are available in
20 //! every Rust program.
24 //! This example creates a `Point` struct that implements `Add` and `Sub`, and then
25 //! demonstrates adding and subtracting two `Point`s.
34 //! impl Add<Point, Point> for Point {
35 //! fn add(&self, other: &Point) -> Point {
36 //! Point {x: self.x + other.x, y: self.y + other.y}
40 //! impl Sub<Point, Point> for Point {
41 //! fn sub(&self, other: &Point) -> Point {
42 //! Point {x: self.x - other.x, y: self.y - other.y}
46 //! println!("{}", Point {x: 1, y: 0} + Point {x: 2, y: 3});
47 //! println!("{}", Point {x: 1, y: 0} - Point {x: 2, y: 3});
51 //! See the documentation for each trait for a minimum implementation that prints
52 //! something to the screen.
56 /// The `Drop` trait is used to run some code when a value goes out of scope. This
57 /// is sometimes called a 'destructor'.
61 /// A trivial implementation of `Drop`. The `drop` method is called when `_x` goes
62 /// out of scope, and therefore `main` prints `Dropping!`.
67 /// impl Drop for HasDrop {
68 /// fn drop(&mut self) {
69 /// println!("Dropping!");
79 /// The `drop` method, called when the value goes out of scope.
83 /// The `Add` trait is used to specify the functionality of `+`.
87 /// A trivial implementation of `Add`. When `Foo + Foo` happens, it ends up
88 /// calling `add`, and therefore, `main` prints `Adding!`.
93 /// impl Copy for Foo {}
95 /// impl Add<Foo, Foo> for Foo {
96 /// fn add(&self, _rhs: &Foo) -> Foo {
97 /// println!("Adding!");
107 pub trait Add<Sized? RHS,Result> for Sized? {
108 /// The method for the `+` operator
109 fn add(&self, rhs: &RHS) -> Result;
112 macro_rules! add_impl(
114 impl Add<$t, $t> for $t {
116 fn add(&self, other: &$t) -> $t { (*self) + (*other) }
121 add_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
123 /// The `Sub` trait is used to specify the functionality of `-`.
127 /// A trivial implementation of `Sub`. When `Foo - Foo` happens, it ends up
128 /// calling `sub`, and therefore, `main` prints `Subtracting!`.
133 /// impl Copy for Foo {}
135 /// impl Sub<Foo, Foo> for Foo {
136 /// fn sub(&self, _rhs: &Foo) -> Foo {
137 /// println!("Subtracting!");
147 pub trait Sub<Sized? RHS, Result> for Sized? {
148 /// The method for the `-` operator
149 fn sub(&self, rhs: &RHS) -> Result;
152 macro_rules! sub_impl(
154 impl Sub<$t, $t> for $t {
156 fn sub(&self, other: &$t) -> $t { (*self) - (*other) }
161 sub_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
163 /// The `Mul` trait is used to specify the functionality of `*`.
167 /// A trivial implementation of `Mul`. When `Foo * Foo` happens, it ends up
168 /// calling `mul`, and therefore, `main` prints `Multiplying!`.
173 /// impl Copy for Foo {}
175 /// impl Mul<Foo, Foo> for Foo {
176 /// fn mul(&self, _rhs: &Foo) -> Foo {
177 /// println!("Multiplying!");
187 pub trait Mul<Sized? RHS, Result> for Sized? {
188 /// The method for the `*` operator
189 fn mul(&self, rhs: &RHS) -> Result;
192 macro_rules! mul_impl(
194 impl Mul<$t, $t> for $t {
196 fn mul(&self, other: &$t) -> $t { (*self) * (*other) }
201 mul_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
203 /// The `Div` trait is used to specify the functionality of `/`.
207 /// A trivial implementation of `Div`. When `Foo / Foo` happens, it ends up
208 /// calling `div`, and therefore, `main` prints `Dividing!`.
213 /// impl Copy for Foo {}
215 /// impl Div<Foo, Foo> for Foo {
216 /// fn div(&self, _rhs: &Foo) -> Foo {
217 /// println!("Dividing!");
227 pub trait Div<Sized? RHS, Result> for Sized? {
228 /// The method for the `/` operator
229 fn div(&self, rhs: &RHS) -> Result;
232 macro_rules! div_impl(
234 impl Div<$t, $t> for $t {
236 fn div(&self, other: &$t) -> $t { (*self) / (*other) }
241 div_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
243 /// The `Rem` trait is used to specify the functionality of `%`.
247 /// A trivial implementation of `Rem`. When `Foo % Foo` happens, it ends up
248 /// calling `rem`, and therefore, `main` prints `Remainder-ing!`.
253 /// impl Copy for Foo {}
255 /// impl Rem<Foo, Foo> for Foo {
256 /// fn rem(&self, _rhs: &Foo) -> Foo {
257 /// println!("Remainder-ing!");
267 pub trait Rem<Sized? RHS, Result> for Sized? {
268 /// The method for the `%` operator
269 fn rem(&self, rhs: &RHS) -> Result;
272 macro_rules! rem_impl(
274 impl Rem<$t, $t> for $t {
276 fn rem(&self, other: &$t) -> $t { (*self) % (*other) }
281 macro_rules! rem_float_impl(
282 ($t:ty, $fmod:ident) => {
283 impl Rem<$t, $t> for $t {
285 fn rem(&self, other: &$t) -> $t {
286 extern { fn $fmod(a: $t, b: $t) -> $t; }
287 unsafe { $fmod(*self, *other) }
293 rem_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
294 rem_float_impl!(f32, fmodf)
295 rem_float_impl!(f64, fmod)
297 /// The `Neg` trait is used to specify the functionality of unary `-`.
301 /// A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
302 /// `neg`, and therefore, `main` prints `Negating!`.
307 /// impl Copy for Foo {}
309 /// impl Neg<Foo> for Foo {
310 /// fn neg(&self) -> Foo {
311 /// println!("Negating!");
321 pub trait Neg<Result> for Sized? {
322 /// The method for the unary `-` operator
323 fn neg(&self) -> Result;
326 macro_rules! neg_impl(
328 impl Neg<$t> for $t {
330 fn neg(&self) -> $t { -*self }
335 macro_rules! neg_uint_impl(
336 ($t:ty, $t_signed:ty) => {
337 impl Neg<$t> for $t {
339 fn neg(&self) -> $t { -(*self as $t_signed) as $t }
344 neg_impl!(int i8 i16 i32 i64 f32 f64)
346 neg_uint_impl!(uint, int)
347 neg_uint_impl!(u8, i8)
348 neg_uint_impl!(u16, i16)
349 neg_uint_impl!(u32, i32)
350 neg_uint_impl!(u64, i64)
353 /// The `Not` trait is used to specify the functionality of unary `!`.
357 /// A trivial implementation of `Not`. When `!Foo` happens, it ends up calling
358 /// `not`, and therefore, `main` prints `Not-ing!`.
363 /// impl Copy for Foo {}
365 /// impl Not<Foo> for Foo {
366 /// fn not(&self) -> Foo {
367 /// println!("Not-ing!");
377 pub trait Not<Result> for Sized? {
378 /// The method for the unary `!` operator
379 fn not(&self) -> Result;
383 macro_rules! not_impl(
385 impl Not<$t> for $t {
387 fn not(&self) -> $t { !*self }
392 not_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
394 /// The `BitAnd` trait is used to specify the functionality of `&`.
398 /// A trivial implementation of `BitAnd`. When `Foo & Foo` happens, it ends up
399 /// calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`.
404 /// impl Copy for Foo {}
406 /// impl BitAnd<Foo, Foo> for Foo {
407 /// fn bitand(&self, _rhs: &Foo) -> Foo {
408 /// println!("Bitwise And-ing!");
418 pub trait BitAnd<Sized? RHS, Result> for Sized? {
419 /// The method for the `&` operator
420 fn bitand(&self, rhs: &RHS) -> Result;
423 macro_rules! bitand_impl(
425 impl BitAnd<$t, $t> for $t {
427 fn bitand(&self, rhs: &$t) -> $t { (*self) & (*rhs) }
432 bitand_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
434 /// The `BitOr` trait is used to specify the functionality of `|`.
438 /// A trivial implementation of `BitOr`. When `Foo | Foo` happens, it ends up
439 /// calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`.
444 /// impl Copy for Foo {}
446 /// impl BitOr<Foo, Foo> for Foo {
447 /// fn bitor(&self, _rhs: &Foo) -> Foo {
448 /// println!("Bitwise Or-ing!");
458 pub trait BitOr<Sized? RHS, Result> for Sized? {
459 /// The method for the `|` operator
460 fn bitor(&self, rhs: &RHS) -> Result;
463 macro_rules! bitor_impl(
465 impl BitOr<$t,$t> for $t {
467 fn bitor(&self, rhs: &$t) -> $t { (*self) | (*rhs) }
472 bitor_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
474 /// The `BitXor` trait is used to specify the functionality of `^`.
478 /// A trivial implementation of `BitXor`. When `Foo ^ Foo` happens, it ends up
479 /// calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`.
484 /// impl Copy for Foo {}
486 /// impl BitXor<Foo, Foo> for Foo {
487 /// fn bitxor(&self, _rhs: &Foo) -> Foo {
488 /// println!("Bitwise Xor-ing!");
498 pub trait BitXor<Sized? RHS, Result> for Sized? {
499 /// The method for the `^` operator
500 fn bitxor(&self, rhs: &RHS) -> Result;
503 macro_rules! bitxor_impl(
505 impl BitXor<$t, $t> for $t {
507 fn bitxor(&self, other: &$t) -> $t { (*self) ^ (*other) }
512 bitxor_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
514 /// The `Shl` trait is used to specify the functionality of `<<`.
518 /// A trivial implementation of `Shl`. When `Foo << Foo` happens, it ends up
519 /// calling `shl`, and therefore, `main` prints `Shifting left!`.
524 /// impl Copy for Foo {}
526 /// impl Shl<Foo, Foo> for Foo {
527 /// fn shl(&self, _rhs: &Foo) -> Foo {
528 /// println!("Shifting left!");
538 pub trait Shl<Sized? RHS, Result> for Sized? {
539 /// The method for the `<<` operator
540 fn shl(&self, rhs: &RHS) -> Result;
543 macro_rules! shl_impl(
545 impl Shl<uint, $t> for $t {
547 fn shl(&self, other: &uint) -> $t {
554 shl_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
556 /// The `Shr` trait is used to specify the functionality of `>>`.
560 /// A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
561 /// calling `shr`, and therefore, `main` prints `Shifting right!`.
566 /// impl Copy for Foo {}
568 /// impl Shr<Foo, Foo> for Foo {
569 /// fn shr(&self, _rhs: &Foo) -> Foo {
570 /// println!("Shifting right!");
580 pub trait Shr<Sized? RHS, Result> for Sized? {
581 /// The method for the `>>` operator
582 fn shr(&self, rhs: &RHS) -> Result;
585 macro_rules! shr_impl(
587 impl Shr<uint, $t> for $t {
589 fn shr(&self, other: &uint) -> $t { (*self) >> (*other) }
594 shr_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
596 /// The `Index` trait is used to specify the functionality of indexing operations
597 /// like `arr[idx]` when used in an immutable context.
601 /// A trivial implementation of `Index`. When `Foo[Foo]` happens, it ends up
602 /// calling `index`, and therefore, `main` prints `Indexing!`.
607 /// impl Copy for Foo {}
609 /// impl Index<Foo, Foo> for Foo {
610 /// fn index<'a>(&'a self, _index: &Foo) -> &'a Foo {
611 /// println!("Indexing!");
621 pub trait Index<Sized? Index, Sized? Result> for Sized? {
622 /// The method for the indexing (`Foo[Bar]`) operation
623 fn index<'a>(&'a self, index: &Index) -> &'a Result;
626 /// The `IndexMut` trait is used to specify the functionality of indexing
627 /// operations like `arr[idx]`, when used in a mutable context.
631 /// A trivial implementation of `IndexMut`. When `Foo[Foo]` happens, it ends up
632 /// calling `index_mut`, and therefore, `main` prints `Indexing!`.
637 /// impl Copy for Foo {}
639 /// impl IndexMut<Foo, Foo> for Foo {
640 /// fn index_mut<'a>(&'a mut self, _index: &Foo) -> &'a mut Foo {
641 /// println!("Indexing!");
651 pub trait IndexMut<Sized? Index, Sized? Result> for Sized? {
652 /// The method for the indexing (`Foo[Bar]`) operation
653 fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Result;
656 /// The `Slice` trait is used to specify the functionality of slicing operations
657 /// like `arr[from..to]` when used in an immutable context.
661 /// A trivial implementation of `Slice`. When `Foo[..Foo]` happens, it ends up
662 /// calling `slice_to`, and therefore, `main` prints `Slicing!`.
667 /// impl Copy for Foo {}
669 /// impl Slice<Foo, Foo> for Foo {
670 /// fn as_slice_<'a>(&'a self) -> &'a Foo {
671 /// println!("Slicing!");
674 /// fn slice_from_or_fail<'a>(&'a self, _from: &Foo) -> &'a Foo {
675 /// println!("Slicing!");
678 /// fn slice_to_or_fail<'a>(&'a self, _to: &Foo) -> &'a Foo {
679 /// println!("Slicing!");
682 /// fn slice_or_fail<'a>(&'a self, _from: &Foo, _to: &Foo) -> &'a Foo {
683 /// println!("Slicing!");
693 pub trait Slice<Sized? Idx, Sized? Result> for Sized? {
694 /// The method for the slicing operation foo[]
695 fn as_slice_<'a>(&'a self) -> &'a Result;
696 /// The method for the slicing operation foo[from..]
697 fn slice_from_or_fail<'a>(&'a self, from: &Idx) -> &'a Result;
698 /// The method for the slicing operation foo[..to]
699 fn slice_to_or_fail<'a>(&'a self, to: &Idx) -> &'a Result;
700 /// The method for the slicing operation foo[from..to]
701 fn slice_or_fail<'a>(&'a self, from: &Idx, to: &Idx) -> &'a Result;
704 /// The `SliceMut` trait is used to specify the functionality of slicing
705 /// operations like `arr[from..to]`, when used in a mutable context.
709 /// A trivial implementation of `SliceMut`. When `Foo[Foo..]` happens, it ends up
710 /// calling `slice_from_mut`, and therefore, `main` prints `Slicing!`.
715 /// impl Copy for Foo {}
717 /// impl SliceMut<Foo, Foo> for Foo {
718 /// fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Foo {
719 /// println!("Slicing!");
722 /// fn slice_from_or_fail_mut<'a>(&'a mut self, _from: &Foo) -> &'a mut Foo {
723 /// println!("Slicing!");
726 /// fn slice_to_or_fail_mut<'a>(&'a mut self, _to: &Foo) -> &'a mut Foo {
727 /// println!("Slicing!");
730 /// fn slice_or_fail_mut<'a>(&'a mut self, _from: &Foo, _to: &Foo) -> &'a mut Foo {
731 /// println!("Slicing!");
741 pub trait SliceMut<Sized? Idx, Sized? Result> for Sized? {
742 /// The method for the slicing operation foo[]
743 fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Result;
744 /// The method for the slicing operation foo[from..]
745 fn slice_from_or_fail_mut<'a>(&'a mut self, from: &Idx) -> &'a mut Result;
746 /// The method for the slicing operation foo[..to]
747 fn slice_to_or_fail_mut<'a>(&'a mut self, to: &Idx) -> &'a mut Result;
748 /// The method for the slicing operation foo[from..to]
749 fn slice_or_fail_mut<'a>(&'a mut self, from: &Idx, to: &Idx) -> &'a mut Result;
752 /// The `Deref` trait is used to specify the functionality of dereferencing
753 /// operations like `*v`.
757 /// A struct with a single field which is accessible via dereferencing the
761 /// struct DerefExample<T> {
765 /// impl<T> Deref<T> for DerefExample<T> {
766 /// fn deref<'a>(&'a self) -> &'a T {
772 /// let x = DerefExample { value: 'a' };
773 /// assert_eq!('a', *x);
777 pub trait Deref<Sized? Result> for Sized? {
778 /// The method called to dereference a value
779 fn deref<'a>(&'a self) -> &'a Result;
782 impl<'a, Sized? T> Deref<T> for &'a T {
783 fn deref(&self) -> &T { *self }
786 impl<'a, Sized? T> Deref<T> for &'a mut T {
787 fn deref(&self) -> &T { *self }
790 /// The `DerefMut` trait is used to specify the functionality of dereferencing
791 /// mutably like `*v = 1;`
795 /// A struct with a single field which is modifiable via dereferencing the
799 /// struct DerefMutExample<T> {
803 /// impl<T> Deref<T> for DerefMutExample<T> {
804 /// fn deref<'a>(&'a self) -> &'a T {
809 /// impl<T> DerefMut<T> for DerefMutExample<T> {
810 /// fn deref_mut<'a>(&'a mut self) -> &'a mut T {
816 /// let mut x = DerefMutExample { value: 'a' };
818 /// assert_eq!('b', *x);
822 pub trait DerefMut<Sized? Result> for Sized? : Deref<Result> {
823 /// The method called to mutably dereference a value
824 fn deref_mut<'a>(&'a mut self) -> &'a mut Result;
827 impl<'a, Sized? T> DerefMut<T> for &'a mut T {
828 fn deref_mut(&mut self) -> &mut T { *self }
831 /// A version of the call operator that takes an immutable receiver.
833 pub trait Fn<Args,Result> for Sized? {
834 /// This is called when the call operator is used.
835 extern "rust-call" fn call(&self, args: Args) -> Result;
838 /// A version of the call operator that takes a mutable receiver.
840 pub trait FnMut<Args,Result> for Sized? {
841 /// This is called when the call operator is used.
842 extern "rust-call" fn call_mut(&mut self, args: Args) -> Result;
845 /// A version of the call operator that takes a by-value receiver.
847 pub trait FnOnce<Args,Result> {
848 /// This is called when the call operator is used.
849 extern "rust-call" fn call_once(self, args: Args) -> Result;
852 impl<Sized? F,A,R> FnMut<A,R> for F
855 extern "rust-call" fn call_mut(&mut self, args: A) -> R {
860 impl<F,A,R> FnOnce<A,R> for F
863 extern "rust-call" fn call_once(mut self, args: A) -> R {
872 impl<Result> Fn<(),Result> for extern "Rust" fn() -> Result {
873 #[allow(non_snake_case)]
874 extern "rust-call" fn call(&self, _args: ()) -> Result {
879 impl<Result,A0> Fn<(A0,),Result> for extern "Rust" fn(A0) -> Result {
880 #[allow(non_snake_case)]
881 extern "rust-call" fn call(&self, args: (A0,)) -> Result {
888 ($($args:ident)*) => (
889 impl<Result$(,$args)*>
890 Fn<($($args,)*),Result>
891 for extern "Rust" fn($($args: $args,)*) -> Result {
892 #[allow(non_snake_case)]
893 extern "rust-call" fn call(&self, args: ($($args,)*)) -> Result {
894 let ($($args,)*) = args;
904 def_fn!(A0 A1 A2 A3 A4)
905 def_fn!(A0 A1 A2 A3 A4 A5)
906 def_fn!(A0 A1 A2 A3 A4 A5 A6)
907 def_fn!(A0 A1 A2 A3 A4 A5 A6 A7)
908 def_fn!(A0 A1 A2 A3 A4 A5 A6 A7 A8)
909 def_fn!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9)
910 def_fn!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10)
911 def_fn!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11)
912 def_fn!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12)
913 def_fn!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13)
914 def_fn!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14)
915 def_fn!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15)