]> git.lizzy.rs Git - rust.git/blob - src/libcore/ops.rs
Register new snapshots
[rust.git] / src / libcore / ops.rs
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.
4 //
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.
10
11 //! Overloadable operators
12 //!
13 //! Implementing these traits allows you to get an effect similar to
14 //! overloading operators.
15 //!
16 //! The values for the right hand side of an operator are automatically
17 //! borrowed, so `a + b` is sugar for `a.add(&b)`.
18 //!
19 //! All of these traits are imported by the prelude, so they are available in
20 //! every Rust program.
21 //!
22 //! # Example
23 //!
24 //! This example creates a `Point` struct that implements `Add` and `Sub`, and then
25 //! demonstrates adding and subtracting two `Point`s.
26 //!
27 //! ```rust
28 //! #[deriving(Show)]
29 //! struct Point {
30 //!     x: int,
31 //!     y: int
32 //! }
33 //!
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}
37 //!     }
38 //! }
39 //!
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}
43 //!     }
44 //! }
45 //! fn main() {
46 //!     println!("{}", Point {x: 1, y: 0} + Point {x: 2, y: 3});
47 //!     println!("{}", Point {x: 1, y: 0} - Point {x: 2, y: 3});
48 //! }
49 //! ```
50 //!
51 //! See the documentation for each trait for a minimum implementation that prints
52 //! something to the screen.
53
54 use kinds::Sized;
55
56 /// The `Drop` trait is used to run some code when a value goes out of scope. This
57 /// is sometimes called a 'destructor'.
58 ///
59 /// # Example
60 ///
61 /// A trivial implementation of `Drop`. The `drop` method is called when `_x` goes
62 /// out of scope, and therefore `main` prints `Dropping!`.
63 ///
64 /// ```rust
65 /// struct HasDrop;
66 ///
67 /// impl Drop for HasDrop {
68 ///   fn drop(&mut self) {
69 ///       println!("Dropping!");
70 ///   }
71 /// }
72 ///
73 /// fn main() {
74 ///   let _x = HasDrop;
75 /// }
76 /// ```
77 #[lang="drop"]
78 pub trait Drop {
79     /// The `drop` method, called when the value goes out of scope.
80     fn drop(&mut self);
81 }
82
83 /// The `Add` trait is used to specify the functionality of `+`.
84 ///
85 /// # Example
86 ///
87 /// A trivial implementation of `Add`. When `Foo + Foo` happens, it ends up
88 /// calling `add`, and therefore, `main` prints `Adding!`.
89 ///
90 /// ```rust
91 /// struct Foo;
92 ///
93 /// impl Add<Foo, Foo> for Foo {
94 ///     fn add(&self, _rhs: &Foo) -> Foo {
95 ///       println!("Adding!");
96 ///       *self
97 ///   }
98 /// }
99 ///
100 /// fn main() {
101 ///   Foo + Foo;
102 /// }
103 /// ```
104 #[lang="add"]
105 pub trait Add<Sized? RHS,Result> for Sized? {
106     /// The method for the `+` operator
107     fn add(&self, rhs: &RHS) -> Result;
108 }
109
110 macro_rules! add_impl(
111     ($($t:ty)*) => ($(
112         impl Add<$t, $t> for $t {
113             #[inline]
114             fn add(&self, other: &$t) -> $t { (*self) + (*other) }
115         }
116     )*)
117 )
118
119 add_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
120
121 /// The `Sub` trait is used to specify the functionality of `-`.
122 ///
123 /// # Example
124 ///
125 /// A trivial implementation of `Sub`. When `Foo - Foo` happens, it ends up
126 /// calling `sub`, and therefore, `main` prints `Subtracting!`.
127 ///
128 /// ```rust
129 /// struct Foo;
130 ///
131 /// impl Sub<Foo, Foo> for Foo {
132 ///     fn sub(&self, _rhs: &Foo) -> Foo {
133 ///         println!("Subtracting!");
134 ///         *self
135 ///     }
136 /// }
137 ///
138 /// fn main() {
139 ///     Foo - Foo;
140 /// }
141 /// ```
142 #[lang="sub"]
143 pub trait Sub<Sized? RHS, Result> for Sized? {
144     /// The method for the `-` operator
145     fn sub(&self, rhs: &RHS) -> Result;
146 }
147
148 macro_rules! sub_impl(
149     ($($t:ty)*) => ($(
150         impl Sub<$t, $t> for $t {
151             #[inline]
152             fn sub(&self, other: &$t) -> $t { (*self) - (*other) }
153         }
154     )*)
155 )
156
157 sub_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
158
159 /// The `Mul` trait is used to specify the functionality of `*`.
160 ///
161 /// # Example
162 ///
163 /// A trivial implementation of `Mul`. When `Foo * Foo` happens, it ends up
164 /// calling `mul`, and therefore, `main` prints `Multiplying!`.
165 ///
166 /// ```rust
167 /// struct Foo;
168 ///
169 /// impl Mul<Foo, Foo> for Foo {
170 ///     fn mul(&self, _rhs: &Foo) -> Foo {
171 ///         println!("Multiplying!");
172 ///         *self
173 ///     }
174 /// }
175 ///
176 /// fn main() {
177 ///     Foo * Foo;
178 /// }
179 /// ```
180 #[lang="mul"]
181 pub trait Mul<Sized? RHS, Result>  for Sized? {
182     /// The method for the `*` operator
183     fn mul(&self, rhs: &RHS) -> Result;
184 }
185
186 macro_rules! mul_impl(
187     ($($t:ty)*) => ($(
188         impl Mul<$t, $t> for $t {
189             #[inline]
190             fn mul(&self, other: &$t) -> $t { (*self) * (*other) }
191         }
192     )*)
193 )
194
195 mul_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
196
197 /// The `Div` trait is used to specify the functionality of `/`.
198 ///
199 /// # Example
200 ///
201 /// A trivial implementation of `Div`. When `Foo / Foo` happens, it ends up
202 /// calling `div`, and therefore, `main` prints `Dividing!`.
203 ///
204 /// ```
205 /// struct Foo;
206 ///
207 /// impl Div<Foo, Foo> for Foo {
208 ///     fn div(&self, _rhs: &Foo) -> Foo {
209 ///         println!("Dividing!");
210 ///         *self
211 ///     }
212 /// }
213 ///
214 /// fn main() {
215 ///     Foo / Foo;
216 /// }
217 /// ```
218 #[lang="div"]
219 pub trait Div<Sized? RHS, Result> for Sized? {
220     /// The method for the `/` operator
221     fn div(&self, rhs: &RHS) -> Result;
222 }
223
224 macro_rules! div_impl(
225     ($($t:ty)*) => ($(
226         impl Div<$t, $t> for $t {
227             #[inline]
228             fn div(&self, other: &$t) -> $t { (*self) / (*other) }
229         }
230     )*)
231 )
232
233 div_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
234
235 /// The `Rem` trait is used to specify the functionality of `%`.
236 ///
237 /// # Example
238 ///
239 /// A trivial implementation of `Rem`. When `Foo % Foo` happens, it ends up
240 /// calling `rem`, and therefore, `main` prints `Remainder-ing!`.
241 ///
242 /// ```
243 /// struct Foo;
244 ///
245 /// impl Rem<Foo, Foo> for Foo {
246 ///     fn rem(&self, _rhs: &Foo) -> Foo {
247 ///         println!("Remainder-ing!");
248 ///         *self
249 ///     }
250 /// }
251 ///
252 /// fn main() {
253 ///     Foo % Foo;
254 /// }
255 /// ```
256 #[lang="rem"]
257 pub trait Rem<Sized? RHS, Result>  for Sized? {
258     /// The method for the `%` operator
259     fn rem(&self, rhs: &RHS) -> Result;
260 }
261
262 macro_rules! rem_impl(
263     ($($t:ty)*) => ($(
264         impl Rem<$t, $t> for $t {
265             #[inline]
266             fn rem(&self, other: &$t) -> $t { (*self) % (*other) }
267         }
268     )*)
269 )
270
271 macro_rules! rem_float_impl(
272     ($t:ty, $fmod:ident) => {
273         impl Rem<$t, $t> for $t {
274             #[inline]
275             fn rem(&self, other: &$t) -> $t {
276                 extern { fn $fmod(a: $t, b: $t) -> $t; }
277                 unsafe { $fmod(*self, *other) }
278             }
279         }
280     }
281 )
282
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)
286
287 /// The `Neg` trait is used to specify the functionality of unary `-`.
288 ///
289 /// # Example
290 ///
291 /// A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
292 /// `neg`, and therefore, `main` prints `Negating!`.
293 ///
294 /// ```
295 /// struct Foo;
296 ///
297 /// impl Neg<Foo> for Foo {
298 ///     fn neg(&self) -> Foo {
299 ///         println!("Negating!");
300 ///         *self
301 ///     }
302 /// }
303 ///
304 /// fn main() {
305 ///     -Foo;
306 /// }
307 /// ```
308 #[lang="neg"]
309 pub trait Neg<Result> for Sized? {
310     /// The method for the unary `-` operator
311     fn neg(&self) -> Result;
312 }
313
314 macro_rules! neg_impl(
315     ($($t:ty)*) => ($(
316         impl Neg<$t> for $t {
317             #[inline]
318             fn neg(&self) -> $t { -*self }
319         }
320     )*)
321 )
322
323 macro_rules! neg_uint_impl(
324     ($t:ty, $t_signed:ty) => {
325         impl Neg<$t> for $t {
326             #[inline]
327             fn neg(&self) -> $t { -(*self as $t_signed) as $t }
328         }
329     }
330 )
331
332 neg_impl!(int i8 i16 i32 i64 f32 f64)
333
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)
339
340
341 /// The `Not` trait is used to specify the functionality of unary `!`.
342 ///
343 /// # Example
344 ///
345 /// A trivial implementation of `Not`. When `!Foo` happens, it ends up calling
346 /// `not`, and therefore, `main` prints `Not-ing!`.
347 ///
348 /// ```
349 /// struct Foo;
350 ///
351 /// impl Not<Foo> for Foo {
352 ///     fn not(&self) -> Foo {
353 ///         println!("Not-ing!");
354 ///         *self
355 ///     }
356 /// }
357 ///
358 /// fn main() {
359 ///     !Foo;
360 /// }
361 /// ```
362 #[lang="not"]
363 pub trait Not<Result> for Sized? {
364     /// The method for the unary `!` operator
365     fn not(&self) -> Result;
366 }
367
368
369 macro_rules! not_impl(
370     ($($t:ty)*) => ($(
371         impl Not<$t> for $t {
372             #[inline]
373             fn not(&self) -> $t { !*self }
374         }
375     )*)
376 )
377
378 not_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
379
380 /// The `BitAnd` trait is used to specify the functionality of `&`.
381 ///
382 /// # Example
383 ///
384 /// A trivial implementation of `BitAnd`. When `Foo & Foo` happens, it ends up
385 /// calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`.
386 ///
387 /// ```
388 /// struct Foo;
389 ///
390 /// impl BitAnd<Foo, Foo> for Foo {
391 ///     fn bitand(&self, _rhs: &Foo) -> Foo {
392 ///         println!("Bitwise And-ing!");
393 ///         *self
394 ///     }
395 /// }
396 ///
397 /// fn main() {
398 ///     Foo & Foo;
399 /// }
400 /// ```
401 #[lang="bitand"]
402 pub trait BitAnd<Sized? RHS, Result> for Sized? {
403     /// The method for the `&` operator
404     fn bitand(&self, rhs: &RHS) -> Result;
405 }
406
407 macro_rules! bitand_impl(
408     ($($t:ty)*) => ($(
409         impl BitAnd<$t, $t> for $t {
410             #[inline]
411             fn bitand(&self, rhs: &$t) -> $t { (*self) & (*rhs) }
412         }
413     )*)
414 )
415
416 bitand_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
417
418 /// The `BitOr` trait is used to specify the functionality of `|`.
419 ///
420 /// # Example
421 ///
422 /// A trivial implementation of `BitOr`. When `Foo | Foo` happens, it ends up
423 /// calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`.
424 ///
425 /// ```
426 /// struct Foo;
427 ///
428 /// impl BitOr<Foo, Foo> for Foo {
429 ///     fn bitor(&self, _rhs: &Foo) -> Foo {
430 ///         println!("Bitwise Or-ing!");
431 ///         *self
432 ///     }
433 /// }
434 ///
435 /// fn main() {
436 ///     Foo | Foo;
437 /// }
438 /// ```
439 #[lang="bitor"]
440 pub trait BitOr<Sized? RHS, Result> for Sized? {
441     /// The method for the `|` operator
442     fn bitor(&self, rhs: &RHS) -> Result;
443 }
444
445 macro_rules! bitor_impl(
446     ($($t:ty)*) => ($(
447         impl BitOr<$t,$t> for $t {
448             #[inline]
449             fn bitor(&self, rhs: &$t) -> $t { (*self) | (*rhs) }
450         }
451     )*)
452 )
453
454 bitor_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
455
456 /// The `BitXor` trait is used to specify the functionality of `^`.
457 ///
458 /// # Example
459 ///
460 /// A trivial implementation of `BitXor`. When `Foo ^ Foo` happens, it ends up
461 /// calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`.
462 ///
463 /// ```
464 /// struct Foo;
465 ///
466 /// impl BitXor<Foo, Foo> for Foo {
467 ///     fn bitxor(&self, _rhs: &Foo) -> Foo {
468 ///         println!("Bitwise Xor-ing!");
469 ///         *self
470 ///     }
471 /// }
472 ///
473 /// fn main() {
474 ///     Foo ^ Foo;
475 /// }
476 /// ```
477 #[lang="bitxor"]
478 pub trait BitXor<Sized? RHS, Result> for Sized? {
479     /// The method for the `^` operator
480     fn bitxor(&self, rhs: &RHS) -> Result;
481 }
482
483 macro_rules! bitxor_impl(
484     ($($t:ty)*) => ($(
485         impl BitXor<$t, $t> for $t {
486             #[inline]
487             fn bitxor(&self, other: &$t) -> $t { (*self) ^ (*other) }
488         }
489     )*)
490 )
491
492 bitxor_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
493
494 /// The `Shl` trait is used to specify the functionality of `<<`.
495 ///
496 /// # Example
497 ///
498 /// A trivial implementation of `Shl`. When `Foo << Foo` happens, it ends up
499 /// calling `shl`, and therefore, `main` prints `Shifting left!`.
500 ///
501 /// ```
502 /// struct Foo;
503 ///
504 /// impl Shl<Foo, Foo> for Foo {
505 ///     fn shl(&self, _rhs: &Foo) -> Foo {
506 ///         println!("Shifting left!");
507 ///         *self
508 ///     }
509 /// }
510 ///
511 /// fn main() {
512 ///     Foo << Foo;
513 /// }
514 /// ```
515 #[lang="shl"]
516 pub trait Shl<Sized? RHS, Result> for Sized? {
517     /// The method for the `<<` operator
518     fn shl(&self, rhs: &RHS) -> Result;
519 }
520
521 macro_rules! shl_impl(
522     ($($t:ty)*) => ($(
523         impl Shl<uint, $t> for $t {
524             #[inline]
525             fn shl(&self, other: &uint) -> $t {
526                 (*self) << (*other)
527             }
528         }
529     )*)
530 )
531
532 shl_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
533
534 /// The `Shr` trait is used to specify the functionality of `>>`.
535 ///
536 /// # Example
537 ///
538 /// A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
539 /// calling `shr`, and therefore, `main` prints `Shifting right!`.
540 ///
541 /// ```
542 /// struct Foo;
543 ///
544 /// impl Shr<Foo, Foo> for Foo {
545 ///     fn shr(&self, _rhs: &Foo) -> Foo {
546 ///         println!("Shifting right!");
547 ///         *self
548 ///     }
549 /// }
550 ///
551 /// fn main() {
552 ///     Foo >> Foo;
553 /// }
554 /// ```
555 #[lang="shr"]
556 pub trait Shr<Sized? RHS, Result> for Sized? {
557     /// The method for the `>>` operator
558     fn shr(&self, rhs: &RHS) -> Result;
559 }
560
561 macro_rules! shr_impl(
562     ($($t:ty)*) => ($(
563         impl Shr<uint, $t> for $t {
564             #[inline]
565             fn shr(&self, other: &uint) -> $t { (*self) >> (*other) }
566         }
567     )*)
568 )
569
570 shr_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
571
572 /// The `Index` trait is used to specify the functionality of indexing operations
573 /// like `arr[idx]` when used in an immutable context.
574 ///
575 /// # Example
576 ///
577 /// A trivial implementation of `Index`. When `Foo[Foo]` happens, it ends up
578 /// calling `index`, and therefore, `main` prints `Indexing!`.
579 ///
580 /// ```
581 /// struct Foo;
582 ///
583 /// impl Index<Foo, Foo> for Foo {
584 ///     fn index<'a>(&'a self, _index: &Foo) -> &'a Foo {
585 ///         println!("Indexing!");
586 ///         self
587 ///     }
588 /// }
589 ///
590 /// fn main() {
591 ///     Foo[Foo];
592 /// }
593 /// ```
594 #[lang="index"]
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;
598 }
599
600 /// The `IndexMut` trait is used to specify the functionality of indexing
601 /// operations like `arr[idx]`, when used in a mutable context.
602 ///
603 /// # Example
604 ///
605 /// A trivial implementation of `IndexMut`. When `Foo[Foo]` happens, it ends up
606 /// calling `index_mut`, and therefore, `main` prints `Indexing!`.
607 ///
608 /// ```
609 /// struct Foo;
610 ///
611 /// impl IndexMut<Foo, Foo> for Foo {
612 ///     fn index_mut<'a>(&'a mut self, _index: &Foo) -> &'a mut Foo {
613 ///         println!("Indexing!");
614 ///         self
615 ///     }
616 /// }
617 ///
618 /// fn main() {
619 ///     &mut Foo[Foo];
620 /// }
621 /// ```
622 #[lang="index_mut"]
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;
626 }
627
628 /// The `Slice` trait is used to specify the functionality of slicing operations
629 /// like `arr[from..to]` when used in an immutable context.
630 ///
631 /// # Example
632 ///
633 /// A trivial implementation of `Slice`. When `Foo[..Foo]` happens, it ends up
634 /// calling `slice_to`, and therefore, `main` prints `Slicing!`.
635 ///
636 /// ```ignore
637 /// struct Foo;
638 ///
639 /// impl Slice<Foo, Foo> for Foo {
640 ///     fn as_slice_<'a>(&'a self) -> &'a Foo {
641 ///         println!("Slicing!");
642 ///         self
643 ///     }
644 ///     fn slice_from_or_fail<'a>(&'a self, _from: &Foo) -> &'a Foo {
645 ///         println!("Slicing!");
646 ///         self
647 ///     }
648 ///     fn slice_to_or_fail<'a>(&'a self, _to: &Foo) -> &'a Foo {
649 ///         println!("Slicing!");
650 ///         self
651 ///     }
652 ///     fn slice_or_fail<'a>(&'a self, _from: &Foo, _to: &Foo) -> &'a Foo {
653 ///         println!("Slicing!");
654 ///         self
655 ///     }
656 /// }
657 ///
658 /// fn main() {
659 ///     Foo[..Foo];
660 /// }
661 /// ```
662 #[lang="slice"]
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;
672 }
673
674 /// The `SliceMut` trait is used to specify the functionality of slicing
675 /// operations like `arr[from..to]`, when used in a mutable context.
676 ///
677 /// # Example
678 ///
679 /// A trivial implementation of `SliceMut`. When `Foo[Foo..]` happens, it ends up
680 /// calling `slice_from_mut`, and therefore, `main` prints `Slicing!`.
681 ///
682 /// ```ignore
683 /// struct Foo;
684 ///
685 /// impl SliceMut<Foo, Foo> for Foo {
686 ///     fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Foo {
687 ///         println!("Slicing!");
688 ///         self
689 ///     }
690 ///     fn slice_from_or_fail_mut<'a>(&'a mut self, _from: &Foo) -> &'a mut Foo {
691 ///         println!("Slicing!");
692 ///         self
693 ///     }
694 ///     fn slice_to_or_fail_mut<'a>(&'a mut self, _to: &Foo) -> &'a mut Foo {
695 ///         println!("Slicing!");
696 ///         self
697 ///     }
698 ///     fn slice_or_fail_mut<'a>(&'a mut self, _from: &Foo, _to: &Foo) -> &'a mut Foo {
699 ///         println!("Slicing!");
700 ///         self
701 ///     }
702 /// }
703 ///
704 /// pub fn main() {
705 ///     Foo[mut Foo..];
706 /// }
707 /// ```
708 #[lang="slice_mut"]
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;
718 }
719
720 /// The `Deref` trait is used to specify the functionality of dereferencing
721 /// operations like `*v`.
722 ///
723 /// # Example
724 ///
725 /// A struct with a single field which is accessible via dereferencing the
726 /// struct.
727 ///
728 /// ```
729 /// struct DerefExample<T> {
730 ///     value: T
731 /// }
732 ///
733 /// impl<T> Deref<T> for DerefExample<T> {
734 ///     fn deref<'a>(&'a self) -> &'a T {
735 ///         &self.value
736 ///     }
737 /// }
738 ///
739 /// fn main() {
740 ///     let x = DerefExample { value: 'a' };
741 ///     assert_eq!('a', *x);
742 /// }
743 /// ```
744 #[lang="deref"]
745 pub trait Deref<Sized? Result> for Sized? {
746     /// The method called to dereference a value
747     fn deref<'a>(&'a self) -> &'a Result;
748 }
749
750 impl<'a, Sized? T> Deref<T> for &'a T {
751     fn deref(&self) -> &T { *self }
752 }
753
754 impl<'a, Sized? T> Deref<T> for &'a mut T {
755     fn deref(&self) -> &T { *self }
756 }
757
758 /// The `DerefMut` trait is used to specify the functionality of dereferencing
759 /// mutably like `*v = 1;`
760 ///
761 /// # Example
762 ///
763 /// A struct with a single field which is modifiable via dereferencing the
764 /// struct.
765 ///
766 /// ```
767 /// struct DerefMutExample<T> {
768 ///     value: T
769 /// }
770 ///
771 /// impl<T> Deref<T> for DerefMutExample<T> {
772 ///     fn deref<'a>(&'a self) -> &'a T {
773 ///         &self.value
774 ///     }
775 /// }
776 ///
777 /// impl<T> DerefMut<T> for DerefMutExample<T> {
778 ///     fn deref_mut<'a>(&'a mut self) -> &'a mut T {
779 ///         &mut self.value
780 ///     }
781 /// }
782 ///
783 /// fn main() {
784 ///     let mut x = DerefMutExample { value: 'a' };
785 ///     *x = 'b';
786 ///     assert_eq!('b', *x);
787 /// }
788 /// ```
789 #[lang="deref_mut"]
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;
793 }
794
795 impl<'a, Sized? T> DerefMut<T> for &'a mut T {
796     fn deref_mut(&mut self) -> &mut T { *self }
797 }
798
799 /// A version of the call operator that takes an immutable receiver.
800 #[lang="fn"]
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;
804 }
805
806 /// A version of the call operator that takes a mutable receiver.
807 #[lang="fn_mut"]
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;
811 }
812
813 /// A version of the call operator that takes a by-value receiver.
814 #[lang="fn_once"]
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;
818 }
819
820 impl<F,A,R> FnMut<A,R> for F
821     where F : Fn<A,R>
822 {
823     extern "rust-call" fn call_mut(&mut self, args: A) -> R {
824         self.call(args)
825     }
826 }
827
828 impl<F,A,R> FnOnce<A,R> for F
829     where F : FnMut<A,R>
830 {
831     extern "rust-call" fn call_once(mut self, args: A) -> R {
832         self.call_mut(args)
833     }
834 }