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 * Traits representing built-in operators, useful for overloading
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.
35 * impl Add<Point, Point> for Point {
36 * fn add(&self, other: &Point) -> Point {
37 * Point {x: self.x + other.x, y: self.y + other.y}
41 * impl Sub<Point, Point> for Point {
42 * fn sub(&self, other: &Point) -> Point {
43 * Point {x: self.x - other.x, y: self.y - other.y}
47 * println!("{:?}", Point {x: 1, y: 0} + Point {x: 2, y: 3});
48 * println!("{:?}", Point {x: 1, y: 0} - Point {x: 2, y: 3});
52 * See the documentation for each trait for a minimum implementation that prints
53 * something to the screen.
59 * The `Drop` trait is used to run some code when a value goes out of scope. This
60 * is sometimes called a 'destructor'.
64 * A trivial implementation of `Drop`. The `drop` method is called when `_x` goes
65 * out of scope, and therefore `main` prints `Dropping!`.
70 * impl Drop for HasDrop {
71 * fn drop(&mut self) {
72 * println!("Dropping!");
83 /// The `drop` method, called when the value goes out of scope.
89 * The `Add` trait is used to specify the functionality of `+`.
93 * A trivial implementation of `Add`. When `Foo + Foo` happens, it ends up
94 * calling `add`, and therefore, `main` prints `Adding!`.
99 * impl Add<Foo, Foo> for Foo {
100 * fn add(&self, _rhs: &Foo) -> Foo {
101 * println!("Adding!");
112 pub trait Add<RHS,Result> {
113 /// The method for the `+` operator
114 fn add(&self, rhs: &RHS) -> Result;
119 * The `Sub` trait is used to specify the functionality of `-`.
123 * A trivial implementation of `Sub`. When `Foo - Foo` happens, it ends up
124 * calling `sub`, and therefore, `main` prints `Subtracting!`.
129 * impl Sub<Foo, Foo> for Foo {
130 * fn sub(&self, _rhs: &Foo) -> Foo {
131 * println!("Subtracting!");
142 pub trait Sub<RHS,Result> {
143 /// The method for the `-` operator
144 fn sub(&self, rhs: &RHS) -> Result;
149 * The `Mul` trait is used to specify the functionality of `*`.
153 * A trivial implementation of `Mul`. When `Foo * Foo` happens, it ends up
154 * calling `mul`, and therefore, `main` prints `Multiplying!`.
159 * impl Mul<Foo, Foo> for Foo {
160 * fn mul(&self, _rhs: &Foo) -> Foo {
161 * println!("Multiplying!");
172 pub trait Mul<RHS,Result> {
173 /// The method for the `*` operator
174 fn mul(&self, rhs: &RHS) -> Result;
179 * The `Div` trait is used to specify the functionality of `/`.
183 * A trivial implementation of `Div`. When `Foo / Foo` happens, it ends up
184 * calling `div`, and therefore, `main` prints `Dividing!`.
189 * impl Div<Foo, Foo> for Foo {
190 * fn div(&self, _rhs: &Foo) -> Foo {
191 * println!("Dividing!");
202 pub trait Div<RHS,Result> {
203 /// The method for the `/` operator
204 fn div(&self, rhs: &RHS) -> Result;
209 * The `Rem` trait is used to specify the functionality of `%`.
213 * A trivial implementation of `Rem`. When `Foo % Foo` happens, it ends up
214 * calling `rem`, and therefore, `main` prints `Remainder-ing!`.
219 * impl Rem<Foo, Foo> for Foo {
220 * fn rem(&self, _rhs: &Foo) -> Foo {
221 * println!("Remainder-ing!");
232 pub trait Rem<RHS,Result> {
233 /// The method for the `%` operator
234 fn rem(&self, rhs: &RHS) -> Result;
239 * The `Neg` trait is used to specify the functionality of unary `-`.
243 * A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
244 * `neg`, and therefore, `main` prints `Negating!`.
249 * impl Neg<Foo> for Foo {
250 * fn neg(&self) -> Foo {
251 * println!("Negating!");
262 pub trait Neg<Result> {
263 /// The method for the unary `-` operator
264 fn neg(&self) -> Result;
269 * The `Not` trait is used to specify the functionality of unary `!`.
273 * A trivial implementation of `Not`. When `!Foo` happens, it ends up calling
274 * `not`, and therefore, `main` prints `Not-ing!`.
279 * impl Not<Foo> for Foo {
280 * fn not(&self) -> Foo {
281 * println!("Not-ing!");
292 pub trait Not<Result> {
293 /// The method for the unary `!` operator
294 fn not(&self) -> Result;
299 * The `BitAnd` trait is used to specify the functionality of `&`.
303 * A trivial implementation of `BitAnd`. When `Foo & Foo` happens, it ends up
304 * calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`.
309 * impl BitAnd<Foo, Foo> for Foo {
310 * fn bitand(&self, _rhs: &Foo) -> Foo {
311 * println!("Bitwise And-ing!");
322 pub trait BitAnd<RHS,Result> {
323 /// The method for the `&` operator
324 fn bitand(&self, rhs: &RHS) -> Result;
329 * The `BitOr` trait is used to specify the functionality of `|`.
333 * A trivial implementation of `BitOr`. When `Foo | Foo` happens, it ends up
334 * calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`.
339 * impl BitOr<Foo, Foo> for Foo {
340 * fn bitor(&self, _rhs: &Foo) -> Foo {
341 * println!("Bitwise Or-ing!");
352 pub trait BitOr<RHS,Result> {
353 /// The method for the `|` operator
354 fn bitor(&self, rhs: &RHS) -> Result;
359 * The `BitXor` trait is used to specify the functionality of `^`.
363 * A trivial implementation of `BitXor`. When `Foo ^ Foo` happens, it ends up
364 * calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`.
369 * impl BitXor<Foo, Foo> for Foo {
370 * fn bitxor(&self, _rhs: &Foo) -> Foo {
371 * println!("Bitwise Xor-ing!");
382 pub trait BitXor<RHS,Result> {
383 /// The method for the `^` operator
384 fn bitxor(&self, rhs: &RHS) -> Result;
389 * The `Shl` trait is used to specify the functionality of `<<`.
393 * A trivial implementation of `Shl`. When `Foo << Foo` happens, it ends up
394 * calling `shl`, and therefore, `main` prints `Shifting left!`.
399 * impl Shl<Foo, Foo> for Foo {
400 * fn shl(&self, _rhs: &Foo) -> Foo {
401 * println!("Shifting left!");
412 pub trait Shl<RHS,Result> {
413 /// The method for the `<<` operator
414 fn shl(&self, rhs: &RHS) -> Result;
419 * The `Shr` trait is used to specify the functionality of `>>`.
423 * A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
424 * calling `shr`, and therefore, `main` prints `Shifting right!`.
429 * impl Shr<Foo, Foo> for Foo {
430 * fn shr(&self, _rhs: &Foo) -> Foo {
431 * println!("Shifting right!");
442 pub trait Shr<RHS,Result> {
443 /// The method for the `>>` operator
444 fn shr(&self, rhs: &RHS) -> Result;
449 * The `Index` trait is used to specify the functionality of indexing operations
454 * A trivial implementation of `Index`. When `Foo[Foo]` happens, it ends up
455 * calling `index`, and therefore, `main` prints `Indexing!`.
460 * impl Index<Foo, Foo> for Foo {
461 * fn index(&self, _rhs: &Foo) -> Foo {
462 * println!("Indexing!");
473 pub trait Index<Index,Result> {
474 /// The method for the indexing (`Foo[Bar]`) operation
475 fn index(&self, index: &Index) -> Result;
480 * The `Deref` trait is used to specify the functionality of dereferencing
481 * operations like `*v`.
485 * A struct with a single field which is accessible via dereferencing the
489 * struct DerefExample<T> {
493 * impl<T> Deref<T> for DerefExample<T> {
494 * fn deref<'a>(&'a self) -> &'a T {
500 * let x = DerefExample { value: 'a' };
501 * assert_eq!('a', *x);
506 pub trait Deref<Result> {
507 /// The method called to dereference a value
508 fn deref<'a>(&'a self) -> &'a Result;
513 * The `DerefMut` trait is used to specify the functionality of dereferencing
514 * mutably like `*v = 1;`
518 * A struct with a single field which is modifiable via dereferencing the
522 * struct DerefMutExample<T> {
526 * impl<T> Deref<T> for DerefMutExample<T> {
527 * fn deref<'a>(&'a self) -> &'a T {
532 * impl<T> DerefMut<T> for DerefMutExample<T> {
533 * fn deref_mut<'a>(&'a mut self) -> &'a mut T {
539 * let mut x = DerefMutExample { value: 'a' };
541 * assert_eq!('b', *x);
546 pub trait DerefMut<Result>: Deref<Result> {
547 /// The method called to mutably dereference a value
548 fn deref_mut<'a>(&'a mut self) -> &'a mut Result;
554 use self::test::BenchHarness;
563 impl Drop for HasDtor {
569 fn alloc_obj_with_dtor(bh: &mut BenchHarness) {