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!`.
94 /// impl Add<Foo, Foo> for Foo {
95 /// fn add(self, _rhs: Foo) -> Foo {
96 /// println!("Adding!");
106 pub trait Add<RHS, Result> {
107 /// The method for the `+` operator
108 fn add(self, rhs: RHS) -> Result;
111 macro_rules! add_impl {
113 impl Add<$t, $t> for $t {
115 fn add(self, other: $t) -> $t { self + other }
120 add_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 }
122 /// The `Sub` trait is used to specify the functionality of `-`.
126 /// A trivial implementation of `Sub`. When `Foo - Foo` happens, it ends up
127 /// calling `sub`, and therefore, `main` prints `Subtracting!`.
130 /// #[deriving(Copy)]
133 /// impl Sub<Foo, Foo> for Foo {
134 /// fn sub(self, _rhs: Foo) -> Foo {
135 /// println!("Subtracting!");
145 pub trait Sub<RHS, Result> {
146 /// The method for the `-` operator
147 fn sub(self, rhs: RHS) -> Result;
150 macro_rules! sub_impl {
152 impl Sub<$t, $t> for $t {
154 fn sub(self, other: $t) -> $t { self - other }
159 sub_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 }
161 /// The `Mul` trait is used to specify the functionality of `*`.
165 /// A trivial implementation of `Mul`. When `Foo * Foo` happens, it ends up
166 /// calling `mul`, and therefore, `main` prints `Multiplying!`.
169 /// #[deriving(Copy)]
172 /// impl Mul<Foo, Foo> for Foo {
173 /// fn mul(self, _rhs: Foo) -> Foo {
174 /// println!("Multiplying!");
184 pub trait Mul<RHS, Result> {
185 /// The method for the `*` operator
186 fn mul(self, rhs: RHS) -> Result;
189 macro_rules! mul_impl {
191 impl Mul<$t, $t> for $t {
193 fn mul(self, other: $t) -> $t { self * other }
198 mul_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 }
200 /// The `Div` trait is used to specify the functionality of `/`.
204 /// A trivial implementation of `Div`. When `Foo / Foo` happens, it ends up
205 /// calling `div`, and therefore, `main` prints `Dividing!`.
208 /// #[deriving(Copy)]
211 /// impl Div<Foo, Foo> for Foo {
212 /// fn div(self, _rhs: Foo) -> Foo {
213 /// println!("Dividing!");
223 pub trait Div<RHS, Result> {
224 /// The method for the `/` operator
225 fn div(self, rhs: RHS) -> Result;
228 macro_rules! div_impl {
230 impl Div<$t, $t> for $t {
232 fn div(self, other: $t) -> $t { self / other }
237 div_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 }
239 /// The `Rem` trait is used to specify the functionality of `%`.
243 /// A trivial implementation of `Rem`. When `Foo % Foo` happens, it ends up
244 /// calling `rem`, and therefore, `main` prints `Remainder-ing!`.
247 /// #[deriving(Copy)]
250 /// impl Rem<Foo, Foo> for Foo {
251 /// fn rem(self, _rhs: Foo) -> Foo {
252 /// println!("Remainder-ing!");
262 pub trait Rem<RHS, Result> {
263 /// The method for the `%` operator
264 fn rem(self, rhs: RHS) -> Result;
267 macro_rules! rem_impl {
269 impl Rem<$t, $t> for $t {
271 fn rem(self, other: $t) -> $t { self % other }
276 macro_rules! rem_float_impl {
277 ($t:ty, $fmod:ident) => {
278 impl Rem<$t, $t> for $t {
280 fn rem(self, other: $t) -> $t {
281 extern { fn $fmod(a: $t, b: $t) -> $t; }
282 unsafe { $fmod(self, other) }
288 rem_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 }
289 rem_float_impl! { f32, fmodf }
290 rem_float_impl! { f64, fmod }
292 /// The `Neg` trait is used to specify the functionality of unary `-`.
296 /// A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
297 /// `neg`, and therefore, `main` prints `Negating!`.
300 /// #[deriving(Copy)]
303 /// impl Neg<Foo> for Foo {
304 /// fn neg(&self) -> Foo {
305 /// println!("Negating!");
314 // NOTE(stage0): Remove trait after a snapshot
317 pub trait Neg<Result> for Sized? {
318 /// The method for the unary `-` operator
319 fn neg(&self) -> Result;
322 // NOTE(stage0): Remove macro after a snapshot
324 macro_rules! neg_impl {
326 impl Neg<$t> for $t {
328 fn neg(&self) -> $t { -*self }
333 // NOTE(stage0): Remove macro after a snapshot
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 /// The `Neg` trait is used to specify the functionality of unary `-`.
348 /// A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
349 /// `neg`, and therefore, `main` prints `Negating!`.
354 /// impl Copy for Foo {}
356 /// impl Neg<Foo> for Foo {
357 /// fn neg(self) -> Foo {
358 /// println!("Negating!");
367 #[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
369 pub trait Neg<Result> {
370 /// The method for the unary `-` operator
371 fn neg(self) -> Result;
374 #[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
375 macro_rules! neg_impl {
377 impl Neg<$t> for $t {
379 fn neg(self) -> $t { -self }
384 #[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
385 macro_rules! neg_uint_impl {
386 ($t:ty, $t_signed:ty) => {
387 impl Neg<$t> for $t {
389 fn neg(self) -> $t { -(self as $t_signed) as $t }
394 neg_impl! { int i8 i16 i32 i64 f32 f64 }
396 neg_uint_impl! { uint, int }
397 neg_uint_impl! { u8, i8 }
398 neg_uint_impl! { u16, i16 }
399 neg_uint_impl! { u32, i32 }
400 neg_uint_impl! { u64, i64 }
403 /// The `Not` trait is used to specify the functionality of unary `!`.
407 /// A trivial implementation of `Not`. When `!Foo` happens, it ends up calling
408 /// `not`, and therefore, `main` prints `Not-ing!`.
411 /// #[deriving(Copy)]
414 /// impl Not<Foo> for Foo {
415 /// fn not(&self) -> Foo {
416 /// println!("Not-ing!");
425 // NOTE(stage0): Remove macro after a snapshot
428 pub trait Not<Result> for Sized? {
429 /// The method for the unary `!` operator
430 fn not(&self) -> Result;
434 // NOTE(stage0): Remove macro after a snapshot
436 macro_rules! not_impl {
438 impl Not<$t> for $t {
440 fn not(&self) -> $t { !*self }
445 /// The `Not` trait is used to specify the functionality of unary `!`.
449 /// A trivial implementation of `Not`. When `!Foo` happens, it ends up calling
450 /// `not`, and therefore, `main` prints `Not-ing!`.
455 /// impl Copy for Foo {}
457 /// impl Not<Foo> for Foo {
458 /// fn not(self) -> Foo {
459 /// println!("Not-ing!");
468 #[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
470 pub trait Not<Result> {
471 /// The method for the unary `!` operator
472 fn not(self) -> Result;
475 #[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
476 macro_rules! not_impl {
478 impl Not<$t> for $t {
480 fn not(self) -> $t { !self }
485 not_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 }
487 /// The `BitAnd` trait is used to specify the functionality of `&`.
491 /// A trivial implementation of `BitAnd`. When `Foo & Foo` happens, it ends up
492 /// calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`.
495 /// #[deriving(Copy)]
498 /// impl BitAnd<Foo, Foo> for Foo {
499 /// fn bitand(self, _rhs: Foo) -> Foo {
500 /// println!("Bitwise And-ing!");
510 pub trait BitAnd<RHS, Result> {
511 /// The method for the `&` operator
512 fn bitand(self, rhs: RHS) -> Result;
515 macro_rules! bitand_impl {
517 impl BitAnd<$t, $t> for $t {
519 fn bitand(self, rhs: $t) -> $t { self & rhs }
524 bitand_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 }
526 /// The `BitOr` trait is used to specify the functionality of `|`.
530 /// A trivial implementation of `BitOr`. When `Foo | Foo` happens, it ends up
531 /// calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`.
534 /// #[deriving(Copy)]
537 /// impl BitOr<Foo, Foo> for Foo {
538 /// fn bitor(self, _rhs: Foo) -> Foo {
539 /// println!("Bitwise Or-ing!");
549 pub trait BitOr<RHS, Result> {
550 /// The method for the `|` operator
551 fn bitor(self, rhs: RHS) -> Result;
554 macro_rules! bitor_impl {
556 impl BitOr<$t,$t> for $t {
558 fn bitor(self, rhs: $t) -> $t { self | rhs }
563 bitor_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 }
565 /// The `BitXor` trait is used to specify the functionality of `^`.
569 /// A trivial implementation of `BitXor`. When `Foo ^ Foo` happens, it ends up
570 /// calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`.
573 /// #[deriving(Copy)]
576 /// impl BitXor<Foo, Foo> for Foo {
577 /// fn bitxor(self, _rhs: Foo) -> Foo {
578 /// println!("Bitwise Xor-ing!");
588 pub trait BitXor<RHS, Result> {
589 /// The method for the `^` operator
590 fn bitxor(self, rhs: RHS) -> Result;
593 macro_rules! bitxor_impl {
595 impl BitXor<$t, $t> for $t {
597 fn bitxor(self, other: $t) -> $t { self ^ other }
602 bitxor_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 }
604 /// The `Shl` trait is used to specify the functionality of `<<`.
608 /// A trivial implementation of `Shl`. When `Foo << Foo` happens, it ends up
609 /// calling `shl`, and therefore, `main` prints `Shifting left!`.
612 /// #[deriving(Copy)]
615 /// impl Shl<Foo, Foo> for Foo {
616 /// fn shl(self, _rhs: Foo) -> Foo {
617 /// println!("Shifting left!");
627 pub trait Shl<RHS, Result> {
628 /// The method for the `<<` operator
629 fn shl(self, rhs: RHS) -> Result;
632 macro_rules! shl_impl {
634 impl Shl<uint, $t> for $t {
636 fn shl(self, other: uint) -> $t {
643 shl_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 }
645 /// The `Shr` trait is used to specify the functionality of `>>`.
649 /// A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
650 /// calling `shr`, and therefore, `main` prints `Shifting right!`.
653 /// #[deriving(Copy)]
656 /// impl Shr<Foo, Foo> for Foo {
657 /// fn shr(self, _rhs: Foo) -> Foo {
658 /// println!("Shifting right!");
668 pub trait Shr<RHS, Result> {
669 /// The method for the `>>` operator
670 fn shr(self, rhs: RHS) -> Result;
673 macro_rules! shr_impl {
675 impl Shr<uint, $t> for $t {
677 fn shr(self, other: uint) -> $t { self >> other }
682 shr_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 }
684 /// The `Index` trait is used to specify the functionality of indexing operations
685 /// like `arr[idx]` when used in an immutable context.
689 /// A trivial implementation of `Index`. When `Foo[Foo]` happens, it ends up
690 /// calling `index`, and therefore, `main` prints `Indexing!`.
693 /// #[deriving(Copy)]
696 /// impl Index<Foo, Foo> for Foo {
697 /// fn index<'a>(&'a self, _index: &Foo) -> &'a Foo {
698 /// println!("Indexing!");
708 pub trait Index<Sized? Index, Sized? Result> for Sized? {
709 /// The method for the indexing (`Foo[Bar]`) operation
710 fn index<'a>(&'a self, index: &Index) -> &'a Result;
713 /// The `IndexMut` trait is used to specify the functionality of indexing
714 /// operations like `arr[idx]`, when used in a mutable context.
718 /// A trivial implementation of `IndexMut`. When `Foo[Foo]` happens, it ends up
719 /// calling `index_mut`, and therefore, `main` prints `Indexing!`.
722 /// #[deriving(Copy)]
725 /// impl IndexMut<Foo, Foo> for Foo {
726 /// fn index_mut<'a>(&'a mut self, _index: &Foo) -> &'a mut Foo {
727 /// println!("Indexing!");
737 pub trait IndexMut<Sized? Index, Sized? Result> for Sized? {
738 /// The method for the indexing (`Foo[Bar]`) operation
739 fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Result;
742 /// The `Slice` trait is used to specify the functionality of slicing operations
743 /// like `arr[from..to]` when used in an immutable context.
747 /// A trivial implementation of `Slice`. When `Foo[..Foo]` happens, it ends up
748 /// calling `slice_to`, and therefore, `main` prints `Slicing!`.
751 /// #[deriving(Copy)]
754 /// impl Slice<Foo, Foo> for Foo {
755 /// fn as_slice_<'a>(&'a self) -> &'a Foo {
756 /// println!("Slicing!");
759 /// fn slice_from_or_fail<'a>(&'a self, _from: &Foo) -> &'a Foo {
760 /// println!("Slicing!");
763 /// fn slice_to_or_fail<'a>(&'a self, _to: &Foo) -> &'a Foo {
764 /// println!("Slicing!");
767 /// fn slice_or_fail<'a>(&'a self, _from: &Foo, _to: &Foo) -> &'a Foo {
768 /// println!("Slicing!");
778 pub trait Slice<Sized? Idx, Sized? Result> for Sized? {
779 /// The method for the slicing operation foo[]
780 fn as_slice_<'a>(&'a self) -> &'a Result;
781 /// The method for the slicing operation foo[from..]
782 fn slice_from_or_fail<'a>(&'a self, from: &Idx) -> &'a Result;
783 /// The method for the slicing operation foo[..to]
784 fn slice_to_or_fail<'a>(&'a self, to: &Idx) -> &'a Result;
785 /// The method for the slicing operation foo[from..to]
786 fn slice_or_fail<'a>(&'a self, from: &Idx, to: &Idx) -> &'a Result;
789 /// The `SliceMut` trait is used to specify the functionality of slicing
790 /// operations like `arr[from..to]`, when used in a mutable context.
794 /// A trivial implementation of `SliceMut`. When `Foo[Foo..]` happens, it ends up
795 /// calling `slice_from_mut`, and therefore, `main` prints `Slicing!`.
798 /// #[deriving(Copy)]
801 /// impl SliceMut<Foo, Foo> for Foo {
802 /// fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Foo {
803 /// println!("Slicing!");
806 /// fn slice_from_or_fail_mut<'a>(&'a mut self, _from: &Foo) -> &'a mut Foo {
807 /// println!("Slicing!");
810 /// fn slice_to_or_fail_mut<'a>(&'a mut self, _to: &Foo) -> &'a mut Foo {
811 /// println!("Slicing!");
814 /// fn slice_or_fail_mut<'a>(&'a mut self, _from: &Foo, _to: &Foo) -> &'a mut Foo {
815 /// println!("Slicing!");
825 pub trait SliceMut<Sized? Idx, Sized? Result> for Sized? {
826 /// The method for the slicing operation foo[]
827 fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Result;
828 /// The method for the slicing operation foo[from..]
829 fn slice_from_or_fail_mut<'a>(&'a mut self, from: &Idx) -> &'a mut Result;
830 /// The method for the slicing operation foo[..to]
831 fn slice_to_or_fail_mut<'a>(&'a mut self, to: &Idx) -> &'a mut Result;
832 /// The method for the slicing operation foo[from..to]
833 fn slice_or_fail_mut<'a>(&'a mut self, from: &Idx, to: &Idx) -> &'a mut Result;
836 /// The `Deref` trait is used to specify the functionality of dereferencing
837 /// operations like `*v`.
841 /// A struct with a single field which is accessible via dereferencing the
845 /// struct DerefExample<T> {
849 /// impl<T> Deref<T> for DerefExample<T> {
850 /// fn deref<'a>(&'a self) -> &'a T {
856 /// let x = DerefExample { value: 'a' };
857 /// assert_eq!('a', *x);
861 pub trait Deref<Sized? Result> for Sized? {
862 /// The method called to dereference a value
863 fn deref<'a>(&'a self) -> &'a Result;
866 impl<'a, Sized? T> Deref<T> for &'a T {
867 fn deref(&self) -> &T { *self }
870 impl<'a, Sized? T> Deref<T> for &'a mut T {
871 fn deref(&self) -> &T { *self }
874 /// The `DerefMut` trait is used to specify the functionality of dereferencing
875 /// mutably like `*v = 1;`
879 /// A struct with a single field which is modifiable via dereferencing the
883 /// struct DerefMutExample<T> {
887 /// impl<T> Deref<T> for DerefMutExample<T> {
888 /// fn deref<'a>(&'a self) -> &'a T {
893 /// impl<T> DerefMut<T> for DerefMutExample<T> {
894 /// fn deref_mut<'a>(&'a mut self) -> &'a mut T {
900 /// let mut x = DerefMutExample { value: 'a' };
902 /// assert_eq!('b', *x);
906 pub trait DerefMut<Sized? Result> for Sized? : Deref<Result> {
907 /// The method called to mutably dereference a value
908 fn deref_mut<'a>(&'a mut self) -> &'a mut Result;
911 impl<'a, Sized? T> DerefMut<T> for &'a mut T {
912 fn deref_mut(&mut self) -> &mut T { *self }
915 /// A version of the call operator that takes an immutable receiver.
917 pub trait Fn<Args,Result> for Sized? {
918 /// This is called when the call operator is used.
919 extern "rust-call" fn call(&self, args: Args) -> Result;
922 /// A version of the call operator that takes a mutable receiver.
924 pub trait FnMut<Args,Result> for Sized? {
925 /// This is called when the call operator is used.
926 extern "rust-call" fn call_mut(&mut self, args: Args) -> Result;
929 /// A version of the call operator that takes a by-value receiver.
931 pub trait FnOnce<Args,Result> {
932 /// This is called when the call operator is used.
933 extern "rust-call" fn call_once(self, args: Args) -> Result;
936 impl<Sized? F,A,R> FnMut<A,R> for F
939 extern "rust-call" fn call_mut(&mut self, args: A) -> R {
944 impl<F,A,R> FnOnce<A,R> for F
947 extern "rust-call" fn call_once(mut self, args: A) -> R {