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.
13 * Overloadable operators
15 * Implementing these traits allows you to get an effect similar to
16 * overloading operators.
18 * The values for the right hand side of an operator are automatically
19 * borrowed, so `a + b` is sugar for `a.add(&b)`.
21 * All of these traits are imported by the prelude, so they are available in
26 * This example creates a `Point` struct that implements `Add` and `Sub`, and then
27 * demonstrates adding and subtracting two `Point`s.
36 * impl Add<Point, Point> for Point {
37 * fn add(&self, other: &Point) -> Point {
38 * Point {x: self.x + other.x, y: self.y + other.y}
42 * impl Sub<Point, Point> for Point {
43 * fn sub(&self, other: &Point) -> Point {
44 * Point {x: self.x - other.x, y: self.y - other.y}
48 * println!("{}", Point {x: 1, y: 0} + Point {x: 2, y: 3});
49 * println!("{}", Point {x: 1, y: 0} - Point {x: 2, y: 3});
53 * See the documentation for each trait for a minimum implementation that prints
54 * something to the screen.
60 * The `Drop` trait is used to run some code when a value goes out of scope. This
61 * is sometimes called a 'destructor'.
65 * A trivial implementation of `Drop`. The `drop` method is called when `_x` goes
66 * out of scope, and therefore `main` prints `Dropping!`.
71 * impl Drop for HasDrop {
72 * fn drop(&mut self) {
73 * println!("Dropping!");
84 /// The `drop` method, called when the value goes out of scope.
90 * The `Add` trait is used to specify the functionality of `+`.
94 * A trivial implementation of `Add`. When `Foo + Foo` happens, it ends up
95 * calling `add`, and therefore, `main` prints `Adding!`.
100 * impl Add<Foo, Foo> for Foo {
101 * fn add(&self, _rhs: &Foo) -> Foo {
102 * println!("Adding!");
113 pub trait Add<RHS,Result> {
114 /// The method for the `+` operator
115 fn add(&self, rhs: &RHS) -> Result;
118 macro_rules! add_impl(
121 impl Add<$t, $t> for $t {
123 fn add(&self, other: &$t) -> $t { (*self) + (*other) }
128 add_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
132 * The `Sub` trait is used to specify the functionality of `-`.
136 * A trivial implementation of `Sub`. When `Foo - Foo` happens, it ends up
137 * calling `sub`, and therefore, `main` prints `Subtracting!`.
142 * impl Sub<Foo, Foo> for Foo {
143 * fn sub(&self, _rhs: &Foo) -> Foo {
144 * println!("Subtracting!");
155 pub trait Sub<RHS,Result> {
156 /// The method for the `-` operator
157 fn sub(&self, rhs: &RHS) -> Result;
160 macro_rules! sub_impl(
163 impl Sub<$t, $t> for $t {
165 fn sub(&self, other: &$t) -> $t { (*self) - (*other) }
170 sub_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
174 * The `Mul` trait is used to specify the functionality of `*`.
178 * A trivial implementation of `Mul`. When `Foo * Foo` happens, it ends up
179 * calling `mul`, and therefore, `main` prints `Multiplying!`.
184 * impl Mul<Foo, Foo> for Foo {
185 * fn mul(&self, _rhs: &Foo) -> Foo {
186 * println!("Multiplying!");
197 pub trait Mul<RHS,Result> {
198 /// The method for the `*` operator
199 fn mul(&self, rhs: &RHS) -> Result;
202 macro_rules! mul_impl(
205 impl Mul<$t, $t> for $t {
207 fn mul(&self, other: &$t) -> $t { (*self) * (*other) }
212 mul_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
216 * The `Div` trait is used to specify the functionality of `/`.
220 * A trivial implementation of `Div`. When `Foo / Foo` happens, it ends up
221 * calling `div`, and therefore, `main` prints `Dividing!`.
226 * impl Div<Foo, Foo> for Foo {
227 * fn div(&self, _rhs: &Foo) -> Foo {
228 * println!("Dividing!");
239 pub trait Div<RHS,Result> {
240 /// The method for the `/` operator
241 fn div(&self, rhs: &RHS) -> Result;
244 macro_rules! div_impl(
247 impl Div<$t, $t> for $t {
249 fn div(&self, other: &$t) -> $t { (*self) / (*other) }
254 div_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
258 * The `Rem` trait is used to specify the functionality of `%`.
262 * A trivial implementation of `Rem`. When `Foo % Foo` happens, it ends up
263 * calling `rem`, and therefore, `main` prints `Remainder-ing!`.
268 * impl Rem<Foo, Foo> for Foo {
269 * fn rem(&self, _rhs: &Foo) -> Foo {
270 * println!("Remainder-ing!");
281 pub trait Rem<RHS,Result> {
282 /// The method for the `%` operator
283 fn rem(&self, rhs: &RHS) -> Result;
286 macro_rules! rem_impl(
289 impl Rem<$t, $t> for $t {
291 fn rem(&self, other: &$t) -> $t { (*self) % (*other) }
296 macro_rules! rem_float_impl(
297 ($t:ty, $fmod:ident) => {
299 impl Rem<$t, $t> for $t {
301 fn rem(&self, other: &$t) -> $t {
302 extern { fn $fmod(a: $t, b: $t) -> $t; }
303 unsafe { $fmod(*self, *other) }
309 rem_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
310 rem_float_impl!(f32, fmodf)
311 rem_float_impl!(f64, fmod)
315 * The `Neg` trait is used to specify the functionality of unary `-`.
319 * A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
320 * `neg`, and therefore, `main` prints `Negating!`.
325 * impl Neg<Foo> for Foo {
326 * fn neg(&self) -> Foo {
327 * println!("Negating!");
338 pub trait Neg<Result> {
339 /// The method for the unary `-` operator
340 fn neg(&self) -> Result;
343 macro_rules! neg_impl(
346 impl Neg<$t> for $t {
348 fn neg(&self) -> $t { -*self }
353 macro_rules! neg_uint_impl(
354 ($t:ty, $t_signed:ty) => {
356 impl Neg<$t> for $t {
358 fn neg(&self) -> $t { -(*self as $t_signed) as $t }
363 neg_impl!(int i8 i16 i32 i64 f32 f64)
365 neg_uint_impl!(uint, int)
366 neg_uint_impl!(u8, i8)
367 neg_uint_impl!(u16, i16)
368 neg_uint_impl!(u32, i32)
369 neg_uint_impl!(u64, i64)
374 * The `Not` trait is used to specify the functionality of unary `!`.
378 * A trivial implementation of `Not`. When `!Foo` happens, it ends up calling
379 * `not`, and therefore, `main` prints `Not-ing!`.
384 * impl Not<Foo> for Foo {
385 * fn not(&self) -> Foo {
386 * println!("Not-ing!");
397 pub trait Not<Result> {
398 /// The method for the unary `!` operator
399 fn not(&self) -> Result;
403 macro_rules! not_impl(
406 impl Not<$t> for $t {
408 fn not(&self) -> $t { !*self }
413 not_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
417 * The `BitAnd` trait is used to specify the functionality of `&`.
421 * A trivial implementation of `BitAnd`. When `Foo & Foo` happens, it ends up
422 * calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`.
427 * impl BitAnd<Foo, Foo> for Foo {
428 * fn bitand(&self, _rhs: &Foo) -> Foo {
429 * println!("Bitwise And-ing!");
440 pub trait BitAnd<RHS,Result> {
441 /// The method for the `&` operator
442 fn bitand(&self, rhs: &RHS) -> Result;
445 macro_rules! bitand_impl(
448 impl BitAnd<$t, $t> for $t {
450 fn bitand(&self, rhs: &$t) -> $t { (*self) & (*rhs) }
455 bitand_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
459 * The `BitOr` trait is used to specify the functionality of `|`.
463 * A trivial implementation of `BitOr`. When `Foo | Foo` happens, it ends up
464 * calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`.
469 * impl BitOr<Foo, Foo> for Foo {
470 * fn bitor(&self, _rhs: &Foo) -> Foo {
471 * println!("Bitwise Or-ing!");
482 pub trait BitOr<RHS,Result> {
483 /// The method for the `|` operator
484 fn bitor(&self, rhs: &RHS) -> Result;
487 macro_rules! bitor_impl(
490 impl BitOr<$t,$t> for $t {
492 fn bitor(&self, rhs: &$t) -> $t { (*self) | (*rhs) }
497 bitor_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
501 * The `BitXor` trait is used to specify the functionality of `^`.
505 * A trivial implementation of `BitXor`. When `Foo ^ Foo` happens, it ends up
506 * calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`.
511 * impl BitXor<Foo, Foo> for Foo {
512 * fn bitxor(&self, _rhs: &Foo) -> Foo {
513 * println!("Bitwise Xor-ing!");
524 pub trait BitXor<RHS,Result> {
525 /// The method for the `^` operator
526 fn bitxor(&self, rhs: &RHS) -> Result;
529 macro_rules! bitxor_impl(
532 impl BitXor<$t, $t> for $t {
534 fn bitxor(&self, other: &$t) -> $t { (*self) ^ (*other) }
539 bitxor_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
543 * The `Shl` trait is used to specify the functionality of `<<`.
547 * A trivial implementation of `Shl`. When `Foo << Foo` happens, it ends up
548 * calling `shl`, and therefore, `main` prints `Shifting left!`.
553 * impl Shl<Foo, Foo> for Foo {
554 * fn shl(&self, _rhs: &Foo) -> Foo {
555 * println!("Shifting left!");
566 pub trait Shl<RHS,Result> {
567 /// The method for the `<<` operator
568 fn shl(&self, rhs: &RHS) -> Result;
571 macro_rules! shl_impl(
574 impl Shl<$t, $t> for $t {
576 fn shl(&self, other: &$t) -> $t { (*self) << (*other) }
581 shl_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
585 * The `Shr` trait is used to specify the functionality of `>>`.
589 * A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
590 * calling `shr`, and therefore, `main` prints `Shifting right!`.
595 * impl Shr<Foo, Foo> for Foo {
596 * fn shr(&self, _rhs: &Foo) -> Foo {
597 * println!("Shifting right!");
608 pub trait Shr<RHS,Result> {
609 /// The method for the `>>` operator
610 fn shr(&self, rhs: &RHS) -> Result;
613 macro_rules! shr_impl(
616 impl Shr<$t, $t> for $t {
618 fn shr(&self, other: &$t) -> $t { (*self) >> (*other) }
623 shr_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
627 * The `Index` trait is used to specify the functionality of indexing operations
632 * A trivial implementation of `Index`. When `Foo[Foo]` happens, it ends up
633 * calling `index`, and therefore, `main` prints `Indexing!`.
638 * impl Index<Foo, Foo> for Foo {
639 * fn index(&self, _rhs: &Foo) -> Foo {
640 * println!("Indexing!");
651 pub trait Index<Index,Result> {
652 /// The method for the indexing (`Foo[Bar]`) operation
653 fn index(&self, index: &Index) -> Result;
658 * The `Deref` trait is used to specify the functionality of dereferencing
659 * operations like `*v`.
663 * A struct with a single field which is accessible via dereferencing the
667 * struct DerefExample<T> {
671 * impl<T> Deref<T> for DerefExample<T> {
672 * fn deref<'a>(&'a self) -> &'a T {
678 * let x = DerefExample { value: 'a' };
679 * assert_eq!('a', *x);
684 pub trait Deref<Result> {
685 /// The method called to dereference a value
686 fn deref<'a>(&'a self) -> &'a Result;
691 * The `DerefMut` trait is used to specify the functionality of dereferencing
692 * mutably like `*v = 1;`
696 * A struct with a single field which is modifiable via dereferencing the
700 * struct DerefMutExample<T> {
704 * impl<T> Deref<T> for DerefMutExample<T> {
705 * fn deref<'a>(&'a self) -> &'a T {
710 * impl<T> DerefMut<T> for DerefMutExample<T> {
711 * fn deref_mut<'a>(&'a mut self) -> &'a mut T {
717 * let mut x = DerefMutExample { value: 'a' };
719 * assert_eq!('b', *x);
724 pub trait DerefMut<Result>: Deref<Result> {
725 /// The method called to mutably dereference a value
726 fn deref_mut<'a>(&'a mut self) -> &'a mut Result;
729 /// A version of the call operator that takes an immutable receiver.
731 pub trait Fn<Args,Result> {
732 /// This is called when the call operator is used.
733 fn call(&self, args: Args) -> Result;
736 /// A version of the call operator that takes a mutable receiver.
738 pub trait FnMut<Args,Result> {
739 /// This is called when the call operator is used.
740 fn call_mut(&mut self, args: Args) -> Result;
743 /// A version of the call operator that takes a by-value receiver.
745 pub trait FnOnce<Args,Result> {
746 /// This is called when the call operator is used.
747 fn call_once(self, args: Args) -> Result;
753 use self::test::Bencher;
762 impl Drop for HasDtor {
768 fn alloc_obj_with_dtor(b: &mut Bencher) {