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 // So we don't have to document the actual methods on the traits.
12 #[allow(missing_doc)];
16 * Traits for the built-in operators. Implementing these traits allows you to get
17 * an effect similar to overloading operators.
19 * The values for the right hand side of an operator are automatically
20 * borrowed, so `a + b` is sugar for `a.add(&b)`.
22 * All of these traits are imported by the prelude, so they are available in
27 * This example creates a `Point` struct that implements `Add` and `Sub`, and then
28 * 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(format!("{:?}", Point {x: 1, y: 0} + Point {x: 2, y: 3}));
49 * println(format!("{:?}", 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!");
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 fn add(&self, rhs: &RHS) -> Result;
118 * The `Sub` trait is used to specify the functionality of `-`.
122 * A trivial implementation of `Sub`. When `Foo - Foo` happens, it ends up
123 * calling `sub`, and therefore, `main` prints `Subtracting!`.
128 * impl Sub<Foo, Foo> for Foo {
129 * fn sub(&self, _rhs: &Foo) -> Foo {
130 * println("Subtracting!");
141 pub trait Sub<RHS,Result> {
142 fn sub(&self, rhs: &RHS) -> Result;
147 * The `Mul` trait is used to specify the functionality of `*`.
151 * A trivial implementation of `Mul`. When `Foo * Foo` happens, it ends up
152 * calling `mul`, and therefore, `main` prints `Multiplying!`.
157 * impl Mul<Foo, Foo> for Foo {
158 * fn mul(&self, _rhs: &Foo) -> Foo {
159 * println("Multiplying!");
170 pub trait Mul<RHS,Result> {
171 fn mul(&self, rhs: &RHS) -> Result;
176 * The `Div` trait is used to specify the functionality of `/`.
180 * A trivial implementation of `Div`. When `Foo / Foo` happens, it ends up
181 * calling `div`, and therefore, `main` prints `Dividing!`.
186 * impl Div<Foo, Foo> for Foo {
187 * fn div(&self, _rhs: &Foo) -> Foo {
188 * println("Dividing!");
199 pub trait Div<RHS,Result> {
200 fn div(&self, rhs: &RHS) -> Result;
205 * The `Rem` trait is used to specify the functionality of `%`.
209 * A trivial implementation of `Rem`. When `Foo % Foo` happens, it ends up
210 * calling `rem`, and therefore, `main` prints `Remainder-ing!`.
215 * impl Rem<Foo, Foo> for Foo {
216 * fn rem(&self, _rhs: &Foo) -> Foo {
217 * println("Remainder-ing!");
228 pub trait Rem<RHS,Result> {
229 fn rem(&self, rhs: &RHS) -> Result;
234 * The `Neg` trait is used to specify the functionality of unary `-`.
238 * A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
239 * `neg`, and therefore, `main` prints `Negating!`.
244 * impl Neg<Foo> for Foo {
245 * fn neg(&self) -> Foo {
246 * println("Negating!");
257 pub trait Neg<Result> {
258 fn neg(&self) -> Result;
263 * The `Not` trait is used to specify the functionality of unary `!`.
267 * A trivial implementation of `Not`. When `!Foo` happens, it ends up calling
268 * `not`, and therefore, `main` prints `Not-ing!`.
273 * impl Not<Foo> for Foo {
274 * fn not(&self) -> Foo {
275 * println("Not-ing!");
286 pub trait Not<Result> {
287 fn not(&self) -> Result;
292 * The `BitAnd` trait is used to specify the functionality of `&`.
296 * A trivial implementation of `BitAnd`. When `Foo & Foo` happens, it ends up
297 * calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`.
302 * impl BitAnd<Foo, Foo> for Foo {
303 * fn bitand(&self, _rhs: &Foo) -> Foo {
304 * println("Bitwise And-ing!");
315 pub trait BitAnd<RHS,Result> {
316 fn bitand(&self, rhs: &RHS) -> Result;
321 * The `BitOr` trait is used to specify the functionality of `|`.
325 * A trivial implementation of `BitOr`. When `Foo | Foo` happens, it ends up
326 * calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`.
331 * impl BitOr<Foo, Foo> for Foo {
332 * fn bitor(&self, _rhs: &Foo) -> Foo {
333 * println("Bitwise Or-ing!");
344 pub trait BitOr<RHS,Result> {
345 fn bitor(&self, rhs: &RHS) -> Result;
350 * The `BitXor` trait is used to specify the functionality of `^`.
354 * A trivial implementation of `BitXor`. When `Foo ^ Foo` happens, it ends up
355 * calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`.
360 * impl BitXor<Foo, Foo> for Foo {
361 * fn bitxor(&self, _rhs: &Foo) -> Foo {
362 * println("Bitwise Xor-ing!");
373 pub trait BitXor<RHS,Result> {
374 fn bitxor(&self, rhs: &RHS) -> Result;
379 * The `Shl` trait is used to specify the functionality of `<<`.
383 * A trivial implementation of `Shl`. When `Foo << Foo` happens, it ends up
384 * calling `shl`, and therefore, `main` prints `Shifting left!`.
389 * impl Shl<Foo, Foo> for Foo {
390 * fn shl(&self, _rhs: &Foo) -> Foo {
391 * println("Shifting left!");
402 pub trait Shl<RHS,Result> {
403 fn shl(&self, rhs: &RHS) -> Result;
408 * The `Shr` trait is used to specify the functionality of `>>`.
412 * A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
413 * calling `shr`, and therefore, `main` prints `Shifting right!`.
418 * impl Shr<Foo, Foo> for Foo {
419 * fn shr(&self, _rhs: &Foo) -> Foo {
420 * println("Shifting right!");
431 pub trait Shr<RHS,Result> {
432 fn shr(&self, rhs: &RHS) -> Result;
437 * The `Index` trait is used to specify the functionality of indexing operations
442 * A trivial implementation of `Index`. When `Foo[Foo]` happens, it ends up
443 * calling `index`, and therefore, `main` prints `Indexing!`.
448 * impl Index<Foo, Foo> for Foo {
449 * fn index(&self, _rhs: &Foo) -> Foo {
450 * println("Indexing!");
461 pub trait Index<Index,Result> {
462 fn index(&self, index: &Index) -> Result;
468 use extra::test::BenchHarness;
477 impl Drop for HasDtor {
483 fn alloc_obj_with_dtor(bh: &mut BenchHarness) {