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 Add<Foo, Foo> for Foo {
94 /// fn add(&self, _rhs: &Foo) -> Foo {
95 /// println!("Adding!");
105 pub trait Add<Sized? RHS,Result> for Sized? {
106 /// The method for the `+` operator
107 fn add(&self, rhs: &RHS) -> Result;
110 macro_rules! add_impl(
112 impl Add<$t, $t> for $t {
114 fn add(&self, other: &$t) -> $t { (*self) + (*other) }
119 add_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
121 /// The `Sub` trait is used to specify the functionality of `-`.
125 /// A trivial implementation of `Sub`. When `Foo - Foo` happens, it ends up
126 /// calling `sub`, and therefore, `main` prints `Subtracting!`.
131 /// impl Sub<Foo, Foo> for Foo {
132 /// fn sub(&self, _rhs: &Foo) -> Foo {
133 /// println!("Subtracting!");
143 pub trait Sub<Sized? RHS, Result> for Sized? {
144 /// The method for the `-` operator
145 fn sub(&self, rhs: &RHS) -> Result;
148 macro_rules! sub_impl(
150 impl Sub<$t, $t> for $t {
152 fn sub(&self, other: &$t) -> $t { (*self) - (*other) }
157 sub_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
159 /// The `Mul` trait is used to specify the functionality of `*`.
163 /// A trivial implementation of `Mul`. When `Foo * Foo` happens, it ends up
164 /// calling `mul`, and therefore, `main` prints `Multiplying!`.
169 /// impl Mul<Foo, Foo> for Foo {
170 /// fn mul(&self, _rhs: &Foo) -> Foo {
171 /// println!("Multiplying!");
181 pub trait Mul<Sized? RHS, Result> for Sized? {
182 /// The method for the `*` operator
183 fn mul(&self, rhs: &RHS) -> Result;
186 macro_rules! mul_impl(
188 impl Mul<$t, $t> for $t {
190 fn mul(&self, other: &$t) -> $t { (*self) * (*other) }
195 mul_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
197 /// The `Div` trait is used to specify the functionality of `/`.
201 /// A trivial implementation of `Div`. When `Foo / Foo` happens, it ends up
202 /// calling `div`, and therefore, `main` prints `Dividing!`.
207 /// impl Div<Foo, Foo> for Foo {
208 /// fn div(&self, _rhs: &Foo) -> Foo {
209 /// println!("Dividing!");
219 pub trait Div<Sized? RHS, Result> for Sized? {
220 /// The method for the `/` operator
221 fn div(&self, rhs: &RHS) -> Result;
224 macro_rules! div_impl(
226 impl Div<$t, $t> for $t {
228 fn div(&self, other: &$t) -> $t { (*self) / (*other) }
233 div_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
235 /// The `Rem` trait is used to specify the functionality of `%`.
239 /// A trivial implementation of `Rem`. When `Foo % Foo` happens, it ends up
240 /// calling `rem`, and therefore, `main` prints `Remainder-ing!`.
245 /// impl Rem<Foo, Foo> for Foo {
246 /// fn rem(&self, _rhs: &Foo) -> Foo {
247 /// println!("Remainder-ing!");
257 pub trait Rem<Sized? RHS, Result> for Sized? {
258 /// The method for the `%` operator
259 fn rem(&self, rhs: &RHS) -> Result;
262 macro_rules! rem_impl(
264 impl Rem<$t, $t> for $t {
266 fn rem(&self, other: &$t) -> $t { (*self) % (*other) }
271 macro_rules! rem_float_impl(
272 ($t:ty, $fmod:ident) => {
273 impl Rem<$t, $t> for $t {
275 fn rem(&self, other: &$t) -> $t {
276 extern { fn $fmod(a: $t, b: $t) -> $t; }
277 unsafe { $fmod(*self, *other) }
283 rem_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
284 rem_float_impl!(f32, fmodf)
285 rem_float_impl!(f64, fmod)
287 /// The `Neg` trait is used to specify the functionality of unary `-`.
291 /// A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
292 /// `neg`, and therefore, `main` prints `Negating!`.
297 /// impl Neg<Foo> for Foo {
298 /// fn neg(&self) -> Foo {
299 /// println!("Negating!");
309 pub trait Neg<Result> for Sized? {
310 /// The method for the unary `-` operator
311 fn neg(&self) -> Result;
314 macro_rules! neg_impl(
316 impl Neg<$t> for $t {
318 fn neg(&self) -> $t { -*self }
323 macro_rules! neg_uint_impl(
324 ($t:ty, $t_signed:ty) => {
325 impl Neg<$t> for $t {
327 fn neg(&self) -> $t { -(*self as $t_signed) as $t }
332 neg_impl!(int i8 i16 i32 i64 f32 f64)
334 neg_uint_impl!(uint, int)
335 neg_uint_impl!(u8, i8)
336 neg_uint_impl!(u16, i16)
337 neg_uint_impl!(u32, i32)
338 neg_uint_impl!(u64, i64)
341 /// The `Not` trait is used to specify the functionality of unary `!`.
345 /// A trivial implementation of `Not`. When `!Foo` happens, it ends up calling
346 /// `not`, and therefore, `main` prints `Not-ing!`.
351 /// impl Not<Foo> for Foo {
352 /// fn not(&self) -> Foo {
353 /// println!("Not-ing!");
363 pub trait Not<Result> for Sized? {
364 /// The method for the unary `!` operator
365 fn not(&self) -> Result;
369 macro_rules! not_impl(
371 impl Not<$t> for $t {
373 fn not(&self) -> $t { !*self }
378 not_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
380 /// The `BitAnd` trait is used to specify the functionality of `&`.
384 /// A trivial implementation of `BitAnd`. When `Foo & Foo` happens, it ends up
385 /// calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`.
390 /// impl BitAnd<Foo, Foo> for Foo {
391 /// fn bitand(&self, _rhs: &Foo) -> Foo {
392 /// println!("Bitwise And-ing!");
402 pub trait BitAnd<Sized? RHS, Result> for Sized? {
403 /// The method for the `&` operator
404 fn bitand(&self, rhs: &RHS) -> Result;
407 macro_rules! bitand_impl(
409 impl BitAnd<$t, $t> for $t {
411 fn bitand(&self, rhs: &$t) -> $t { (*self) & (*rhs) }
416 bitand_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
418 /// The `BitOr` trait is used to specify the functionality of `|`.
422 /// A trivial implementation of `BitOr`. When `Foo | Foo` happens, it ends up
423 /// calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`.
428 /// impl BitOr<Foo, Foo> for Foo {
429 /// fn bitor(&self, _rhs: &Foo) -> Foo {
430 /// println!("Bitwise Or-ing!");
440 pub trait BitOr<Sized? RHS, Result> for Sized? {
441 /// The method for the `|` operator
442 fn bitor(&self, rhs: &RHS) -> Result;
445 macro_rules! bitor_impl(
447 impl BitOr<$t,$t> for $t {
449 fn bitor(&self, rhs: &$t) -> $t { (*self) | (*rhs) }
454 bitor_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
456 /// The `BitXor` trait is used to specify the functionality of `^`.
460 /// A trivial implementation of `BitXor`. When `Foo ^ Foo` happens, it ends up
461 /// calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`.
466 /// impl BitXor<Foo, Foo> for Foo {
467 /// fn bitxor(&self, _rhs: &Foo) -> Foo {
468 /// println!("Bitwise Xor-ing!");
478 pub trait BitXor<Sized? RHS, Result> for Sized? {
479 /// The method for the `^` operator
480 fn bitxor(&self, rhs: &RHS) -> Result;
483 macro_rules! bitxor_impl(
485 impl BitXor<$t, $t> for $t {
487 fn bitxor(&self, other: &$t) -> $t { (*self) ^ (*other) }
492 bitxor_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
494 /// The `Shl` trait is used to specify the functionality of `<<`.
498 /// A trivial implementation of `Shl`. When `Foo << Foo` happens, it ends up
499 /// calling `shl`, and therefore, `main` prints `Shifting left!`.
504 /// impl Shl<Foo, Foo> for Foo {
505 /// fn shl(&self, _rhs: &Foo) -> Foo {
506 /// println!("Shifting left!");
516 pub trait Shl<Sized? RHS, Result> for Sized? {
517 /// The method for the `<<` operator
518 fn shl(&self, rhs: &RHS) -> Result;
521 macro_rules! shl_impl(
523 impl Shl<uint, $t> for $t {
525 fn shl(&self, other: &uint) -> $t {
532 shl_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
534 /// The `Shr` trait is used to specify the functionality of `>>`.
538 /// A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
539 /// calling `shr`, and therefore, `main` prints `Shifting right!`.
544 /// impl Shr<Foo, Foo> for Foo {
545 /// fn shr(&self, _rhs: &Foo) -> Foo {
546 /// println!("Shifting right!");
556 pub trait Shr<Sized? RHS, Result> for Sized? {
557 /// The method for the `>>` operator
558 fn shr(&self, rhs: &RHS) -> Result;
561 macro_rules! shr_impl(
563 impl Shr<uint, $t> for $t {
565 fn shr(&self, other: &uint) -> $t { (*self) >> (*other) }
570 shr_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
572 /// The `Index` trait is used to specify the functionality of indexing operations
573 /// like `arr[idx]` when used in an immutable context.
577 /// A trivial implementation of `Index`. When `Foo[Foo]` happens, it ends up
578 /// calling `index`, and therefore, `main` prints `Indexing!`.
583 /// impl Index<Foo, Foo> for Foo {
584 /// fn index<'a>(&'a self, _index: &Foo) -> &'a Foo {
585 /// println!("Indexing!");
595 pub trait Index<Sized? Index, Sized? Result> for Sized? {
596 /// The method for the indexing (`Foo[Bar]`) operation
597 fn index<'a>(&'a self, index: &Index) -> &'a Result;
600 /// The `IndexMut` trait is used to specify the functionality of indexing
601 /// operations like `arr[idx]`, when used in a mutable context.
605 /// A trivial implementation of `IndexMut`. When `Foo[Foo]` happens, it ends up
606 /// calling `index_mut`, and therefore, `main` prints `Indexing!`.
611 /// impl IndexMut<Foo, Foo> for Foo {
612 /// fn index_mut<'a>(&'a mut self, _index: &Foo) -> &'a mut Foo {
613 /// println!("Indexing!");
623 pub trait IndexMut<Sized? Index, Sized? Result> for Sized? {
624 /// The method for the indexing (`Foo[Bar]`) operation
625 fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Result;
628 /// The `Slice` trait is used to specify the functionality of slicing operations
629 /// like `arr[from..to]` when used in an immutable context.
633 /// A trivial implementation of `Slice`. When `Foo[..Foo]` happens, it ends up
634 /// calling `slice_to`, and therefore, `main` prints `Slicing!`.
639 /// impl Slice<Foo, Foo> for Foo {
640 /// fn as_slice_<'a>(&'a self) -> &'a Foo {
641 /// println!("Slicing!");
644 /// fn slice_from_or_fail<'a>(&'a self, _from: &Foo) -> &'a Foo {
645 /// println!("Slicing!");
648 /// fn slice_to_or_fail<'a>(&'a self, _to: &Foo) -> &'a Foo {
649 /// println!("Slicing!");
652 /// fn slice_or_fail<'a>(&'a self, _from: &Foo, _to: &Foo) -> &'a Foo {
653 /// println!("Slicing!");
663 pub trait Slice<Sized? Idx, Sized? Result> for Sized? {
664 /// The method for the slicing operation foo[]
665 fn as_slice_<'a>(&'a self) -> &'a Result;
666 /// The method for the slicing operation foo[from..]
667 fn slice_from_or_fail<'a>(&'a self, from: &Idx) -> &'a Result;
668 /// The method for the slicing operation foo[..to]
669 fn slice_to_or_fail<'a>(&'a self, to: &Idx) -> &'a Result;
670 /// The method for the slicing operation foo[from..to]
671 fn slice_or_fail<'a>(&'a self, from: &Idx, to: &Idx) -> &'a Result;
674 /// The `SliceMut` trait is used to specify the functionality of slicing
675 /// operations like `arr[from..to]`, when used in a mutable context.
679 /// A trivial implementation of `SliceMut`. When `Foo[Foo..]` happens, it ends up
680 /// calling `slice_from_mut`, and therefore, `main` prints `Slicing!`.
685 /// impl SliceMut<Foo, Foo> for Foo {
686 /// fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Foo {
687 /// println!("Slicing!");
690 /// fn slice_from_or_fail_mut<'a>(&'a mut self, _from: &Foo) -> &'a mut Foo {
691 /// println!("Slicing!");
694 /// fn slice_to_or_fail_mut<'a>(&'a mut self, _to: &Foo) -> &'a mut Foo {
695 /// println!("Slicing!");
698 /// fn slice_or_fail_mut<'a>(&'a mut self, _from: &Foo, _to: &Foo) -> &'a mut Foo {
699 /// println!("Slicing!");
709 pub trait SliceMut<Sized? Idx, Sized? Result> for Sized? {
710 /// The method for the slicing operation foo[]
711 fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Result;
712 /// The method for the slicing operation foo[from..]
713 fn slice_from_or_fail_mut<'a>(&'a mut self, from: &Idx) -> &'a mut Result;
714 /// The method for the slicing operation foo[..to]
715 fn slice_to_or_fail_mut<'a>(&'a mut self, to: &Idx) -> &'a mut Result;
716 /// The method for the slicing operation foo[from..to]
717 fn slice_or_fail_mut<'a>(&'a mut self, from: &Idx, to: &Idx) -> &'a mut Result;
720 /// The `Deref` trait is used to specify the functionality of dereferencing
721 /// operations like `*v`.
725 /// A struct with a single field which is accessible via dereferencing the
729 /// struct DerefExample<T> {
733 /// impl<T> Deref<T> for DerefExample<T> {
734 /// fn deref<'a>(&'a self) -> &'a T {
740 /// let x = DerefExample { value: 'a' };
741 /// assert_eq!('a', *x);
745 pub trait Deref<Sized? Result> for Sized? {
746 /// The method called to dereference a value
747 fn deref<'a>(&'a self) -> &'a Result;
750 impl<'a, Sized? T> Deref<T> for &'a T {
751 fn deref(&self) -> &T { *self }
754 impl<'a, Sized? T> Deref<T> for &'a mut T {
755 fn deref(&self) -> &T { *self }
758 /// The `DerefMut` trait is used to specify the functionality of dereferencing
759 /// mutably like `*v = 1;`
763 /// A struct with a single field which is modifiable via dereferencing the
767 /// struct DerefMutExample<T> {
771 /// impl<T> Deref<T> for DerefMutExample<T> {
772 /// fn deref<'a>(&'a self) -> &'a T {
777 /// impl<T> DerefMut<T> for DerefMutExample<T> {
778 /// fn deref_mut<'a>(&'a mut self) -> &'a mut T {
784 /// let mut x = DerefMutExample { value: 'a' };
786 /// assert_eq!('b', *x);
790 pub trait DerefMut<Sized? Result> for Sized? : Deref<Result> {
791 /// The method called to mutably dereference a value
792 fn deref_mut<'a>(&'a mut self) -> &'a mut Result;
795 impl<'a, Sized? T> DerefMut<T> for &'a mut T {
796 fn deref_mut(&mut self) -> &mut T { *self }
799 /// A version of the call operator that takes an immutable receiver.
801 pub trait Fn<Args,Result> for Sized? {
802 /// This is called when the call operator is used.
803 extern "rust-call" fn call(&self, args: Args) -> Result;
806 /// A version of the call operator that takes a mutable receiver.
808 pub trait FnMut<Args,Result> for Sized? {
809 /// This is called when the call operator is used.
810 extern "rust-call" fn call_mut(&mut self, args: Args) -> Result;
813 /// A version of the call operator that takes a by-value receiver.
815 pub trait FnOnce<Args,Result> for Sized? {
816 /// This is called when the call operator is used.
817 extern "rust-call" fn call_once(self, args: Args) -> Result;
820 impl<F,A,R> FnMut<A,R> for F
823 extern "rust-call" fn call_mut(&mut self, args: A) -> R {
828 impl<F,A,R> FnOnce<A,R> for F
831 extern "rust-call" fn call_once(mut self, args: A) -> R {