]> git.lizzy.rs Git - rust.git/blob - src/libcore/ops.rs
auto merge of #19628 : jbranchaud/rust/add-string-as-string-doctest, r=steveklabnik
[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 Copy for Foo {}
94 ///
95 /// impl Add<Foo, Foo> for Foo {
96 ///     fn add(&self, _rhs: &Foo) -> Foo {
97 ///       println!("Adding!");
98 ///       *self
99 ///   }
100 /// }
101 ///
102 /// fn main() {
103 ///   Foo + Foo;
104 /// }
105 /// ```
106 #[lang="add"]
107 pub trait Add<Sized? RHS,Result> for Sized? {
108     /// The method for the `+` operator
109     fn add(&self, rhs: &RHS) -> Result;
110 }
111
112 macro_rules! add_impl(
113     ($($t:ty)*) => ($(
114         impl Add<$t, $t> for $t {
115             #[inline]
116             fn add(&self, other: &$t) -> $t { (*self) + (*other) }
117         }
118     )*)
119 )
120
121 add_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
122
123 /// The `Sub` trait is used to specify the functionality of `-`.
124 ///
125 /// # Example
126 ///
127 /// A trivial implementation of `Sub`. When `Foo - Foo` happens, it ends up
128 /// calling `sub`, and therefore, `main` prints `Subtracting!`.
129 ///
130 /// ```rust
131 /// struct Foo;
132 ///
133 /// impl Copy for Foo {}
134 ///
135 /// impl Sub<Foo, Foo> for Foo {
136 ///     fn sub(&self, _rhs: &Foo) -> Foo {
137 ///         println!("Subtracting!");
138 ///         *self
139 ///     }
140 /// }
141 ///
142 /// fn main() {
143 ///     Foo - Foo;
144 /// }
145 /// ```
146 #[lang="sub"]
147 pub trait Sub<Sized? RHS, Result> for Sized? {
148     /// The method for the `-` operator
149     fn sub(&self, rhs: &RHS) -> Result;
150 }
151
152 macro_rules! sub_impl(
153     ($($t:ty)*) => ($(
154         impl Sub<$t, $t> for $t {
155             #[inline]
156             fn sub(&self, other: &$t) -> $t { (*self) - (*other) }
157         }
158     )*)
159 )
160
161 sub_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
162
163 /// The `Mul` trait is used to specify the functionality of `*`.
164 ///
165 /// # Example
166 ///
167 /// A trivial implementation of `Mul`. When `Foo * Foo` happens, it ends up
168 /// calling `mul`, and therefore, `main` prints `Multiplying!`.
169 ///
170 /// ```rust
171 /// struct Foo;
172 ///
173 /// impl Copy for Foo {}
174 ///
175 /// impl Mul<Foo, Foo> for Foo {
176 ///     fn mul(&self, _rhs: &Foo) -> Foo {
177 ///         println!("Multiplying!");
178 ///         *self
179 ///     }
180 /// }
181 ///
182 /// fn main() {
183 ///     Foo * Foo;
184 /// }
185 /// ```
186 #[lang="mul"]
187 pub trait Mul<Sized? RHS, Result>  for Sized? {
188     /// The method for the `*` operator
189     fn mul(&self, rhs: &RHS) -> Result;
190 }
191
192 macro_rules! mul_impl(
193     ($($t:ty)*) => ($(
194         impl Mul<$t, $t> for $t {
195             #[inline]
196             fn mul(&self, other: &$t) -> $t { (*self) * (*other) }
197         }
198     )*)
199 )
200
201 mul_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
202
203 /// The `Div` trait is used to specify the functionality of `/`.
204 ///
205 /// # Example
206 ///
207 /// A trivial implementation of `Div`. When `Foo / Foo` happens, it ends up
208 /// calling `div`, and therefore, `main` prints `Dividing!`.
209 ///
210 /// ```
211 /// struct Foo;
212 ///
213 /// impl Copy for Foo {}
214 ///
215 /// impl Div<Foo, Foo> for Foo {
216 ///     fn div(&self, _rhs: &Foo) -> Foo {
217 ///         println!("Dividing!");
218 ///         *self
219 ///     }
220 /// }
221 ///
222 /// fn main() {
223 ///     Foo / Foo;
224 /// }
225 /// ```
226 #[lang="div"]
227 pub trait Div<Sized? RHS, Result> for Sized? {
228     /// The method for the `/` operator
229     fn div(&self, rhs: &RHS) -> Result;
230 }
231
232 macro_rules! div_impl(
233     ($($t:ty)*) => ($(
234         impl Div<$t, $t> for $t {
235             #[inline]
236             fn div(&self, other: &$t) -> $t { (*self) / (*other) }
237         }
238     )*)
239 )
240
241 div_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
242
243 /// The `Rem` trait is used to specify the functionality of `%`.
244 ///
245 /// # Example
246 ///
247 /// A trivial implementation of `Rem`. When `Foo % Foo` happens, it ends up
248 /// calling `rem`, and therefore, `main` prints `Remainder-ing!`.
249 ///
250 /// ```
251 /// struct Foo;
252 ///
253 /// impl Copy for Foo {}
254 ///
255 /// impl Rem<Foo, Foo> for Foo {
256 ///     fn rem(&self, _rhs: &Foo) -> Foo {
257 ///         println!("Remainder-ing!");
258 ///         *self
259 ///     }
260 /// }
261 ///
262 /// fn main() {
263 ///     Foo % Foo;
264 /// }
265 /// ```
266 #[lang="rem"]
267 pub trait Rem<Sized? RHS, Result>  for Sized? {
268     /// The method for the `%` operator
269     fn rem(&self, rhs: &RHS) -> Result;
270 }
271
272 macro_rules! rem_impl(
273     ($($t:ty)*) => ($(
274         impl Rem<$t, $t> for $t {
275             #[inline]
276             fn rem(&self, other: &$t) -> $t { (*self) % (*other) }
277         }
278     )*)
279 )
280
281 macro_rules! rem_float_impl(
282     ($t:ty, $fmod:ident) => {
283         impl Rem<$t, $t> for $t {
284             #[inline]
285             fn rem(&self, other: &$t) -> $t {
286                 extern { fn $fmod(a: $t, b: $t) -> $t; }
287                 unsafe { $fmod(*self, *other) }
288             }
289         }
290     }
291 )
292
293 rem_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
294 rem_float_impl!(f32, fmodf)
295 rem_float_impl!(f64, fmod)
296
297 /// The `Neg` trait is used to specify the functionality of unary `-`.
298 ///
299 /// # Example
300 ///
301 /// A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
302 /// `neg`, and therefore, `main` prints `Negating!`.
303 ///
304 /// ```
305 /// struct Foo;
306 ///
307 /// impl Copy for Foo {}
308 ///
309 /// impl Neg<Foo> for Foo {
310 ///     fn neg(&self) -> Foo {
311 ///         println!("Negating!");
312 ///         *self
313 ///     }
314 /// }
315 ///
316 /// fn main() {
317 ///     -Foo;
318 /// }
319 /// ```
320 #[lang="neg"]
321 pub trait Neg<Result> for Sized? {
322     /// The method for the unary `-` operator
323     fn neg(&self) -> Result;
324 }
325
326 macro_rules! neg_impl(
327     ($($t:ty)*) => ($(
328         impl Neg<$t> for $t {
329             #[inline]
330             fn neg(&self) -> $t { -*self }
331         }
332     )*)
333 )
334
335 macro_rules! neg_uint_impl(
336     ($t:ty, $t_signed:ty) => {
337         impl Neg<$t> for $t {
338             #[inline]
339             fn neg(&self) -> $t { -(*self as $t_signed) as $t }
340         }
341     }
342 )
343
344 neg_impl!(int i8 i16 i32 i64 f32 f64)
345
346 neg_uint_impl!(uint, int)
347 neg_uint_impl!(u8, i8)
348 neg_uint_impl!(u16, i16)
349 neg_uint_impl!(u32, i32)
350 neg_uint_impl!(u64, i64)
351
352
353 /// The `Not` trait is used to specify the functionality of unary `!`.
354 ///
355 /// # Example
356 ///
357 /// A trivial implementation of `Not`. When `!Foo` happens, it ends up calling
358 /// `not`, and therefore, `main` prints `Not-ing!`.
359 ///
360 /// ```
361 /// struct Foo;
362 ///
363 /// impl Copy for Foo {}
364 ///
365 /// impl Not<Foo> for Foo {
366 ///     fn not(&self) -> Foo {
367 ///         println!("Not-ing!");
368 ///         *self
369 ///     }
370 /// }
371 ///
372 /// fn main() {
373 ///     !Foo;
374 /// }
375 /// ```
376 #[lang="not"]
377 pub trait Not<Result> for Sized? {
378     /// The method for the unary `!` operator
379     fn not(&self) -> Result;
380 }
381
382
383 macro_rules! not_impl(
384     ($($t:ty)*) => ($(
385         impl Not<$t> for $t {
386             #[inline]
387             fn not(&self) -> $t { !*self }
388         }
389     )*)
390 )
391
392 not_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
393
394 /// The `BitAnd` trait is used to specify the functionality of `&`.
395 ///
396 /// # Example
397 ///
398 /// A trivial implementation of `BitAnd`. When `Foo & Foo` happens, it ends up
399 /// calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`.
400 ///
401 /// ```
402 /// struct Foo;
403 ///
404 /// impl Copy for Foo {}
405 ///
406 /// impl BitAnd<Foo, Foo> for Foo {
407 ///     fn bitand(&self, _rhs: &Foo) -> Foo {
408 ///         println!("Bitwise And-ing!");
409 ///         *self
410 ///     }
411 /// }
412 ///
413 /// fn main() {
414 ///     Foo & Foo;
415 /// }
416 /// ```
417 #[lang="bitand"]
418 pub trait BitAnd<Sized? RHS, Result> for Sized? {
419     /// The method for the `&` operator
420     fn bitand(&self, rhs: &RHS) -> Result;
421 }
422
423 macro_rules! bitand_impl(
424     ($($t:ty)*) => ($(
425         impl BitAnd<$t, $t> for $t {
426             #[inline]
427             fn bitand(&self, rhs: &$t) -> $t { (*self) & (*rhs) }
428         }
429     )*)
430 )
431
432 bitand_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
433
434 /// The `BitOr` trait is used to specify the functionality of `|`.
435 ///
436 /// # Example
437 ///
438 /// A trivial implementation of `BitOr`. When `Foo | Foo` happens, it ends up
439 /// calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`.
440 ///
441 /// ```
442 /// struct Foo;
443 ///
444 /// impl Copy for Foo {}
445 ///
446 /// impl BitOr<Foo, Foo> for Foo {
447 ///     fn bitor(&self, _rhs: &Foo) -> Foo {
448 ///         println!("Bitwise Or-ing!");
449 ///         *self
450 ///     }
451 /// }
452 ///
453 /// fn main() {
454 ///     Foo | Foo;
455 /// }
456 /// ```
457 #[lang="bitor"]
458 pub trait BitOr<Sized? RHS, Result> for Sized? {
459     /// The method for the `|` operator
460     fn bitor(&self, rhs: &RHS) -> Result;
461 }
462
463 macro_rules! bitor_impl(
464     ($($t:ty)*) => ($(
465         impl BitOr<$t,$t> for $t {
466             #[inline]
467             fn bitor(&self, rhs: &$t) -> $t { (*self) | (*rhs) }
468         }
469     )*)
470 )
471
472 bitor_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
473
474 /// The `BitXor` trait is used to specify the functionality of `^`.
475 ///
476 /// # Example
477 ///
478 /// A trivial implementation of `BitXor`. When `Foo ^ Foo` happens, it ends up
479 /// calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`.
480 ///
481 /// ```
482 /// struct Foo;
483 ///
484 /// impl Copy for Foo {}
485 ///
486 /// impl BitXor<Foo, Foo> for Foo {
487 ///     fn bitxor(&self, _rhs: &Foo) -> Foo {
488 ///         println!("Bitwise Xor-ing!");
489 ///         *self
490 ///     }
491 /// }
492 ///
493 /// fn main() {
494 ///     Foo ^ Foo;
495 /// }
496 /// ```
497 #[lang="bitxor"]
498 pub trait BitXor<Sized? RHS, Result> for Sized? {
499     /// The method for the `^` operator
500     fn bitxor(&self, rhs: &RHS) -> Result;
501 }
502
503 macro_rules! bitxor_impl(
504     ($($t:ty)*) => ($(
505         impl BitXor<$t, $t> for $t {
506             #[inline]
507             fn bitxor(&self, other: &$t) -> $t { (*self) ^ (*other) }
508         }
509     )*)
510 )
511
512 bitxor_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
513
514 /// The `Shl` trait is used to specify the functionality of `<<`.
515 ///
516 /// # Example
517 ///
518 /// A trivial implementation of `Shl`. When `Foo << Foo` happens, it ends up
519 /// calling `shl`, and therefore, `main` prints `Shifting left!`.
520 ///
521 /// ```
522 /// struct Foo;
523 ///
524 /// impl Copy for Foo {}
525 ///
526 /// impl Shl<Foo, Foo> for Foo {
527 ///     fn shl(&self, _rhs: &Foo) -> Foo {
528 ///         println!("Shifting left!");
529 ///         *self
530 ///     }
531 /// }
532 ///
533 /// fn main() {
534 ///     Foo << Foo;
535 /// }
536 /// ```
537 #[lang="shl"]
538 pub trait Shl<Sized? RHS, Result> for Sized? {
539     /// The method for the `<<` operator
540     fn shl(&self, rhs: &RHS) -> Result;
541 }
542
543 macro_rules! shl_impl(
544     ($($t:ty)*) => ($(
545         impl Shl<uint, $t> for $t {
546             #[inline]
547             fn shl(&self, other: &uint) -> $t {
548                 (*self) << (*other)
549             }
550         }
551     )*)
552 )
553
554 shl_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
555
556 /// The `Shr` trait is used to specify the functionality of `>>`.
557 ///
558 /// # Example
559 ///
560 /// A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
561 /// calling `shr`, and therefore, `main` prints `Shifting right!`.
562 ///
563 /// ```
564 /// struct Foo;
565 ///
566 /// impl Copy for Foo {}
567 ///
568 /// impl Shr<Foo, Foo> for Foo {
569 ///     fn shr(&self, _rhs: &Foo) -> Foo {
570 ///         println!("Shifting right!");
571 ///         *self
572 ///     }
573 /// }
574 ///
575 /// fn main() {
576 ///     Foo >> Foo;
577 /// }
578 /// ```
579 #[lang="shr"]
580 pub trait Shr<Sized? RHS, Result> for Sized? {
581     /// The method for the `>>` operator
582     fn shr(&self, rhs: &RHS) -> Result;
583 }
584
585 macro_rules! shr_impl(
586     ($($t:ty)*) => ($(
587         impl Shr<uint, $t> for $t {
588             #[inline]
589             fn shr(&self, other: &uint) -> $t { (*self) >> (*other) }
590         }
591     )*)
592 )
593
594 shr_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
595
596 /// The `Index` trait is used to specify the functionality of indexing operations
597 /// like `arr[idx]` when used in an immutable context.
598 ///
599 /// # Example
600 ///
601 /// A trivial implementation of `Index`. When `Foo[Foo]` happens, it ends up
602 /// calling `index`, and therefore, `main` prints `Indexing!`.
603 ///
604 /// ```
605 /// struct Foo;
606 ///
607 /// impl Copy for Foo {}
608 ///
609 /// impl Index<Foo, Foo> for Foo {
610 ///     fn index<'a>(&'a self, _index: &Foo) -> &'a Foo {
611 ///         println!("Indexing!");
612 ///         self
613 ///     }
614 /// }
615 ///
616 /// fn main() {
617 ///     Foo[Foo];
618 /// }
619 /// ```
620 #[lang="index"]
621 pub trait Index<Sized? Index, Sized? Result> for Sized? {
622     /// The method for the indexing (`Foo[Bar]`) operation
623     fn index<'a>(&'a self, index: &Index) -> &'a Result;
624 }
625
626 /// The `IndexMut` trait is used to specify the functionality of indexing
627 /// operations like `arr[idx]`, when used in a mutable context.
628 ///
629 /// # Example
630 ///
631 /// A trivial implementation of `IndexMut`. When `Foo[Foo]` happens, it ends up
632 /// calling `index_mut`, and therefore, `main` prints `Indexing!`.
633 ///
634 /// ```
635 /// struct Foo;
636 ///
637 /// impl Copy for Foo {}
638 ///
639 /// impl IndexMut<Foo, Foo> for Foo {
640 ///     fn index_mut<'a>(&'a mut self, _index: &Foo) -> &'a mut Foo {
641 ///         println!("Indexing!");
642 ///         self
643 ///     }
644 /// }
645 ///
646 /// fn main() {
647 ///     &mut Foo[Foo];
648 /// }
649 /// ```
650 #[lang="index_mut"]
651 pub trait IndexMut<Sized? Index, Sized? Result> for Sized? {
652     /// The method for the indexing (`Foo[Bar]`) operation
653     fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Result;
654 }
655
656 /// The `Slice` trait is used to specify the functionality of slicing operations
657 /// like `arr[from..to]` when used in an immutable context.
658 ///
659 /// # Example
660 ///
661 /// A trivial implementation of `Slice`. When `Foo[..Foo]` happens, it ends up
662 /// calling `slice_to`, and therefore, `main` prints `Slicing!`.
663 ///
664 /// ```ignore
665 /// struct Foo;
666 ///
667 /// impl Copy for Foo {}
668 ///
669 /// impl Slice<Foo, Foo> for Foo {
670 ///     fn as_slice_<'a>(&'a self) -> &'a Foo {
671 ///         println!("Slicing!");
672 ///         self
673 ///     }
674 ///     fn slice_from_or_fail<'a>(&'a self, _from: &Foo) -> &'a Foo {
675 ///         println!("Slicing!");
676 ///         self
677 ///     }
678 ///     fn slice_to_or_fail<'a>(&'a self, _to: &Foo) -> &'a Foo {
679 ///         println!("Slicing!");
680 ///         self
681 ///     }
682 ///     fn slice_or_fail<'a>(&'a self, _from: &Foo, _to: &Foo) -> &'a Foo {
683 ///         println!("Slicing!");
684 ///         self
685 ///     }
686 /// }
687 ///
688 /// fn main() {
689 ///     Foo[..Foo];
690 /// }
691 /// ```
692 #[lang="slice"]
693 pub trait Slice<Sized? Idx, Sized? Result> for Sized? {
694     /// The method for the slicing operation foo[]
695     fn as_slice_<'a>(&'a self) -> &'a Result;
696     /// The method for the slicing operation foo[from..]
697     fn slice_from_or_fail<'a>(&'a self, from: &Idx) -> &'a Result;
698     /// The method for the slicing operation foo[..to]
699     fn slice_to_or_fail<'a>(&'a self, to: &Idx) -> &'a Result;
700     /// The method for the slicing operation foo[from..to]
701     fn slice_or_fail<'a>(&'a self, from: &Idx, to: &Idx) -> &'a Result;
702 }
703
704 /// The `SliceMut` trait is used to specify the functionality of slicing
705 /// operations like `arr[from..to]`, when used in a mutable context.
706 ///
707 /// # Example
708 ///
709 /// A trivial implementation of `SliceMut`. When `Foo[Foo..]` happens, it ends up
710 /// calling `slice_from_mut`, and therefore, `main` prints `Slicing!`.
711 ///
712 /// ```ignore
713 /// struct Foo;
714 ///
715 /// impl Copy for Foo {}
716 ///
717 /// impl SliceMut<Foo, Foo> for Foo {
718 ///     fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Foo {
719 ///         println!("Slicing!");
720 ///         self
721 ///     }
722 ///     fn slice_from_or_fail_mut<'a>(&'a mut self, _from: &Foo) -> &'a mut Foo {
723 ///         println!("Slicing!");
724 ///         self
725 ///     }
726 ///     fn slice_to_or_fail_mut<'a>(&'a mut self, _to: &Foo) -> &'a mut Foo {
727 ///         println!("Slicing!");
728 ///         self
729 ///     }
730 ///     fn slice_or_fail_mut<'a>(&'a mut self, _from: &Foo, _to: &Foo) -> &'a mut Foo {
731 ///         println!("Slicing!");
732 ///         self
733 ///     }
734 /// }
735 ///
736 /// pub fn main() {
737 ///     Foo[mut Foo..];
738 /// }
739 /// ```
740 #[lang="slice_mut"]
741 pub trait SliceMut<Sized? Idx, Sized? Result> for Sized? {
742     /// The method for the slicing operation foo[]
743     fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Result;
744     /// The method for the slicing operation foo[from..]
745     fn slice_from_or_fail_mut<'a>(&'a mut self, from: &Idx) -> &'a mut Result;
746     /// The method for the slicing operation foo[..to]
747     fn slice_to_or_fail_mut<'a>(&'a mut self, to: &Idx) -> &'a mut Result;
748     /// The method for the slicing operation foo[from..to]
749     fn slice_or_fail_mut<'a>(&'a mut self, from: &Idx, to: &Idx) -> &'a mut Result;
750 }
751
752 /// The `Deref` trait is used to specify the functionality of dereferencing
753 /// operations like `*v`.
754 ///
755 /// # Example
756 ///
757 /// A struct with a single field which is accessible via dereferencing the
758 /// struct.
759 ///
760 /// ```
761 /// struct DerefExample<T> {
762 ///     value: T
763 /// }
764 ///
765 /// impl<T> Deref<T> for DerefExample<T> {
766 ///     fn deref<'a>(&'a self) -> &'a T {
767 ///         &self.value
768 ///     }
769 /// }
770 ///
771 /// fn main() {
772 ///     let x = DerefExample { value: 'a' };
773 ///     assert_eq!('a', *x);
774 /// }
775 /// ```
776 #[lang="deref"]
777 pub trait Deref<Sized? Result> for Sized? {
778     /// The method called to dereference a value
779     fn deref<'a>(&'a self) -> &'a Result;
780 }
781
782 impl<'a, Sized? T> Deref<T> for &'a T {
783     fn deref(&self) -> &T { *self }
784 }
785
786 impl<'a, Sized? T> Deref<T> for &'a mut T {
787     fn deref(&self) -> &T { *self }
788 }
789
790 /// The `DerefMut` trait is used to specify the functionality of dereferencing
791 /// mutably like `*v = 1;`
792 ///
793 /// # Example
794 ///
795 /// A struct with a single field which is modifiable via dereferencing the
796 /// struct.
797 ///
798 /// ```
799 /// struct DerefMutExample<T> {
800 ///     value: T
801 /// }
802 ///
803 /// impl<T> Deref<T> for DerefMutExample<T> {
804 ///     fn deref<'a>(&'a self) -> &'a T {
805 ///         &self.value
806 ///     }
807 /// }
808 ///
809 /// impl<T> DerefMut<T> for DerefMutExample<T> {
810 ///     fn deref_mut<'a>(&'a mut self) -> &'a mut T {
811 ///         &mut self.value
812 ///     }
813 /// }
814 ///
815 /// fn main() {
816 ///     let mut x = DerefMutExample { value: 'a' };
817 ///     *x = 'b';
818 ///     assert_eq!('b', *x);
819 /// }
820 /// ```
821 #[lang="deref_mut"]
822 pub trait DerefMut<Sized? Result> for Sized? : Deref<Result> {
823     /// The method called to mutably dereference a value
824     fn deref_mut<'a>(&'a mut self) -> &'a mut Result;
825 }
826
827 impl<'a, Sized? T> DerefMut<T> for &'a mut T {
828     fn deref_mut(&mut self) -> &mut T { *self }
829 }
830
831 /// A version of the call operator that takes an immutable receiver.
832 #[lang="fn"]
833 pub trait Fn<Args,Result> for Sized? {
834     /// This is called when the call operator is used.
835     extern "rust-call" fn call(&self, args: Args) -> Result;
836 }
837
838 /// A version of the call operator that takes a mutable receiver.
839 #[lang="fn_mut"]
840 pub trait FnMut<Args,Result> for Sized? {
841     /// This is called when the call operator is used.
842     extern "rust-call" fn call_mut(&mut self, args: Args) -> Result;
843 }
844
845 /// A version of the call operator that takes a by-value receiver.
846 #[lang="fn_once"]
847 pub trait FnOnce<Args,Result> {
848     /// This is called when the call operator is used.
849     extern "rust-call" fn call_once(self, args: Args) -> Result;
850 }
851
852 impl<Sized? F,A,R> FnMut<A,R> for F
853     where F : Fn<A,R>
854 {
855     extern "rust-call" fn call_mut(&mut self, args: A) -> R {
856         self.call(args)
857     }
858 }
859
860 impl<F,A,R> FnOnce<A,R> for F
861     where F : FnMut<A,R>
862 {
863     extern "rust-call" fn call_once(mut self, args: A) -> R {
864         self.call_mut(args)
865     }
866 }
867
868 #[cfg(stage0)]
869 mod fn_impls {
870     use super::Fn;
871
872     impl<Result> Fn<(),Result> for extern "Rust" fn() -> Result {
873         #[allow(non_snake_case)]
874         extern "rust-call" fn call(&self, _args: ()) -> Result {
875             (*self)()
876         }
877     }
878
879     impl<Result,A0> Fn<(A0,),Result> for extern "Rust" fn(A0) -> Result {
880         #[allow(non_snake_case)]
881         extern "rust-call" fn call(&self, args: (A0,)) -> Result {
882             let (a0,) = args;
883             (*self)(a0)
884         }
885     }
886
887     macro_rules! def_fn(
888         ($($args:ident)*) => (
889             impl<Result$(,$args)*>
890             Fn<($($args,)*),Result>
891             for extern "Rust" fn($($args: $args,)*) -> Result {
892                 #[allow(non_snake_case)]
893                 extern "rust-call" fn call(&self, args: ($($args,)*)) -> Result {
894                     let ($($args,)*) = args;
895                     (*self)($($args,)*)
896                 }
897             }
898         )
899     )
900
901     def_fn!(A0 A1)
902     def_fn!(A0 A1 A2)
903     def_fn!(A0 A1 A2 A3)
904     def_fn!(A0 A1 A2 A3 A4)
905     def_fn!(A0 A1 A2 A3 A4 A5)
906     def_fn!(A0 A1 A2 A3 A4 A5 A6)
907     def_fn!(A0 A1 A2 A3 A4 A5 A6 A7)
908     def_fn!(A0 A1 A2 A3 A4 A5 A6 A7 A8)
909     def_fn!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9)
910     def_fn!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10)
911     def_fn!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11)
912     def_fn!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12)
913     def_fn!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13)
914     def_fn!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14)
915     def_fn!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15)
916 }